This article to everyone to share the content is PHP TCP connection, has a certain reference value, the need for friends can refer to
The native PHP notation.
$host = ' server IP '; $port = port number; $timeout = 5; $socket = Socket_create (Af_inet, Sock_stream, sol_tcp); if (Socket_connect ($socket, $host, $port) = = = False) {//Create connection socket_close ($socket); $message = ' Create socket error '; throw new Exception ($message, Socket_last_error ()); } if (Socket_write ($socket, $buffer) = = = False) {//bundle 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); Pick-up Package socket_close ($socket);
Use the swoole notation.
$client = new Swoole_client (swoole_sock_tcp, swoole_sock_sync); $ret = $client->connect (' Server IP ', port number, 0.5, 0); Create a connection if (! $ret) { throw new Exception (' Connect error ', $client->errcode);} $client->send ($buffer); Contract $rspbuffer = $client->recv (); Pick up the package
Reprint Address
Https://www.liudon.org/1324.html
The native PHP notation.
$host = ' server IP '; $port = port number; $timeout = 5; $socket = Socket_create (Af_inet, Sock_stream, sol_tcp); if (Socket_connect ($socket, $host, $port) = = = False) {//Create connection socket_close ($socket); $message = ' Create socket error '; throw new Exception ($message, Socket_last_error ()); } if (Socket_write ($socket, $buffer) = = = False) {//bundle 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); Pick-up Package socket_close ($socket);
Use the swoole notation.
$client = new Swoole_client (swoole_sock_tcp, swoole_sock_sync); $ret = $client->connect (' Server IP ', port number, 0.5, 0); Create a connection if (! $ret) { throw new Exception (' Connect error ', $client->errcode);} $client->send ($buffer); Contract $rspbuffer = $client->recv (); Pick up the package
Reprint Address
Https://www.liudon.org/1324.html