Home >Backend Development >PHP Tutorial >PHP SOCKET Technology Research_PHP Tutorial
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() , then use fputs() to send messages and 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.
Very 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 be changed without notice The situation will change with subsequent releases of PHP. We remind you to use this extension module at your own risk. "It seems that php4.0 socket communication is not yet completely stable.
Today, the client I wrote 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, the PHP client sent it once. message, then receive the return 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 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) . "
";
} else {
echo "connect OK.
";
}
}
}
function sendmsg($msg){
socket_write($this->socket,$ msg,strlen($msg));
$result = socket_read($this->socket,100);
$this->response = $result;
if($this-> debug == 1){
printf("%s $msg
",$this->sendflag);
printf("< font color=blue>
socket_close($this->socket);
}
}
www.bkjia.com