我正在从 php 套接字中的 gps 设备获取数据。设备已连接到服务器,但套接字无法读取数据并给出如下错误:
socket_read() 无法从对等方重置的套接字 104 连接中读取
我的套接字代码:
$ip = "107.191.105.101";
$port = '5028';
if(!($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
/*if( !socket_bind($sock, $ip , $port) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
} */
socket_connect($sock, $ip , $port);
//
echo "Socket bind OK \n";
//listen the socket
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//array of client sockets
$client_socks = array();
$max_clients = 1000;
//array of sockets to read
$read = array();
//start loop to listen for incoming connections and process existing connections
while (true)
{
//prepare array of readable client sockets
$read = array();
//first socket is the master socket
$read[0] = $sock;
//now add the existing client sockets
for ($i = 0; $i < $max_clients; $i++)
{
if($client_socks[$i] != null)
{
$read[$i+1] = $client_socks[$i];
}
}
//now call select - blocking call
if(socket_select($read , $write , $except , null) === false)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
//
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
//if ready contains the master socket, then a new connection has come in
if (in_array($sock, $read))
{
for ($i = 0; $i < $max_clients; $i++)
{
if ($client_socks[$i] == null)
{
$client_socks[$i] = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client_socks[$i], $address, $port))
{
echo "Client $address : $port is now connected to us. \n";
}
break;
}
}
}
//check each client if they send any data
for ($i = 0; $i < $max_clients; $i++)
{
if (in_array($client_socks[$i] , $read))
{
$input = socket_read($client_socks[$i] , 10240, PHP_BINARY_READ);
if ($input == null || $input === '')
{
//zero length string meaning disconnected, remove and close the socket
unset($client_socks[$i]);
socket_close($client_socks[$i]);
}
if(strlen($input) == 17)
{
// Recieve data from tcp socket
$payloadFromDevice = bin2hex($input);
$input = socket_read($client_socks[$i] , 2048, PHP_BINARY_READ);
if ($input == null || $input === '')
{
//zero length string meaning disconnected, remove and close the socket
unset($client_socks[$i]);
socket_close($client_socks[$i]);
}
$input = bin2hex($input); // from gps module rawdata bin to hex
// Now we need to wait for next data from the device
// Recieve next payload from the socket (now with data)
$tcpPayloadFromDevice = $input;
$res_write=socket_write($client_socks[$i], chr("00000002"));
unset($client_socks[$i]);
}
}
}
}
我的代码工作正常,它连接到设备,但是当我尝试使用数据读取数据时,socket_read()
数据为空或未读取错误。我也试过socket_recv()
了,但仍然没有得到数据,或者我得到了同样的错误。