PHP Technology Advanced PHP SOCKET technology Research

Source: Internet
Author: User
Today try to write a PHP and C language through the socket communication program, read the PHP manual, found that there are several ways to establish a socket client.

1, through Fsockopen () to establish a socket connection, and then use Fputs () to send messages, with Fgets () to receive messages.

2. Establish a socket connection via Socket_create (), then send a message with socket_send () or Socket_write () and send a message with SOCKET_RECV () or Socket_read ().

Strangely enough, I saw this passage in the handbook. "This extension is experimental. The behavior of the module, including the name of its function and any other documentation about the module, may change with subsequent releases of PHP without notice. We remind you that you are at your own risk while using this extension module. "It seems that the php4.0 socket communication is not completely stable.

Today I wrote the client to do two communication with the server, I use the above method has written a client program, found that when only one communication, that is, the PHP client sends a message, and then receive the return message, then close the connection. Both of these methods can be implemented correctly and quickly, but as two communication, but there are obvious differences, the first method of communication is particularly fast end, this I can see through the output of the server, but the second communication to wait for several minutes to end, I tried several times this, I'm not sure where my program went wrong, or the way the connection is problematic, but the second way to do this two communication is fast, correct! accomplished very much.

Finally, I wrote a class based on the second case.

File Description////////////////////////////////////////////Class name:sock       et//version:v1.0//functional outline:create socket,and Send message to server//Revision history : 2004/12/15 first version created//CURRENT:2004/12/15 Liu yongsheng////////////////////////////// Class socket{var $socket;//socket handle var $sendflag = " >>> "; var $recvflag = "<<<"; var $response; var $debug = 1;  function socket ($hostname, $port) {$address = gethostbyname ($hostname);     $this->socket = socket_create (AF_INET,SOCK_STREAM,SOL_TCP);  $result = Socket_connect ($this->socket, $address, $port); if ($this->debug = = 1) {if ($result < 0) {echo "Socket_connect () Failed.\nreason: ($result)". Socket_strerror ($result).   "<br>";   } else{echo "Connect ok.<br>"; }}} function sendmsg ($msg) {socket_Write ($this->socket, $msg, strlen ($msg));  $result = Socket_read ($this->socket,100);  $this->response = $result;      if ($this->debug = = 1) {printf ("<font color= #CCCCCC >%s $msg </fon><br>", $this->sendflag);  printf ("<font color=blue>%s $result </font><br>", $this->recvflag); } return $result; } function Close () {socket_close ($this->socket);}}

The above is the PHP technology advanced PHP SOCKET technology research content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.