Php socket CS communication server cannot receive client information // Establish the socet connection of the client
$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
// Connect to the server socket
$ Connection = socket_connect ($ socket, '192. 168.142.27 ', 192 );
// The information to be sent to the server.
$ Info = 'Hello World ';
While ($ connection)
{
Socket_write ($ socket, "$ info ");
Break;
}
?>
If no break is added, the server can receive information, but it is a series of 'Hello World ';
When break is added, no information is received;
For beginners, I hope you can answer this question! Thank you!
Reply to discussion (solution)
You have exited the loop and terminated the program, and you have not received the code from the server.
So how do you know that the server has not received the data?
Break; remove it, because you cannot receive data after the program exits.
The receiving code is as follows:
$ Tcp = getprotobyname ("tcp ");
// Create a server socket and return a socket, also known as a communication node. A typical network connection consists of two sockets, one running on the client and the other running on the server.
$ Socket = socket_create (AF_INET, SOCK_STREAM, $ tcp );
// Bind the ip address and port to be listened on. The ip address bound here must be a LAN ip address. The 127.0.0.1 client cannot be connected to the server.
Socket_bind ($ socket, '192. 168.142.27 ', 192 );
// Listening port
Socket_listen ($ socket );
$ Connection = socket_accept ($ socket );
$ Data = '';
$ Count = 0;
While ($ connection)
{
$ Data = @ socket_read ($ connection, 1024, PHP_NORMAL_READ );
Printf ("Buffer:". $ data. "\ n ");
Break;
}
?>
It is found that if the while loop is more than a certain number of times, the receiving end will receive a series of "hello", but when the number of cycles is less than this value, no data will be received at the receiving end (I am using this value at 204 ), what is the situation?