我将我的网站连接到具有VB6应用程序的服务器。我使用PHP连接到指定端口上的IP。我的代码如下:
public static function hello()
{
static::$version = Config::get('socket.version');
static::$user = Config::get('socket.user');
static::$pwd = Config::get('socket.password');
$xmlstr = '<?xml version="1.0"?>';
$xmlstr .= '<HelloRequest version="' . static::$version . '" user="' . static::$user . '" pwd="' . static::$pwd . '" />';
//$xmlstr = '|'.strlen($xmlstr).'|'.$xmlstr; // Appending Length
return static::runRequest($xmlstr);
}
并在同一个类中调用runRequest($ xmlstr)方法:
public static function runRequest($request)
{
static::$address = Config::get('socket.address');
static::$port = Config::get('socket.port');
static::$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!static::$socket) {
return (object) array(
'error' => true,
'message' => "socket_create() failed: reason: " . socket_strerror(socket_last_error())
);
}
socket_set_option(static::$socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 5, 'usec' => 0));
$result = socket_connect(static::$socket, static::$address, static::$port);
if (!$result) {
static::close();
return (object) array(
'error' => true,
'message' => "socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error(static::$socket))
);
}
$bytes = socket_send(static::$socket, $request, strlen($request), 0);
if ($bytes > 0) {
$buf = " ";
$totalBytes = 0;
$outbuf = "";
$maxlen = 4096;
$loopcnt = 0;
$maxloops = 100;
while (true) {
$receivedBytes = socket_recv(static::$socket, $buf, $maxlen, 0);
if ($receivedBytes > 0) {
$buf = str_replace("&", "&", $buf);
$outbuf .= $buf;
$totalBytes += $receivedBytes;
if(strrpos($outbuf, "\r\n") == true)
break;
}
$loopcnt++;
}
$outbuf = str_replace("\r\n", "", $outbuf);
socket_close(static::$socket);
return (object) array(
'error' => false,
'message' => $outbuf
);
}
socket_close(static::$socket);
return (object) array(
'error' => true,
'message' => 'Something went wrong. Please try again.'
);
}
问题在于代码是否存在: $ receivedBytes = socket_recv(static :: $ socket,$ buf,$ maxlen,0);
并在页面标题上返回500内部服务器错误并在页面上返回请求超时:
请求超时
此请求处理时间过长,服务器超时。如果不应超时,请与本网站的管理员联系以增加“连接超时”。
任何线索都可能出错。用户名/密码,IP和端口是正确的,使用putty进行测试。
我们正在使用litespeed服务器(不是apache)。以下是该事件的日志:
2015-06-15 17:46:35.201 [INFO] [ip.address.removed:62539:HTTP2-1]连接空闲时间:121状态:6观看事件:25,关闭! 2015-06-15 17:46:35.201 [NOTICE] [ip.address.removed:62539:HTTP2-1]内容len:0,请求行:&#39; GET / socket / hello HTTP / 1.1&#39; 2015-06-15 17:46:35.201 [NOTICE] [ip.address.removed:62539:HTTP2-1]重定向:#1,URL:/index.php/socket/hello 2015-06-15 17:46:35.201 [INFO] [ip.address.removed:62539:HTTP2-1] HttpExtConnector状态:8,请求体发送:0,响应体大小:-2,响应体发送:0,留在缓冲区:0,尝试:0。 2015-06-15 17:46:35.201 [INFO] [ip.address.removed:62539:HTTP2-1] Lsapi连接状态:3,观看事件:25,respState:1,m_iTotalPending:0,m_iPacketLeft:0,m_iCurRespHeader :791624304,需要发送121秒,总处理时间:121。 2015-06-15 17:46:35.201 [INFO] [ip.address.removed:62539:HTTP2-1]通过PID中止请求处理:4736,kill:1,开始时间:121,发送时间:121,req处理:0