PHP example shows the socket communication mechanism

Source: Internet
Author: User
Tags php example vars

PHP example shows the socket communication mechanism

Zhang Ying Published on 2010-04-24

Category: PHP

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

View copy print?
  1. <?php
  2. Establishing a server-side socket
  3. $tcp = Getprotobyname ("TCP");
  4. $socket = Socket_create (Af_inet, Sock_stream, $tcp);
  5. Socket_bind ($socket, ' 127.0.0.1 ', 10008); //Bind the port to be monitored
  6. Socket_listen ($socket); //Listening port
  7. Initializes a data, and communicates with the client
  8. $buffer = "Connect";
  9. while (true) {
  10. //Accept a socket connection
  11. $connection = socket_accept ($socket);
  12. if (! $connection) {
  13. echo "connect fail";
  14. }else{
  15. echo "Socket connected\n";
  16. //Pass an information data to the client
  17. if ($buffer! = "") {
  18. echo "send data to client\n";
  19. Socket_write ($connection, $buffer.  "\ n");
  20. echo "wrote to Socket\n";
  21. } Else {
  22. echo "No data in the buffer\n";
  23. }
  24. //Get information from the client
  25. While ($data = @socket_read ($connection, 1024x768, Php_normal_read)) {
  26. printf ("Buffer:".) $data.  "\ n");
  27. //Get information to the client for a feedback
  28. Socket_write ($connection, "Information received\n");
  29. }
  30. }
  31. Socket_close ($connection);
  32. //Close socket
  33. printf ("Closed the socket\n");
  34. }
  35. ?>

Third, socket client client.php

View copy print?
  1. <?php
  2. Establish a SOCET connection for the client
  3. $socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
  4. $connection = Socket_connect ($socket, ' 127.0.0.1 ', 10008); //Connect server-side sockets
  5. while ($buffer = @socket_read ($socket, 1024x768, Php_normal_read)) {
  6. //The server tells the client that its status
  7. if (Preg_match ("/not connect/",$buffer)) {
  8. echo "Don t connect\n";
  9. Break ;
  10. } Else {
  11. //server came to information
  12. echo "Buffer Data:". $buffer.  "\ n";
  13. echo "Writing to socket\n";
  14. //write the customer's information to the channel and pass it to the server side
  15. if (!socket_write ($socket, "SOME data\n")) {
  16. echo "Write failed\n";
  17. }
  18. //The response information given by the server after receiving the message
  19. While ($buffer = Socket_read ($socket, 1024x768, Php_normal_read)) {
  20. echo "sent to Server:some data\n response from server is:". $buffer.  "\ n";
  21. }
  22. }
  23. }
  24. ?>

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

Socket interaction Process

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

[Email protected] 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

[Email protected] ~]$ netstat-an |grep LISTEN |grep 10008
TCP 0 0 127.0.0.1:10008 0.0.0.0:* LISTEN
[Email protected] ~]$

b, send the information to the cache

$buffer = "Connect"; See Code 2 above , run the socket client [[email protected] zhangy]#/usr/local/php/bin/php-a/home/zhangy/ Www/test2/client.phpinteractive mode Enabledbuffer data:connectwriting to Socketsent to Server:some Data response from S Erver was:information Received3, back to server [e-mail protected] zhangy]#/usr/local/php/bin/php-a/home/zhangy/www /test2/server.phpinteractive mode enabledsocket connectedsend data to Clientwrote to Socketbuffer:some DATAClosed the SOC Ket about purely personal understanding, if not, please teach the heroes.

PHP example shows the socket communication mechanism

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.