Server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php // server.php // Remember to use double quote for \r\n $ip = '127.0.0.1'; $port = '1337'; // Set up our socket $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\r\n"; }else{ echo "socket_create() succeed.\n"; } if (socket_bind($socket, $ip, $port) === false) { echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\r\n"; }else{ echo "socket_bind() succeed.\r\n"; } socket_listen($socket); echo "socket_listen() succeed.\r\n"; //socket_set_nonblock($socket); // Initialize the buffer $buffer = "NO DATA"; while(true) { // Accept any connections coming in on this socket echo "Accepting...\n"; //System will be pending and waiting for connections $connection = socket_accept($socket); if ($connection === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n"; break; } printf("Socket connected\r\n"); //Client must use socket_read($connection, 1024, PHP_BINARY_READ) to receive multiple lines $msg = "\nWelcome to the PHP Test Server. \n" . "To quit, type 'quit'. To shut down the server type 'shutdown'.\n"; socket_write($connection, $msg, strlen($msg)); // Check to see if there is anything in the buffer echo "buffer:" . $buffer . "\n"; if($buffer != "") { //https://openhome.cc/Gossip/CGossip/PrintfScanf.html //基本上,printf() 就是將指定的文字、數值等輸出至螢幕上,並且執行過後會傳回所輸出的字元數 printf("Something is in the buffer...sending data...\r\n"); socket_write($connection, $buffer . "\r\n"); printf("Wrote to socket\r\n"); } else { printf("No Data in the buffer\r\n"); } // Get the input /* PHP_NORMAL_READ: single line PHP_BINARY_READ: multiple line (Default) */ while($data = socket_read($connection, 1024, PHP_NORMAL_READ)) { $buffer = $data; socket_write($connection, "Information Received\r\n"); printf("Buffer: " . $buffer . "\r\n"); } socket_close($connection); printf("Closed the socket\r\n\r\n"); } ?> |
Client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php // client.php // Remember to use double quote for \r\n $ip = '127.0.0.1'; $port = '1337'; // Create the socket and connect $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $connection = socket_connect($socket, $ip, $port); while($buffer = socket_read($socket, 1024)) { //printf("Buffer:%s\n", $buffer); echo "buffer:" . $buffer . "\n"; if($buffer == 'NO DATA') { echo('NO DATA'); break; } else { // Do something with the data in the buffer echo("Buffer Data: " . $buffer . ""); break; } } echo('Writing to Socket'); // Write some test data to our socket if(!socket_write($socket, "SOME DATA\r\n")) { echo('Write failed'); } // Read any response from the socket while($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)){ echo("Data sent was: SOME DATA Response was:" . $buffer . ""); } echo('Done Reading from Socket'); exit(); ?> |
Commands:
php server.php
php client.php