Today, I tried to write a program for communication between PHP and C language through socket. I have read the PHP manual and found that there are several ways to establish 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 (), and then use socket_send () to write a program for communication between PHP and C language through socket today. I have read the PHP Manual, 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 )."
";
} 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 ("% s $ result
", $ This-> recvflag );
}
Return $ result;
}
Function close (){
Socket_close ($ this-> socket );
}
}