PHP socket Communication Mechanism example description and code _php tutorial

Source: Internet
Author: User
PHP socket Communication Mechanism example description and code

PHP Tutorial Socket Communication Mechanism example description and code

One, what is a socket?

What is a socket a so-called socket, commonly referred to as a "socket", is used to describe an IP address and port, which is a handle to a communication chain. Applications typically make requests to the network through sockets or answer network requests. Plainly speaking is a communication mechanism. It is similar to the Telephone Customer service department of these parts of banks, telecommunications. When you call, there will be the allocation of a person to answer your question, the Customer service department is equivalent to the server side of the socket, your side of the equivalent of the client, in and before you call the end, if someone is looking to talk with you talking, is not possible, because you are communicating with him, Of course, the telephone switchboard of the customer service department will not be allocated repeatedly.
Let me give you an example of how the socket works. If you're a developer based on an application layer, you don't have to understand the principle, but it's better to know that. PHP API for sockets on the Web. It's OK to use it down.

Second, socket server server.php
Establishing a server-side socket
$tcp = Getprotobyname ("TCP");
$socket = Socket_create (Af_inet, Sock_stream, $tcp);
Socket_bind ($socket, ' 127.0.0.1 ', 10008); Bind the port to listen on
Socket_listen ($socket); Listening port

Initializes a data, and communicates with the client
$buffer = "Connect";
while (true) {
Accept a socket connection
$connection = Socket_accept ($socket);
if (! $connection) {
echo "Connect fail";
}else{
echo "Socket Connectedn";
Passing an information data to the client
if ($buffer! = "") {
echo "Send data to Clientn";
Socket_write ($connection, $buffer. "N");
Echo "wrote to Socketn";
} else {
echo "No data in the Buffern";
}
Getting information from the client
while ($data = @socket_read ($connection, 1024x768, php_normal_read)) {
printf ("Buffer:".) $data. "N");
Get information to the client for a feedback
Socket_write ($connection, "Information Receivedn");
}
}

Socket_close ($connection);
Close socket
printf ("closed the Socketn");
}
?>

Third, socket client client.php
Establish a SOCET connection for the client
$socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
$connection = Socket_connect ($socket, ' 127.0.0.1 ', 10008); Connecting a server-side socket

while ($buffer = @socket_read ($socket, 1024x768, php_normal_read)) {
The server tells the client that its state
if (Preg_match ("/not connect/", $buffer)) {
echo "Don t connectn";
Break
} else {
Message from the server
echo "Buffer data:". $buffer. "N";

echo "Writing to Socketn";
Write the customer's information to the channel and pass it to the server side
if (!socket_write ($socket, "some Datan")) {
echo "Write Failedn";
}
The response information that is given after the server has received the information
while ($buffer = Socket_read ($socket, 1024x768, php_normal_read)) {
echo "Sent to Server:some Datan response from server is:". $buffer. "N";
}

}
}

?>

Iv. a picture of the communication mechanism (do not consider the end of the wait time to automatically close the socket)

A brief explanation, before the explanation I want to say, why to use PHP command to execute the server and client, the client you use Fsockopen, so it is accessed on the browser, not at least the connection timeout. Why is this, because when you create a socket, it will constantly listen to the customer there is no need to connect.

Socket communication is established to two socket channels, one is created on the server side, and one is created by the client.
Line Line 1, the server creates a socket channel and places the information in the cache, waiting for the client to connect
Line Line 2, the client creates a socket channel, and connects to the server side, obtains the server side information to communicate, will transmit the information to send the channel
Line Line 3, the server-side obtains information from the client and tells the client that the information I have received. The information to be transmitted is sent to the channel
Line Line 4, client obtains confirmation from server side
To this one communication is completely established, after the data transfer is complete, the server will disconnect the socket communication, the operation is as follows


1, run the socket server side


[Root@blackghost zhangy]#/usr/local/php/bin/php-a/home/zhangy/www/test2/server.php
Interactive mode enabled
That's the job.
A, listening on a 10008 port
[Zhangy@blackghost ~]$ Netstat-an |grep Listen |grep 10008
TCP 0 0 127.0.0.1:10008 0.0.0.0:* Listen
[Zhangy@blackghost ~]$
b, send the message to the cache $buffer = "Connect"; Look at the code above.

2, run the socket client

[Root@blackghost zhangy]#/usr/local/php/bin/php-a/home/zhangy/www/test2/client.php
Interactive mode enabled

Buffer Data:connect

Writing to socket
Sent to Server:some data
Response from server Was:information received

3, back to server side

[Root@blackghost zhangy]#/usr/local/php/bin/php-a/home/zhangy/www/test2/server.php
Interactive mode enabled

Socket connected
Send data to Client
wrote to socket
Buffer:some data

Closed the socket


http://www.bkjia.com/PHPjc/444905.html www.bkjia.com true http://www.bkjia.com/PHPjc/444905.html techarticle PHP Socket Communication Mechanism example description with Code PHP tutorial Socket communication Mechanism Instance description with code one, what is socket what is a socket called socket is usually also called socket, used to describe ...

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