<?php
$port=4433;
//create socket
$sock=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create the socket\n");
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($sock));
exit;
}
socket_bind($sock,gethostbyname(gethostname()),$port) or die("Could not bind to socket\n");
echo "Server is ready!";
//listening to the client
socket_listen($sock,2);
echo "Waiting for clients.......<br>";
//accepting the request
$csock=socket_accept($sock);
echo "Client connected!";
//read the request
for($i=0;$i<5;$i++)
{
$req=socket_read($csock,4433);
echo $req;
$res="Thank you.....";
//send/write the message to client socket
socket_write($csock,$req);
}
//close the socket
socket_close($csock);
?>
该计划的输出:
服务器准备就绪!等待客户.......
客户已连接!(322034287458; M01; BOT; Z04; 8991101806187113329F; 0; 00000; GPS设备已启动;; g2 4( tX-9▒&amp;)x&gt;▒0▒+
h ## PuTTYPuTTY g2 4(tX。,)X`,▒0▒+
h ## PuTTYPuTTY g2 4(tX.9▒6)&lt;▒7▒0▒*
h ## PuTTYPuTTY g2 4(tX / :)▒@p0▒*
h ## [root @ ip-172-31-22-221 html]#PuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTY