我已经收集了一些将 TCP 客户端连接到服务器并更改了 IP 的示例,但我真的不知道我的代码中的错误是什么,因为它应该可以工作并且服务器也从 Gta San Andrease 多人脚本控制台在线,服务器位于我的另一台电脑的 localhost 上,IP 为 192.168.0.11 ,服务器端口为 7777 ,这是来自我的 PHP 网站的客户端代码:
<?php
// form submitted
// where is the socket server?
$host="192.168.0.11";
$port = 7777;
// open a client connection
echo "1";
$fp = fsockopen ($host, $port, $errno, $timeout);
echo "2";
if (!$fp){
echo "3";
$result = "Error: could not open socket connection";
}
else
{
echo "4";
// get the welcome message
fgets ($fp, 7777);
// write the user string to the socket
fputs ($fp, $message);
// get the result
$result .= fgets ($fp, 7777);
// close the connection
fputs ($fp, "END");
fclose ($fp);
// trim the result and remove the starting ?
$result = trim($result);
$result = substr($result, 2);
// now print it to the browser
}
?>
Server said: <b><? echo $result; ?></b>
<?
?>
这是警告/错误:
警告:fsockopen(): unable to connect to 192.168.0.11:7777(连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者连接的主机没有响应,建立连接失败。)