centos7,php5.6 environment
now encounters such a problem. In the same class, there are three methods that use the following code (the values of $host and $port are fixed).
an error is reported when executing to the socket_read () of the second method. The error message is as follows.
socket_read (): unable to read from socket [104]: Connection reset by peer in
$socket = $this->getSocket($host,$port);
if(!$socket){
echo "socket";
return;
}
$res1 = socket_write($socket, $this->login, strlen($this->login));
//$str.
$res2 = socket_write($socket, $str, strlen($str));
if(!$res1 || !$res2){
echo "";
return;
}
while (true) {
//socket_readsocket_read(): unable to read from socket [104]: Connection reset by peer in xxx
$response = socket_read($socket, 1024);
}
here is the code for the getSocket () method
public function getSocket($host,$port)
{
//static::$mySocket = null;
if(!self::$mySocket){
self::$mySocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!self::$mySocket){
return false;
}
socket_set_option(self::$mySocket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1, "usec"=>0 ) );
socket_set_option(self::$mySocket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>1, "usec"=>0 ) );
$result = socket_connect(self::$mySocket, $host,$port);
if (!$result) {
self::$mySocket = null;
return false;
}
}
return self::$mySocket;
}
but when I change the getSocket method to the following, I won"t report an error.
is it because you need to re-execute socket_create and socket_connect before each socket_write?
Please take a look, thank you very much!
public function getSocket($host,$port)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$socket){
return false;
}
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1, "usec"=>0 ) );
socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>1, "usec"=>0 ) );
$result = socket_connect($socket, $host,$port);
if (!$result) {
return false;
}else{
return $socket;
}
}