PHP Socket Programming Read-write blocking after completion

Source: Internet
Author: User
PHP Socket programming Read completion after writing blocking
The server-side code is as follows:

Set_time_limit (0);

$host = "127.0.0.1";
$port = 12345;

$socket = Socket_create (Af_inet, Sock_stream, sol_tcp) or Die ("Could not createsocket\n"); Create a socket

$result = Socket_bind ($socket, $host, $port) or Die ("Could not bind tosocket\n"); Bind socket to Port

$result = Socket_listen ($socket) or Die ("Could not set up socket listener\n"); Start listening for connections

$spawn = socket_accept ($socket) or Die ("Could not accept incoming connection\n"); Handling Communications

If you switch to a loop to receive the data, the client will process waiting for the receive state
$input = Socket_read ($spawn, 1024x768) or Die ("Could not read data\n");
/*
while ($data = Socket_read ($spawn, 1024x768) = = False)
$input. = $data;
*/

echo ' input: ', strlen ($input), "\ n";

$output = Date ("y-m-d h:i:s"). "\ n"; Processing client input and returning results

echo "Output:", $output, "\ n";

Data transfer writes the returned results to the client
Socket_write ($spawn, $output, strlen ($output)) or Die ("Could not write output\n");

Close sockets
Socket_close ($spawn);
Socket_close ($socket);


Client side, the code is as follows:


Set_time_limit (0);

$host = "127.0.0.1";
$port = 12345;

$socket = Socket_create (Af_inet, Sock_stream, sol_tcp) or Die ("Could not createsocket\n"); Create a socket

$connection = Socket_connect ($socket, $host, $port) or Die ("Could not connet server\n"); Connection

Socket_write ($socket, "time") or Die ("Write failed\n"); Data transfer sends a message to the server

Echo ' Wait for data ';

while ($buffer = Socket_read ($socket, 1024x768))!=false) {
Echo ("Data sent Was:time\nresponse was:".) $buffer. "\ n");
}

Socket_close ($socket);

If the server side is replaced with the client read operation as a loop internal read, the client will remain in the wait data state.
Is there a way to let the server loop read the case, after reading to the client to write data?



Socket TCP PHP Server

Share to:


------Solution--------------------
You have nothing wrong with this loop, but once the data is sent, the socket connection is closed and there is no need to loop.
------Solution--------------------
while (true) {
$msg = Socket_accept ($socket); Accept a socket
if (! $msg) {
echo "Socket_accept () failed:". Socket_strerror ($msg). " \ n ";
Break
}
while (true) {
$command = Strtoupper (Trim (Socket_read ($msg, 1024))); Waiting for client data
if (! $command) break;
Related processing, such as direct echo
Socket_write ($msg, $command, strlen ($command));
}
Socket_close ($msg);
if ($command = = "QUIT")//If you receive the end of the communication
Break
}
Socket_close ($socket); Close socket

------Solution--------------------
reference:
Quote: reference:
 
!--? php
set_time_limit (0);

$host = "127.0.0.1";
$port = 12345;

$socket = socket_create (Af_inet, Sock_stream, sol_tcp) or Die ("Could not createsocket\n");//Create a socket

$result = Socket_bind ($socket, $host, $port) or Die ("Could not bind tosocket\n"); Bind socket to port

$result = Socket_listen ($socket) or Die ("Could not set up socket listener\n");//Start listening connection

$sp Awn = socket_accept ($socket) or Die ("Could not accept incoming connection\n"); Processing communication

//If you switch to a loop to receive data, the client will always process waiting for the receive status
$input = Socket_read ($spawn, 1024x768) or Die ("Could not read data\n");
  • Related Article

    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.