Research on PHP SOCKET technology

Source: Internet
Author: User
Tags manual php and printf socket client
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 the fsockopen () to establish a socket connection, and then use the fputs () to send messages, with Fgets () to receive messages.

2. Establish a socket connection through socket_create (), and then send a message using Socket_send () or Socket_write () and send a message with SOCKET_RECV () or Socket_read ().

Strangely enough, I saw this passage in the manual. "This extension module is experimental. The behavior of the module, including the name of its function and any other documentation about the module, may be changed without notice in the future after the release of PHP. We remind you to use this extension module at your own risk. "It seems that php4.0 socket communication is not completely stable.

Today, I wrote the client to do with the server to do two communication, I used the above method has written a client program, found that when only one communication, that is, the PHP client sent a message, and then received a return message, shut down the connection. Both of these methods are able to implement functions correctly and quickly, but as a two communication, there is a clear difference, the first way the first communication is very fast, which I can see through the output of the service side, but the second communication to wait for several minutes to finish, I tried several times this, I'm not quite sure where my program went wrong, or whether the connection is a problem, but the second way to do this two communication is very fast, right! Done very.

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

//////////////////////////////File Description//////////////////////////////////////////


//Class name:socket


version:v1.0


//Functional outline:create Socket,and Send message to server


//Revision HISTORY:2004/12/15 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);


 }

}







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.