Home >Backend Development >PHP Tutorial >PHP technology advancement PHP SOCKET technology research

PHP technology advancement PHP SOCKET technology research

黄舟
黄舟Original
2016-12-16 13:26:381492browse

Today I tried to write a program that communicates between PHP and C language through socket. After reading the PHP manual, I found that there are several ways to establish a socket client.

1. Establish a socket connection through fsockopen(), and then use fputs( ) to send messages and use fgets() to receive messages.

2. Establish a socket connection through socket_create(), then use socket_send() or socket_write() to send messages, and use socket_recv() or socket_read() to send messages.

It’s strange, I saw this paragraph in the manual: "This extension module is experimental. The behavior of this module, including the names of its functions and any other documentation about this module may change without notice. PHP will change in future releases. We remind you to use this extension module at your own risk. "It seems that php4.0 socket communication is not yet completely stable.

The client I wrote today needs to communicate with the server twice. I used the above method to write a client program. I found that when there was only one communication, that is, the PHP client sent a message once and then received it. Return the message and close the connection. Both methods can implement functions correctly and quickly, but there are obvious differences when communicating twice. The first communication with the first method ends very quickly. I can see this through the output of the server, but It took several minutes for the second communication to end. I tried it several times and it still happened. I’m not sure what went wrong with my program, or there was something wrong with the connection this way. However, the second method failed to complete these two communications. Quick and correct! Very well done.

Finally, I wrote a class based on the second situation

////////////////////////////// File Description //////////////////////////////////////////
//  Class Name            : socket
//  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 句柄
 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 content of PHP technology advanced PHP SOCKET technology research. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn