大家好,如果已经问过,希望你们不要用虚拟枪射击我,但是,就这样吧。
我是 socket_send 到一个 ip/port,我看到服务器接收并回复数据。我的代码接收 xxxx 个字节。我想知道这些字节需要 IE: xml 字符串,以便我可以在我的应用程序中解析和使用数据。cURL 在这种情况下是不可行的,因为服务器接收到标头并且根本不响应。
**Code:**
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$port'...";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = $xml;
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
socket_shutdown($socket, 1);
echo "Reading response:\n\n";
$buf = '';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);
echo $buf . "\n";
echo "OK.\n\n";
?>
回复:
TCP/IP 连接正常。好的。正在尝试连接到端口 '9000' 上的 'xxx.xxx.xxx.xxx' ...好的。发送 HTTP HEAD 请求...好的。读取响应:从 socket_recv() 读取 1365 个字节。关闭插座...好的。