This article mainly introduces the socket usage in php and describes the complete steps of socket communication in PHP, which is of reference value, for more information about socket usage in php, see the examples in this article. The usage is as follows:
1. enable socket
Phpinfo (); check whether socket extension is enabled; otherwise, it is enabled in php. ini.
II. server code writing
The code is as follows:
<? Php
Error_reporting (E_ALL );
Set_time_limit (0 );
// Ob_implicit_flush ();
$ Address = '1970. 0.0.1 ';
$ Port = 10005;
// Create a port
If ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) = false ){
Echo "socket_create () failed: reason:". socket_strerror (socket_last_error (). "\ n ";
}
// Bind
If (socket_bind ($ sock, $ address, $ port) === false ){
Echo "socket_bind () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
}
// Listen
If (socket_listen ($ sock, 5) === false ){
Echo "socket_bind () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
}
Do {
// Get a link
If ($ msgsock = socket_accept ($ sock) === false ){
Echo "socket_accepty () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
Break;
}
// Send welcome to the client
$ Msg = "server send: welcome
";
Socket_write ($ msgsock, $ msg, strlen ($ msg ));
Echo 'read client message \ n ';
$ Buf = socket_read ($ msgsock, 8192 );
$ Talkback = "received message: $ buf \ n ";
Echo $ talkback;
If (false === socket_write ($ msgsock, $ talkback, strlen ($ talkback ))){
Echo "socket_write () failed reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
} Else {
Echo 'send success ';
}
Socket_close ($ msgsock );
} While (true );
// Close the socket
Socket_close ($ sock );
?>
The server must be executed in cli mode. it is possible that the php. ini file is loaded differently in cli mode.
The output is as follows:
At this time, there is a tem. text file in the zhoxh directory. View Configuration File (php. ini) Path => C: \ WINDOWS. It is not my php. ini file, which indicates that the php. ini file called is incorrect. In this case, specify the php. ini file command as follows:
Note that environment variables are configured when php can be executed directly.
III. client
The code is as follows:
<? Php
// Error_reporting (E_ALL );
Echo "TCP/IP connection \ n ";
$ Service_port = 10005;
$ Address = '1970. 0.0.1 ';
$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
If ($ socket = false ){
Echo "socket_create () failed: reason:". socket_strerror (socket_last_error (). "\ n ";
} Else {
Echo "OK. \ n ";
}
Echo "Attempting to connect to '$ address 'on port' $ service_port '...";
$ Result = socket_connect ($ socket, $ address, $ service_port );
If ($ result = false ){
Echo "socket_connect () failed. \ nReason: ($ result)". socket_strerror (socket_last_error ($ socket). "\ n ";
} Else {
Echo "OK \ n ";
}
$ In = "HEAD/http/1.1 \ r \ n ";
$ In. = "HOST: localhost \ r \ n ";
$ In. = "Connection: close \ r \ n ";
$ Out = "";
Echo "sending http head request ...";
Socket_write ($ socket, $ in, strlen ($ in ));
Echo "OK \ n ";
Echo "Reading response: \ n ";
While ($ out = socket_read ($ socket, 8192 )){
Echo $ out;
}
Echo "closeing socket ..";
Socket_close ($ socket );
Echo "OK. \ n ";
The execution result is as follows:
Server:
Client:
I hope this article will help you with PHP programming.