The English literal of the socket is "hole" or "socket". As the BSD UNIX process communication mechanism, take the latter one meaning. Often also referred to as a "socket," which describes IP addresses and ports, is a handle to a communication chain that can be used to communicate between different virtual machines or different computers. Hosts on the internet typically run multiple service software, while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services. The socket is like a porous socket, as its English intended. A host is like a room full of various sockets, each outlet has a number, some sockets provide 220 vac, some provide 110 volts AC, some provide cable TV programs. Customer software plug into different numbered sockets, you can get different services. This article mainly introduces the use of sockets in PHP, in detail on the PHP socket communication complete steps, very reference value, the need for friends can refer to, The specific usage is as follows:
First, open the socket
Phpinfo (); Check to see if the socket extension is turned on, otherwise open in php.ini.
Second, the server-side code of the wording
The code is as follows:
<?phperror_reporting (E_all); set_time_limit (0);//ob_implicit_flush (); $address = ' 127.0.0.1 '; $port = 10005;// Create 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 }//welcome sent to client $msg = "<font color= ' red ' >server send:welcome</font><br/>"; 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 Socketsocket_close ($sock);? >
Server side needs to be executed in CLI mode, it is possible that the CLI mode php.ini file is loaded differently
Can be output like this:
In this case there is a tem.text file in the Zhoxh directory. View Configuration File (php.ini) Path = = C:\WINDOWS. Not my php.ini file, which indicates that the php.ini file was called incorrectly. At this point we will specify the php.ini file command as follows
Note that my PHP can be executed directly when the environment variable is configured.
Third, the client
The code is as follows:
<?php//error_reporting (E_all); echo "<H2>TCP/IP connection
The results of the implementation are as follows:
Server
Client