Php-socket Server client Send receive communication instance detailed

Source: Internet
Author: User

Socket INTRODUCTION

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.

Hosts on the internet typically run multiple service software, while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services.

Socket connection Process

Depending on how the connection is started and the destination to which the local socket is connected, the connection between sockets can be divided into three steps: Server listening, client request, connection acknowledgement.

(1) Server monitoring: Is the server end socket does not locate the specific client socket, but in the status of waiting for the connection, real-time monitoring network status.

(2) Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

(3) Connection confirmation: When the server-side socket is heard or received a client socket connection request, it responds to the client socket request, set up a new thread, the server-side socket description to the client, once the client confirms the description, the connection is established. While the server-side socket continues listening, it continues to receive connection requests from other client sockets

Socket Encyclopedia: http://baike.baidu.com/view/13870.htm


1. Test environment :

Server ip:192.168.150.1

Client ip:192.168.150.2

2. Test process:

The client server will return some information to the client after it receives the data sent by the client on the socket server (192.168.150.2) that sends the data to the service side (192.168.150.2) through the socket.

3, the service side file content:

#server. php<?php //gets the TCP protocol number. $tcp  = getprotobyname ("TCP");  //  establishes the server-side socket , creates and returns a socket, also known as a communication node. A typical network connection consists of a  2  socket, one running on the client and the other running on the server side. $socket  = socket_create (af_inet, sock_stream,  $tcp);  //bind the IP and port to listen to, the IP bound here must write LAN IP, Written 127.0.0.1 the client will not be able to connect with the server. Socket_bind ($socket,  ' 192.168.150.1 ',  10008);      //Listening Port      socket_listen ($socket);       //Initializes a data, and client communication   $buffer  =  " Connect "; while  (true)  {     //  accepts a client request over a socket connection        $connection  = socket_accept ($socket);      if (! $connection ) {         echo  "Connect faild";      }else{             echo  " socket connected\n ";              //  passing an information data to the client               if  ($buffer  !=  "")  {                  echo   "send data to client\n";                  socket_write ($connection,  $buffer  .  "\ n");                  echo  " Wrote to socket\n ";              }  else {                  echo  "no data in the buffer\n"  ;             &nbSP;}              //  data obtained from the client              while  ($data  =  @socket_read ($ Connection, 1024, php_normal_read))  {                      printf ("Buffer: "  . $ data .  "\ n");                 //get information to the client for a feedback,  thank you client, you data is  received  success The response information sent to the client.                      socket_write ($connection,  "thank you client, you data is   Received success\n ");              }      }     //Close  socket    socket_close ($ connection);      printf ("closed the socket\n"); } ?>

4, client file content:client.php<?php //  establish client Socet connection   $socket  = socket_create (af_inet,  SOCK_STREAM,&NBSP;SOL_TCP)    //Connection server-side socket  $connection  = socket_connect ($ socket,  ' 192.168.150.1 ',  10008);   //the information to be sent to the server. $send _data =  "this data will send to server!"; /client to connect to the server and accept the data returned by the server, if the returned data protection Not connect will not be able to connect. while  ($buffer  =  @socket_read ($socket,  1024, php_normal_read)  {      if  (Preg_match ("/not connect/", $buffer))  {          echo  "Don ' t connect\n";          break;     } else {          //information          echo  "buffer data: " from the service side  .  $buffer  .  "\ n";          echo  "writing to socket\n";          //  Write the customer's information to the channel and pass it to the server side           if  (!socket_write ($socket,  "$send _data\n"))  {              echo  "write failed\n";         After the  }         //server receives the information, the client receives the service-side response message to the client.          while  ($buffer  = socket_read ($socket,  1024, php_normal_read))  {                  echo  "Sent to server: $send _data\n response from  server was: " .  $buffer  . " \ n ";          }      &nBsp;      } }  ?> 

5, the service-side socket service startup process.

#/usr/local/php/bin/php-a/home/server.php

Interactive mode Enabled

6, after the server started to view the startup process and port.

#netstat-TNLP |grep 10008
TCP 0 0 192.168.150.13:10008 0.0.0.0:* LISTEN 28892/php


7. Perform the transfer on the client (192.168.1.2)

#/usr/local/php/bin/php-a client.php

Interactive mode Enabled

Buffer Data: Connect

Writing to Socket

Sent to server: Thisdata would Send to server!

Response from server is:Thank you client, your data information Received


8, back to the server to view the received information.

#/usr/local/php/bin/php-a/home/server.php

Interactive mode Enabled

Socket connected
Send data to Client
wrote to socket
Buffer: This data would Send to server!


9. References

The Socket_create () function requires three parameters: one protocol, one socket type, and one public protocol. The Socket_create () function successfully returns a resource type that contains the socket and returns false if it does not succeed.

Socket function
Function Name Description
Socket_accept () accepts a socket connection
Socket_bind () binds the socket to an IP address and port
Socket_close () Close a socket resource
Socket_connect () Start a socket connection
Socket_create () produces a socket equivalent to a data structure that produces a socket
Socket_listen () listens for all connections by the specified socket
Socket_read () reads the specified length of data
SOCKET_READV () reads data from a scatter/aggregate array
Socket_send () This function sends the data to the connected socket
Socket_write () write data to the socket cache
Socket_writev () write data to a scatter/aggregate array


Reference Document: Http://blog.51yip.com/php/673.html

This article is from the "Zhang Yupo" blog, make sure to keep this source http://fighter.blog.51cto.com/1318618/1533957

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.