2 php test files
Server. php
Copy codeThe Code is as follows: <? Php
// Phpinfo ();
// Ensure that no timeout occurs when you connect to the client
Set_time_limit (0 );
$ Ip = '1970. 0.0.1 ';
$ Port = 1935;
/*
+ -------------------------------
* @ Socket: entire communication process
+ -------------------------------
* @ Socket_create
* @ Socket_bind
* @ Socket_listen
* @ Socket_accept
* @ Socket_read
* @ Socket_write
* @ Socket_close
+ --------------------------------
*/
/* ---------------- The following operations are all in the manual -------------------*/
If ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) <0 ){
Echo "socket_create () failed because:". socket_strerror ($ sock). "\ n ";
}
If ($ ret = socket_bind ($ sock, $ ip, $ port) <0 ){
Echo "socket_bind () failed because:". socket_strerror ($ ret). "\ n ";
}
If ($ ret = socket_listen ($ sock, 4) <0 ){
Echo "socket_listen () failed because:". socket_strerror ($ ret). "\ n ";
}
$ Count = 0;
Do {
If ($ msgsock = socket_accept ($ sock) <0 ){
Echo "socket_accept () failed: reason:". socket_strerror ($ msgsock). "\ n ";
Break;
} Else {
// Send to client
$ Msg = "test successful! \ N ";
Socket_write ($ msgsock, $ msg, strlen ($ msg ));
Echo "test succeeded \ n ";
$ Buf = socket_read ($ msgsock, 8192 );
$ Talkback = "received message: $ buf \ n ";
Echo $ talkback;
If (++ $ count> = 5 ){
Break;
};
}
// Echo $ buf;
Socket_close ($ msgsock );
} While (true );
Socket_close ($ sock );
?>
Client. php
Copy codeThe Code is as follows: <? Php
Error_reporting (E_ALL );
Set_time_limit (0 );
Echo "$ Port = 1935;
$ Ip = "127.0.0.1 ";
/*
+ -------------------------------
* @ Socket connection process
+ -------------------------------
* @ Socket_create
* @ Socket_connect
* @ Socket_write
* @ Socket_read
* @ Socket_close
+ --------------------------------
*/
$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
If ($ socket <0 ){
Echo "socket_create () failed: reason:". socket_strerror ($ socket). "\ n ";
} Else {
Echo "OK. \ n ";
}
Echo "trying to connect '$ ip' port' $ port'... \ n ";
$ Result = socket_connect ($ socket, $ ip, $ port );
If ($ result <0 ){
Echo "socket_connect () failed. \ nReason: ($ result)". socket_strerror ($ result). "\ n ";
} Else {
Echo "Connect OK \ n ";
}
$ In = "Ho \ r \ n ";
$ In. = "first blood \ r \ n ";
$ Out = '';
If (! Socket_write ($ socket, $ in, strlen ($ in ))){
Echo "socket_write () failed: reason:". socket_strerror ($ socket). "\ n ";
} Else {
Echo "sent to server information! \ N ";
Echo "sent content: <font color = 'red'> $ in </font> <br> ";
}
While ($ out = socket_read ($ socket, 8192 )){
Echo "received server return message successful! \ N ";
Echo "received content:", $ out;
}
Echo "Disable SOCKET... \ n ";
Socket_close ($ socket );
Echo "Disable OK \ n ";
?>
Listen
#/Usr/local/php/bin/php/usr/local/apache2/htdocs/server. php
Request
#/Usr/local/php/bin/php/usr/local/apache2/htdocs/client. php
Socket is not enabled in PHP by default.
# Cd./ext/sockets/
#/Usr/local/php/bin/phpize
#./Configure -- enable-sockets -- with-php-config =/usr/local/php/bin/php-config
# Make
# Make install
Php. ini modify configuration
Add
Extension = sockets. so
Restart apache
#/Usr/local/apache2/bin/apachectl restart
==============================
Configuration in Windows
Modify php. ini
Extension = php_sockets.dll
Restart apache
2 under cmd
Php installation directory
C:/php/php.exe F: web/server. php
C:/php/php.exe F: web/client. php