PHP for tcp connection

不言
Release: 2023-03-23 14:28:01
Original
4528 people have browsed it

The content of this article is to share with you PHP for tcp connection, which has certain reference value. Friends in need can refer to


How to write native PHP.

$host = '服务端IP';    $port = 端口号;    $timeout = 5;    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);    if (socket_connect($socket, $host, $port) === false) { // 创建连接
        socket_close($socket);        $message = 'create socket error';        throw new Exception($message, socket_last_error());
    }   

    if (socket_write($socket, $buffer) === false) { // 发包
        socket_close($socket);        $message = sprintf("write socket error:%s", socket_strerror(socket_last_error()));        throw new Exception($message, socket_last_error());
    }   

    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);    $rspBuffer = socket_read($socket, 65536); // 接收回包

    socket_close($socket);
Copy after login
Copy after login

Use swoole writing method.

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);$ret = $client->connect('服务端IP', 端口号, 0.5, 0);  // 创建连接if (!$ret) {
    throw new Exception('connect error', $client->errCode);
}   

$client->send($buffer); // 发包$rspBuffer = $client->recv(); // 接收回包
Copy after login
Copy after login

Reprint address


https://www.liudon.org/1324.html

How to write native PHP.

$host = '服务端IP';    $port = 端口号;    $timeout = 5;    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);    if (socket_connect($socket, $host, $port) === false) { // 创建连接
        socket_close($socket);        $message = 'create socket error';        throw new Exception($message, socket_last_error());
    }   

    if (socket_write($socket, $buffer) === false) { // 发包
        socket_close($socket);        $message = sprintf("write socket error:%s", socket_strerror(socket_last_error()));        throw new Exception($message, socket_last_error());
    }   

    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);    $rspBuffer = socket_read($socket, 65536); // 接收回包

    socket_close($socket);
Copy after login
Copy after login

Use swoole writing method.

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);$ret = $client->connect('服务端IP', 端口号, 0.5, 0);  // 创建连接if (!$ret) {
    throw new Exception('connect error', $client->errCode);
}   

$client->send($buffer); // 发包$rspBuffer = $client->recv(); // 接收回包
Copy after login
Copy after login

Reprint address

https://www.liudon.org/1324.html

Related recommendations:

PHP reads CSV file data and generates CSV files


The above is the detailed content of PHP for tcp connection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template