<?php
//The Client
error_reporting(E_ALL);
$address = "127.0.0.1";
$port = 10000;
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "socket successfully created.\n";
}
echo "Attempting to connect to '$address' on port '$port'...";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "successfully connected to $address.\n";
}
$i = 0;
while (true == true)
{
$i++;
echo "Sending $i to server.\n";
socket_write($socket, $i, strlen($i));
$input = socket_read($socket, 2048);
echo "Response from server is: $input\n";
sleep(5);
}
echo "Closing socket...";
socket_close($socket);
?>
run ที่ localhost แล้วได้ผลลัพธ์แบบนี้
socket successfully created. Attempting to connect to '127.0.0.1' on port '10000'...
Warning: socket_connect() [function.socket-connect]: unable to connect [0]: No connection could be made because the target machine actively refused it. in C:\Apache\WebApp\socket.php on line 17
socket_connect() failed. Reason: () No connection could be made because the target machine actively refused it. Sending 1 to server.
Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 29
Warning: socket_read() [function.socket-read]: unable to read from socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 31
Response from server is: Sending 2 to server.
Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 29
Warning: socket_read() [function.socket-read]: unable to read from socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 31
Response from server is: Sending 3 to server.
Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 29
Warning: socket_read() [function.socket-read]: unable to read from socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 31
Response from server is: Sending 4 to server.
Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 29
Warning: socket_read() [function.socket-read]: unable to read from socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 31
Response from server is: Sending 5 to server.
Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 29
Warning: socket_read() [function.socket-read]: unable to read from socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 31
Response from server is: Sending 6 to server.
Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 29
Warning: socket_read() [function.socket-read]: unable to read from socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\Apache\WebApp\socket.php on line 31
Response from server is:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Apache\WebApp\socket.php on line 34