PHP in the socket communication mechanism example detailed _php tutorial

Source: Internet
Author: User

A detailed example of socket communication mechanism in PHP


This article mainly introduces the socket communication mechanism in PHP, tells the principle of the socket communication mechanism, and analyzes the usage of the socket communication mechanism in detail in the instance form, and needs the friend to refer to the following

This paper describes the socket communication mechanism and usage in PHP. Share to everyone for your reference. The specific analysis is as follows:

First, what is the 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

The code is as follows:

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 connected";
Passing an information data to the client
if ($buffer! = "") {
echo "Send data to Client";
Socket_write ($connection, $buffer. " ");
echo "wrote to Socket";
} else {
echo "No data in the buffer";
}
Getting information from the client
while ($data = @socket_read ($connection, 1024x768, php_normal_read)) {
printf ("Buffer:".) $data. " ");
Get information to the client for a feedback
Socket_write ($connection, "information received");
}
}

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


Third, socket client client.php

The code is as follows:

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 connect";
Break
} else {
Message from the server
echo "Buffer data:". $buffer. " ";

echo "Writing to Socket";
Write the customer's information to the channel and pass it to the server side
if (!socket_write ($socket, "some data")) {
echo "Write Failed";
}
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 data response from server is:". $buffer. " ";
}

}
}
?>


Iv. a picture of the communication mechanism (automatically closes the socket without considering the end of the wait time)

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 specific operation is as follows.

1 running the socket server side

The code is as follows:

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


That's the job.

A listens on a 10008 port

The code is as follows:

[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 information to the cache

The code is as follows:

$buffer = "Connect";

Look at the code above.

2 Running the socket client

The code is as follows:

[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

The code is as follows:

[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

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/975892.html www.bkjia.com true http://www.bkjia.com/PHPjc/975892.html techarticle example of socket communication mechanism in PHP this article mainly introduces the socket communication mechanism in PHP, tells the principle of the socket communication mechanism, and analyzes the socket communication machine in detail in the instance form.

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