Today, I tried to write a PHP program that communicates with the C language through socket.ProgramI have read the PHP manual and found that there are several ways to create a Socket Client.
1. Establish a socket connection through fsockopen (), send messages with fputs (), and receive messages with fgets.
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 is strange that I have seen such a passage in the manual. "This extension module is experimental. The behavior of this module, including its function name and any other documents about this module, may change with PHP release without notice. We remind you to use this extension module at your own risk. "It seems that php4.0 socket communication is not completely stable.
The client I wrote today needs to communicate with the server twice. I used the above method to write a client program and found that when only one communication occurs, that is, when the PHP client sends a message and receives the returned message, the connection is closed. Both methods can implement functions correctly and quickly, but there is a significant difference between the two communication methods. The first communication method is very fast and ends, I can see this through the output from the server, but it takes several minutes for the second communication to end. I tried it several times. I don't know where my program went wrong, there is still a problem with this method of connection, but the second method is very fast and correct to do the two communications! The process is very complete.
Finally, I wrote a class according to 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 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 );
}
}