One, server.php service end:
<?php error_reporting (E_all);
Set_time_limit (0);
Ob_implicit_flush ();
Local IP $address = ' localhost ';
Set up 111 ports for communication $port = 111; Create a socket if (($sock = Socket_create (Af_inet, Sock_stream, sol_tcp)) < 0) {echo "Socket creation failure Reason". Socket_strerror ($sock).
"\ n"; } if ($ret = Socket_bind ($sock, $address, $port)) < 0) {echo creates socket failure reason. Socket_strerror ($ret).
"\ n"; //Listen for if ($ret = Socket_listen ($sock, 5)) < 0) {echo "Monitor failure Reason". Socket_strerror ($ret).
"\ n"; Do {//Receive command if ($msgsock = @socket_accept ($sock)) < 0) {echo command receives failure reason: ". Socket_strerror ($msgsock).
"\ n";
Break $msg = "\nphp Test Server. \ n "." Use Quit,shutdown,sun ...
such as command test. \ n ";
@socket_write ($msgsock, $msg, strlen ($msg)); do {if ($buf = @socket_read ($msgsock, 2048, Php_normal_read))) {echo "Socket_read () Failed:reason:". Socket_strerror ($ret).
"\ n";
Break 2;
} if (! $buf = Trim ($buf)) {continue;
} if ($buf = = ' quit ') {break; } if ($buf= = ' shutdown ') {socket_close ($msgsock);
Break 2;
if ($buf = = ' Sun ') {echo ' What are you doing? ';
$talkback = "backinformation: ' $buf '. \ n";
Socket_write ($msgsock, $talkback, strlen ($talkback));
echo "$BUF \ n";
} while (true);
Socket_close ($msgsock);
} while (true); Socket_close ($sock);?>
Second, client.php client:
<?php
error_reporting (e_all);
Port
$service _port =;
Local
$address = ' localhost ';
Create a TCP/IP socket
$socket = socket_create (Af_inet, Sock_stream, sol_tcp);
if ($socket < 0) {
echo Socket creation failure Reason:. Socket_strerror ($socket). "\ n";
} else {
echo "Ok,he he.\n";
}
$result = Socket_connect ($socket, $address, $service _port);
if ($result < 0) {
echo Socket connection failure Reason: ($result). Socket_strerror ($result). "\ n";
} else {
echo "ok.\n";
}
Send command
$in = "head/http/1.1\r\n";
$in. = "connection:close\r\n\r\n";
$out = ';
echo "Send Command ... ...";
$in = "sun\n";
Socket_write ($socket, $in, strlen ($in));
echo "ok.\n";
echo "Reading backinformatin:\n\n";
while ($out = Socket_read ($socket, 2048)) {
echo $out;
}
echo "close socket ...";
Socket_close ($socket);
echo "Ok,he he.\n\n";
? >
These two pieces of code is a simple introduction to use PHP to implement client and server communication
client.php
server.php
The specific operation, in DOS under the PHP command to open the server, in the listening state.
Then open a DOS window, with the PHP command to open the client, will get the service side of the response ....
#Php