时间:2021-07-01 10:21:17 帮助过:5人阅读
error_reporting(E_ALL);
set_time_limit(0);
echo "TCP/IP Connection
\n";
$port = 6000;
$ip = "127.0.0.1";
$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 "试图连接 '$ip' 端口 '$port'...\n";
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
}else {
echo "连接OK\n";
}
$in = 'r';
$out ='';
if(!socket_write($socket,$in,strlen($in))) {
echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";
}else {
echo "发送到服务器信息成功!\n";
echo "发送的内容为:$in
";
}
while($out = socket_read($socket,8192)) {
//echo "接收服务器回传信息成功!\n";
//$obj=json_decode($out);
//echo "接受的内容为:",$out;
echo $out;
//echo $obj->0;
}
/*
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
echo 'aa'.$buf.'aa';
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . " ";
}
*/
echo "关闭SOCKET...\n";
socket_close($socket);
echo "关闭OK\n";
?>