Home > Backend Development > PHP Tutorial > PHP+Socket series realizes data transmission between client and server

PHP+Socket series realizes data transmission between client and server

藏色散人
Release: 2023-04-11 10:18:01
forward
5000 people have browsed it

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

PHP+Socket series realizes data transmission between client and server

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.

PHP+Socket series realizes data transmission between client and server

php implements socket

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 &#39;请求ip: &#39; . $ip . PHP_EOL . &#39;端口: &#39; . $port;

    while (true) {
        // 获取消息内容
        $msg = socket_read($conn_sock, 10240);
        // TODO 处理业务逻辑

        // 将信息转为大写并原样返回客户端
        socket_write($conn_sock, strtoupper($msg));

        echo $msg;
    }
}
Copy after login

Client connection

<?php

// 创建套接字
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// 连接服务端
socket_connect($socket, &#39;127.0.0.1&#39;, 8888);

while (true) {
    // 让控制台输入内容
    fwrite(STDOUT, &#39;请输入内容:&#39;);
    $in = fgets(STDIN);

    // 向服务端发送内容
    socket_write($socket, $in);

    // 读取服务端发送的消息
    $msg = socket_read($socket, 10240);
    echo $msg;
}
Copy after login

Syntax explanation

socket_create

socket_create(int $domain,int $type, int $protocol): resource|false
Copy after login

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: