PHP Socket Programming starts

Source: Internet
Author: User
Keywords PHP Socket Programming starts

Let's start with a simple example---a TCP service that receives the input string, processes and returns the string to the client. The following is the corresponding code:

PHP Code:

// 设置一些基本的变量
$host="192.168.1.99";
$port=1234;
// 设置超时时间
set_time_limit(0);
// 创建一个Socket
$socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create
socket\n");
//绑定Socket到端口
$result=socket_bind($socket,$host,$port) or die("Could not bind to
socket\n");
// 开始监听链接
$result=socket_listen($socket,3) or die("Could not set up socket
listener\n");
// accept incoming connections
// 另一个Socket来处理通信
$spawn=socket_accept($socket) or die("Could not accept incoming
connection\n");
// 获得客户端的输入
$input=socket_read($spawn,1024) or die("Could not read input\n");
// 清空输入字符串
$input=trim($input);
//处理客户端输入并返回结果
$output=strrev($input) ."\n";
socket_write($spawn,$output,strlen($output)) or die("Could not write
output\n");
// 关闭sockets
socket_close($spawn);
socket_close($socket);
?>
The following is a detailed description of each of these steps:

1. The first step is to create two variables to hold the IP address and port of the server on which the socket is running. You can set up your own server and port (this port can be a number from 1 to 65535), provided that the port is not in use.

PHP Code: // 设置两个变量
$host="192.168.1.99";
$port=1234;
?>
2. The Set_time_out () function can be used on the server side to ensure that PHP does not time out while waiting for the client to connect.

PHP Code: // 超时时间
set_time_limit(0);
?>
3. On the previous basis, it is now time to use the Socket_creat () function to create a socket---This function returns a socket handle that will be used in all subsequent functions.

PHP Code: // 创建Socket
$socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n");
?>
The first parameter "Af_inet" is used to specify a domain name;
The second argument "Sock_strem" tells the function what type of socket to create (in this case TCP type)

So, if you want to create a UDP socket, you can use the following code:

PHP Code: // 创建 socket
$socket=socket_create(AF_INET,SOCK_DGRAM,0) or die("Could not create socket\n");
?>
4. Once a socket handle is created, the next step is to specify or bind it to the specified address and port. This can be done through the Socket_bind () function.

PHP Code: // 绑定 socket to 指定地址和端口
$result=socket_bind($socket,$host,$port) or die("Could not bind to socket\n");
?>
5. When the socket is created and bound to a port, you can start listening for external connections. PHP allows you to start a listener by the Socket_listen () function, and you can specify a number (in this case, the second parameter: 3)

PHP Code: // 开始监听连接
$result=socket_listen($socket,3) or die("Could not set up socket listener\n");
?>
6. Until now, your server has done nothing but wait for a connection request from the client. Once a client connection is received, the socket_assept () function starts to work, it receives the connection request and calls another child socket to process the client-server information .

PHP Code: //接受请求链接
// 调用子socket 处理信息
$spawn=socket_accept($socket) or die("Could not accept incoming connection\n");
?>
This sub-socket can now be used by the subsequent client-server communication.

7. When a connection is established, the server waits for the client to send some input information, which can be obtained by the Socket_read () function and assign it to the PHP $input variable.

PHP Code: // 读取客户端输入
$input=socket_read($spawn,1024) or die("Could not read input\n");
?>
The first parameter of the Socker_read specifies the number of bytes to read, which you can use to limit the size of the data that is fetched from the client.

Note: The Socket_read function will always read the shell data until it encounters the \n,\t or the/s character. The PHP script considers this character to be the input terminator.

8. The server must now handle these data sent by the client (in this case the processing only contains data input and is uploaded to the client). This can be done by the Socket_write () function (which makes it possible to send a data stream back to the client by a communication socket)

PHP Code: // 处理客户端输入并返回数据
$output=strrev($input) ."\n";
socket_write($spawn,$output,strlen($output)) or die("Could not write output\n");
?>
9. Once the output is returned to the client, the parent/child socket should be terminated by the Socket_close () function

PHP Code: // 关闭 sockets
socket_close($spawn);
socket_close($socket);
?>
  • 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.