这是我的 php 客户端代码:
<?php
$service_port = 5310;
$address = gethostbyname('192.168.40.100');
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false)
echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "<br />";
else
{
$result = socket_connect($socket, $address, $service_port);
if ($result === false)
echo "socket_connect() falló.\nRazón: ($result) " . socket_strerror(socket_last_error($socket)) . "<br />";
else
{
$in = "Envio esto: HOLA";
$out = 'RESPONSE: ';
echo "ANTES: " . date("H:i:s") . "<br />"; // #1
socket_write($socket, $in, strlen($in));
echo "DESPUÉS: " . date("H:i:s") . "<br />"; // #2 Same time as #1
// Leer/imprimir respuesta
while ($out = socket_read($socket, 2048))
echo "RESPONSE: " . date("H:i:s") . " " . $out; // #3 Same time as #2
}
}
// Cerramos socket
socket_close($socket);
echo "BANANA: " . date("H:i:s") . "<br />"; // Always run when I got disconected from server
?>
我无权访问server code
,但我读了一点,它被写入VB.net
并使用类似的东西Client.socket.send()
,我得到了响应,当服务器断开我的连接时,我看到那些回显打印得很好,如果没有,我的 php 将被挂起无处不在。
编辑
我添加了另一个socket_write()
是为了告诉服务器,请断开我的连接,但第二个没有发送到服务器。
<?php
$service_port = 5310;
$address = gethostbyname('192.168.40.100');
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false)
echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "<br />";
else
{
$result = socket_connect($socket, $address, $service_port);
if ($result === false)
echo "socket_connect() falló.\nRazón: (" . $result . ") " . socket_strerror(socket_last_error($socket)) . "<br />";
else
{
$in = "Envio esto: HOLA";
$out = '';
$response = "RESPONSE: ";
echo "PRIMERA;";
socket_write($socket, $in, strlen($in));
while ($out = socket_read($socket, 2048))
$response .= $out;
echo $response . ";SEGUNDA;";
$in = "close";
$out = '';
$response = "";
socket_write($socket, $in, strlen($in));
while ($out = socket_read($socket, 2048))
$response .= $out;
echo $response . " " . date("H:i:s");
}
}
socket_close($socket);
?>