当客户端在 write() 期间终止连接时,由对等套接字错误重置连接
Connection reset by peer socket error when the client terminate connection during write()
>我让我的服务器向客户端(Web浏览器(发送了一个大文件,但是当我刷新页面时,我因对等错误而重置连接,程序终止。我期待的是服务器获得新的连接。
while (true)
{
check((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)),
"accept_failed");
read(new_socket, buffer_recv, 1024);
printf("%sn", buffer_recv);
T = std::thread(handle_connection, new_socket);
T.detach();
}
handle_connection包含
while (file.read(&buffer[0], 1024))
{
s = file.gcount();
content = chunk::make_chunk(buffer, s);
check(write(new_socket, &content[0], content.size()), "Write_error");
}
其中检查是简单的错误处理函数并返回字符串make_chunk。如何使服务器不退出并继续侦听连接,请回答我是套接字编程的新手
是的,您可以将send()
与MSG_NOSIGNAL
一起使用,也可以使用setsockopt()
设置SO_NOSIGPIPE
套接字选项。但是,只需对现有代码进行最小的更改并将设置应用于所有创建的套接字,您可以执行以下操作:
从write()
的手册页
EPIPE
: FD连接到读取端关闭的管道或插座。发生这种情况时,写入过程也将收到SIGPIPE信号。 (因此,仅当程序捕获、阻止或忽略此信号时,才会看到写入返回值。
#include <signal.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
int main()
{
/* .... */
sigset_t sigmask;
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGPIPE);
if (sigprocmask(SIG_BLOCK, &sigmask, NULL) != 0) // get EPIPE instead
goto handle_sigprocmask_fail;
/* .... */
int wr_return = write(sockfd, buf, sizeof(buf));
if (wr_return == -1) {
if (errno == EPIPE)
goto handle_epipe;
}
}
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- 当客户端在 write() 期间终止连接时,由对等套接字错误重置连接
- 从 NAT 后面建立对等连接
- iocp openssl 对等服务器在与 ConnectEx 连接后关闭连接
- 对等方断开连接后未释放 SSL 内存
- QtRO - 类 qremoteobjects - 如何连接 2 个和更多远程对等体 beetwen TCP
- 发送大型数据时由对等方重置 Tcp 连接
- C++中的Bittorrent客户端,在非阻塞套接字上连接到对等端总是超时
- Flash客户端c++服务器连接被对等方重置
- WebRTC对等连接