Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to understand the properties of TCP/UDP server on the server side

Shulou Source: shulou.com Published: 2022-06-01 11:07:20 10月04日 Update

This article will explain in detail how to understand the attributes of the TCP/UDP server in the server. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

$setting

The parameters set by the Server- > set () function are saved to the Server- > setting property. The value of the running parameter can be accessed in the callback function.

Swoole\ Server- > setting

Demo:

$server = new Swoole\ Server ('127.0.0.1, 9501); $server- > set ([' worker_num'= > 4,]); var_export ($server- > setting); echo PHP_EOL;//$server- > start (); / / the above code can be executed without start here

Run directly:

$master_pid

Returns the PID of the current server main process

Swoole\ Server- > master_pid: int

Demo:

$server = new Swoole\ Server ('127.0.0.1 server, 9501); $server- > set ([' worker_num'= > 4,]); var_export ($server- > setting); echo PHP_EOL;$server- > on ('start', function (Swoole\ Server $server) {/ / get the PID echo' master_pid:'.$server- > master_pid.PHP_EOL;} of the current server main process) $server- > on ('receive', function (Swoole\ Server $server, $fd, $reactor_id, $data) {/ / receive the client message, reply to the content, and close the connection $server- > send ($fd,' Swoole Server received your data:'.trim ($data). PHP_EOL); $server- > close ($fd);}); $server- > start ()

Start the service and connect using telnet, and then send the message "aaa" on the client:

$manager_pid

However, the PID of the current server management process

Swoole\ Server- > manager_pid: int

Can only be obtained after onStart and onWorkerStart

Demo:

$server = new Swoole\ Server ('127.0.0.1 server, 9501); $server- > on (' start', function (Swoole\ server $server) {/ / get the PID echo 'manager_pid:'.$server- > manager_pid.PHP_EOL;} of the current server management process) $server- > on ('receive', function (Swoole\ Server $server, $fd, $reactor_id, $data) {/ / receive the client message, reply to the content, and close the connection $server- > send ($fd,' Swoole Server received your data:'.trim ($data). PHP_EOL); $server- > close ($fd);}); $server- > start ()

As above, start the service and connect using telnet, and then send the message "aaa" on the client:

$worker_id

Get the current Worker process number, including the Task process.

Swoole\ Server- > worker_id: int

Demo:

$server = new Swoole\ Server ('127.0.0.1, 9501); $server- > set ([' worker_num'= > 4, 'task_worker_num'= > 4,]); $server- > on (' workerStart', function (Swoole\ Server $server, $workerId) {if ($server- > taskworker) {echo 'task workerId:'.$workerId; echo', taskworker _ id:'.$server- > worker_id.PHP_EOL;} else {echo 'workerId:'.$workerId Echo', worker_id:'.$server- > worker_id.PHP_EOL;}); / / must set onReceive event callback function $server- > on ('receive', function (Swoole\ Server $server, $fd, $reactor_id, $data) {}); / / must set onTask event callback function $server- > on (' task', function (Swoole\ Server $server, $task_id, $reactor_id, $data) {}); $server- > start ()

Start the server:

This Swoole\ Server- > worker_id attribute is the same as the $worker_id of onWorkerStart.

The Worker process number range is [0, $server- > setting ['worker_num']-1]

The Task process number range is: [$sever- > setting ['worker_num'], $server- > setting [' worker_num'] + $server- > setting ['task_worker_num']].

The value of worker_id remains unchanged after the Worker process is restarted

$worker_pid

Gets the operating system process ID of the current Worker process, the same as the return value of posix_getpid ().

Swoole\ Server- > worker_pid: int$taskworker

Whether the current process is a Task process

Swoole\ Server- > taskworker: bool

Return value: true indicates that the current process is a Task worker process, otherwise it is a Worker process

$connections

TCP connection iterator, you can use foreach to traverse all current connections to the server. The function of this property is the same as Server- > getClientList, but more friendly.

The traversal element is the fd of a single connection.

Swoole\ Server- > connections

The $connections property is an iterator object, not an PHP array, so it cannot be accessed with var_dump or array subscripts, but can only be looped through foreach.

Base mode

Boast processes are not supported to operate TCP connections in SWOOLE_BASE mode, so in BASE mode, the $connections iterator can only be used within the current process.

Demo:

Foreach ($server- > connections as $fd) {var_dump ($fd);} echo 'current server share' .count ($server- > connections). Connections to '.PHP _ EOL;$ports

An array of ports to listen for. If the server listens on multiple ports, you can traverse Server::$ports to get all Swoole\ Server\ Port objects.

Where swoole_server::$ports [0] is the master server port set by the constructor.

$ports = $server- > ports;$ports [0]-> set ($settings); $ports [1]-> on ('receive', function () {/ / onReceive event callback function}); ````so much about how to understand the attributes of the TCP/UDP server on the server. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Tags: Processes services servers properties contents functions clients clients arrays patterns ports iterations identical events information parameters objects articles more messages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Linux vpn Microsoft