Php socket C/S Communication Server cannot receive client information

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.