PHP for TCP connection

Source: Internet
Author: User
Tags socket error sprintf
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.