This article brings you relevant knowledge about php socket. It mainly introduces what is socket? How does php socket realize client-server data transmission? Friends who are interested can take a look below. I hope it will be helpful to everyone.
Socket introduction
To achieve communication between network processes, almost all applications use sockets. Sockets are the intermediate communication between the application layer and the TCP/IP protocol family. Abstraction layer, which is a set of interfaces. In the design mode, socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the socket interface. For the user, a set of simple interfaces is all, allowing the socket to organize the data to comply with the specified Protocol
The original meaning of socket in English is "hole" or "socket". It is also commonly called "socket" and is used to describe IP addresses and ports. It is a The handle of the communication chain can be used to implement communication between different virtual machines or different computers.
Three processes of socket link
Server listening: IP port number
Client request: Send to service End IP and port connection request
Link confirmation: The server socket listens or receives the client socket connection request, and it will create a new process. Send the server's socket description to the client in response to the client's request. Once the client confirms this description, the connection is established. The server's socket continues to be in the listening state and continues to accept connection requests from other client sockets.
If you need to use socket in php, you need to add -- when compiling php enable-sockets
configuration item to enable, you can use the php -m|grep sockets
command to check the enabling status. For the specific compilation process, please refer to this article
Quick Experience
The simplified codes for the server and client are as follows. After running, the server will block and wait for the client to connect. The client will ask for input content on the console. After input, the information will be displayed in the service The client prints, and the client displays the content converted to uppercase. This example server and client run on the same server:
Server listening
<?php // 创建套接字 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 设置 ip 被释放后立即可使用 socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, true); // 绑定ip与端口 socket_bind($socket, 0, 8888); // 开始监听 socket_listen($socket); while (true) { // 接收内容 $conn_sock = socket_accept($socket); socket_getpeername($conn_sock, $ip, $port); // echo '请求ip: ' . $ip . PHP_EOL . '端口: ' . $port; while (true) { // 获取消息内容 $msg = socket_read($conn_sock, 10240); // TODO 处理业务逻辑 // 将信息转为大写并原样返回客户端 socket_write($conn_sock, strtoupper($msg)); echo $msg; } }
Client connection
<?php // 创建套接字 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 连接服务端 socket_connect($socket, '127.0.0.1', 8888); while (true) { // 让控制台输入内容 fwrite(STDOUT, '请输入内容:'); $in = fgets(STDIN); // 向服务端发送内容 socket_write($socket, $in); // 读取服务端发送的消息 $msg = socket_read($socket, 10240); echo $msg; }
Syntax explanation
socket_create
socket_create(int $domain,int $type, int $protocol): resource|false
Create and return a socket resource , also usually called a communication node. A typical socket consists of at least 2 sockets, one running on the client side and one running on the server side.
Parameters:
##domain Specifies what protocol the current socket uses. The available protocols are as follows:
Description | |
---|---|
IPv4 network protocol, both TCP and UDP are available Use this protocol | |
IPv6 network protocol, both TCP and UDP can use this protocol | |
Local communication protocol, IPC with high performance and low cost |
User specifies the current set Type used by the interface
Description | |
---|---|
Sequential, reliable, full-duplex, link-based byte stream, supporting data transfer flow control mechanism. The TCP protocol is based on this streaming socket. | |
Support for data messages (connectionless, unreliable, fixed maximum length) UDP protocol is based on this message socket | |
Sequential, reliable, full-duplex, connection-oriented, fixed maximum length data communication, the data end reads the entire data segment by receiving each data segment Packet | |
Read the original network protocol. This special socket can be used to manually build any type of protocol. Generally, this socket is used To implement ICMP request | |
Reliable data layer, but the arrival order is not guaranteed. General operating systems do not implement this function |