About the socket for PHP

Source: Internet
Author: User
Tags php server
Recently learning PHP socket, want to pass a function of the implementation to check my understanding of the socket:
1. Client side sends data to server side
2. Server side receives the data sent by the client and sends a response to the client side.
3. Client side Displays the response information sent by server side

Here is the code

server.php:
Set up our sockets
$commonProtocol = Getprotobyname ("TCP");
$socket = Socket_create (Af_inet, Sock_stream, $commonProtocol);
Socket_bind ($socket, ' localhost ', 1234);
Socket_listen ($socket);
Initialize the buffer
$buffer = "NO DATA";
while (true) {
Accept any connections coming "in" this socket
$connection = Socket_accept ($socket);
printf ("Socket connected\r\n");
Check to see if there are anything in the buffer
$buffer =socket_read ($connection, 2048, php_normal_read);
echo "recived from client". $buffer;
$ResponseToClient = $buffer. "Is handled by server!\n";
Socket_write ($connection, $ResponseToClient);
echo "Response to client!\n";
}
?>

client.php:
Create the socket and connect
$socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
$connection = Socket_connect ($socket, ' localhost ', 65500);
$i = 0;
$s = 0;
while (true)
{
echo "Sending data". $i + +. " \ n ";
$SendDataToServer =socket_write ($socket, "server". $s + +);
$ResponseFromServer =socket_read ($socket, 2048, php_normal_read);
echo "received". $ResponseFromServer. " From server \ n ";
}
?>

The problem is: Client side only shows sending DATA0
And the server side only shows the socket connected
Nothing else shows, did not reach the client I expected to continuously send data, server side constantly processing data and return to the client's purpose, please expert advice, thank you very much


Reply to discussion (solution)

Who's the hero back?

Not very clear, why are the ports on both sides different AH?

Upstairs +1
Another: Even if your program runs smoothly, it's a dead loop.

It seems that most of them have not done socket, disappointment

It seems that most of them have not done socket, disappointment
I think PHP in the socket is not reliable ah ...

Why did you do this without the need?
But I can give you an early test case.
Service side

   
Client
  \ n ", Else Echo" Socket_create () failed: ". Socket_strerror ($socket)."
\ n "; $conn = Socket_connect ($socket, $ip, $port); Establish the socket connection if ($conn) echo "Successfully connected to [". $ip. ":". $port. "]
\ n ", Else Echo" Socket_connect () failed: ". Socket_strerror ($conn)."
\ n "; Echo socket_read ($socket, 1024)."
"; $stdin = fopen (' Php://stdin ', ' R '); $ar = Array (" HELLO "," Help "," Test "," QUIT "), $i = 0;while (True) {//$command = Trim (Fgets ($stdin, 1024x768));//Socket_write ($socket, $command, strlen ($command)); echo "Send $ar [$i]
"; Socket_write ($socket, $ar [$i], strlen ($ar [$i])); $i + +; $msg = Trim (Socket_read ($socket, 1024)); echo "Received: $msg
\ n "; if ($msg = = "Bye-bye") break;} Fclose ($stdin); Socket_close ($socket);

The normal words should show:
Socket_create () Success!
Successfully connected to [127.0.0.1:1000]
Server received: Welcome to PHP server!
Send HELLO
Roger: Hello everybody!
Send Help
Roger: HELLO QUIT Help
Send Test
Received: Error command!
Send QUIT
Roger: Bye-bye

It's a dead loop.

  • 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.