Home  >  Article  >  Backend Development  >  How to connect socket socket in PHP

How to connect socket socket in PHP

little bottle
little bottleOriginal
2019-04-22 11:14:282518browse

Socket is usually also called "socket", which is used to describe the IP address and port. It is the handle of a communication chain. Applications usually make requests to the server or respond to network requests through "sockets". This article mainly talks about the connection process between socket characters in PHP, which has certain reference value. Interested friends can learn about it.

Depending on how the link is started and the target to which the local socket is connected, the connection process between sockets can be divided into three steps:

1. Server monitoring: The server-side socket does not locate the specific client socket, but is in a state of waiting for connection and monitors the network status in real time.

2. Client request: refers to a connection request made by the client's socket, and the target to be connected is the server's socket. To do this, the client's socket must first familiarize itself with the server's socket to which it wants to connect, indicate the address and port number of the server's socket, and then make a connection request just like the server's socket.

3. Connection confirmation: means that when the server-side socket listens or receives the connection request of the client socket, it responds to the request of the client socket. , create a new thread and send the description of the server-side socket to the client. Once the client confirms this description, the connection is established. The server-side socket continues to be in the listening state and continues to receive link requests from other client sockets.

1. Test environment:

Server ip: xxx.xxx.xxx.1

# # Client ip: xxx.xxx.xxx.2

2. Test process:

The client server will be on this machine (xxx .xxx.xxx.1) Send data to the socket server of the server (xxx.xxx.xxx.2) through socket. After receiving the data sent by the client, the server returns some data to the client.

3. Server file content: 

<?php
  //获取tcp协议号吗.
  $tcp = getprotobyname("tcp");
  //建立server端socket , 创建并返回一个套接字,也称做一个通讯节点.一个典型的网络连接由2个套接字构成 , 一个运行在客户端 , 另一个运行在服务器端.
  $socket = socket_create(AF_INFT , SOCK_STREAM , $tcp);
  //绑定要监听的ip和端口 , 这里绑定的ip一定要写局域网ip , 写成127.0.0.1客户端将无法与服务器端建议连接.
  socket_bind($socket , &#39;xxx.xxx.xxx.1&#39; , 10008);
  //监听端口
  socket_listen($socket);
  //初始化一个数据 , 和客户端通信
  $buffer = "connect";
  while(true){
    //接受客户端请求过来的yigesocket连接
    $connection = socket-accept($scoket);
    if(!connection){
      echo "connect faild";
    } else {
    echo "Socket connected \n";
    //向客户传递一个信息数据
   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";
}   
  //从客户端获取得的数据
  while($data = $socket_read($connection , 1024 , PHP_NORMAL_READ)){
  printf("Buffer:".$data.&#39;\n&#39;);
  //取得信息给客户端一个反馈 ,Thank you client , you data is Received success发给客户端的回应信息.
  socket-wirte($coennection , "Thank you client , you data is Received success \n");
}
}
//关闭sockket
socket_close($connection);
printf("Closed the socket\n");
  
  }
?>

4. Client file content:

<?php
  //建立客户端的socket连接
  $socket = se);
  //连接服务器端socket
  $connection = socket_connect($socket , &#39;xxx.xxx.xxx.1&#39; , 10008);
  //要求发送到服务端的信息.
  $send_data = "This data will Send to server!";
  //客户端去连接服务端并接受服务端返回的数据 , 如果返回的数据保护not connect就提示不能连接.
  while($buffer = @socket_read($socket , 1024,PHP_NORMAL_READ)){
    if(preg_match("/not connect/" , $buffer)){
    echo "don&#39;t connect\n";
    break;
  } else {
  //服务端传来的信息
  echo "Buffer Data: ".$buffer .&#39;\n&#39;;
  echo "Writing to Socket\n";
  //将客户的信息写道通道中 , 传给服务器端
  if(!socket_write($socket , "$send_data\n")){
     echo "Write failed\n";
  }
  //服务器端收到信息后 , 客户端接受服务端传给客户端的回应信息.
  while($buffer = socket_read($socket , 1024 , PHP_NORMAL_READ)){
    echo "send to server: $send-data\n response from server was:".$buffer."\n";
  }
  }
  }
?>

5. Start the process on the server socket service.

#/usr/local/php/bin/php -a/home/server.php
Interactive mode enable 互动模式启动

6. After the server is started, check the started process and port

#netstat -tnlp |grep 10008
tcp   0  0 192.168.13:10008  0.0.0.0:*  LISTEN
28892/php

7. Perform transmission on the client (192.168.1.2)

#/usr/local/php/bin/php -a client.php
Interactive mode enabled

8. Return to the server to view acceptance information arrived.

#/usr/local/php/bin/php -a /home/server.php
Interactive mode enabled
 
Socket connected
send data client
Wrote to socket
Buffer:This data will Send to server!

Related tutorials:

PHP video tutorial

The above is the detailed content of How to connect socket socket in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn