I have socket handler class, which is used to communicate to client with specific ip and port with the help of several socket functions. At the very first time when I am using writetosocket()
function, it's working perfectly.
我有套接字处理程序类,它用于通过几个套接字函数与特定的ip和端口通信。在我第一次使用writetosocket()函数时,它的工作正常。
But when I am restarting client(with ip and port). And tries to use writetosocket()
it returns me broken pipe error with error code 32. but after some successful execution of socket_write
function. Means I am getting this error after some time duration, when I am writing data on socket. I read some solutions and tried most common solution where I am using socket_shutdown
and socket_close
to terminate socket connection properly whenever I am finding client is not responding. And after that I am again calling startconnection, which is giving me new socket. But still I am getting broken pipe error.
但是当我重新启动客户端(使用ip和端口)时。并尝试使用writetosocket()它返回我破坏管道错误与错误代码32.但在一些成功执行socket_write函数后。意味着我在一段时间后得到这个错误,当我在socket上写数据时。我读了一些解决方案并尝试了最常见的解决方案,我使用socket_shutdown和socket_close来正确地终止套接字连接,只要我发现客户端没有响应。然后我再次调用startconnection,这给了我新的套接字。但我仍然得到管道错误的错误。
function startconnection(){
/* Create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo "$errorcode : $errormsg";
return false;
}
else {
echo "Socket successfully created.";
}
/* Accept incoming connections */
$this->result = socket_connect($this->socket, $this->ipaddress, $this->port);
if($this->result === false){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo "$errorcode : $errormsg";
return false;
}
else {
echo "successfully connected to $this->ipaddress, $this->port";
}
return true;
}
function writetosocket($input){
$sent = socket_write($this->socket, $input, strlen($input));
if($sent === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo "$errorcode : $errormsg";
return false;
}
else {
echo "Message Sent : $input";
}
return true;
}
Help me to understand and resolve this problem so that function can handle broken pipe error.
帮助我理解并解决此问题,以便该函数可以处理损坏的管道错误。
1 个解决方案
#1
0
You are getting that error because the server socket has closed and is no longer listening and the client socket is attempting to send data to the server socket after it has been closed but before the port is free to be used again (while it is in TIME_WAIT).
您收到该错误是因为服务器套接字已关闭且不再侦听,并且客户端套接字在关闭之后但在端口可以再次使用之前(在TIME_WAIT中)之前尝试将数据发送到服务器套接字)。
The Server Socket and the Client Socket both go through different steps before they become available for I/O:
服务器套接字和客户端套接字在可用于I / O之前都会执行不同的步骤:
SERVER
- socket()
- 插座()
- bind()
- 绑定()
- listen()
- 听()
- accept()
- 接受()
Client
- socket()
- 插座()
- bind() [optional, see below]
- bind()[可选,见下文]
- connect() [does an implicit bind on an ephemeral port if not already bound]
- connect()[对短暂的端口进行隐式绑定,如果尚未绑定]
#1
0
You are getting that error because the server socket has closed and is no longer listening and the client socket is attempting to send data to the server socket after it has been closed but before the port is free to be used again (while it is in TIME_WAIT).
您收到该错误是因为服务器套接字已关闭且不再侦听,并且客户端套接字在关闭之后但在端口可以再次使用之前(在TIME_WAIT中)之前尝试将数据发送到服务器套接字)。
The Server Socket and the Client Socket both go through different steps before they become available for I/O:
服务器套接字和客户端套接字在可用于I / O之前都会执行不同的步骤:
SERVER
- socket()
- 插座()
- bind()
- 绑定()
- listen()
- 听()
- accept()
- 接受()
Client
- socket()
- 插座()
- bind() [optional, see below]
- bind()[可选,见下文]
- connect() [does an implicit bind on an ephemeral port if not already bound]
- connect()[对短暂的端口进行隐式绑定,如果尚未绑定]