TCP客户端服务器SIGPIPE [英] TCP client-server SIGPIPE

查看:197
本文介绍了TCP客户端服务器SIGPIPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计和测试基于TCP套接字(Internet域)的客户端服务器程序.目前,我正在本地计算机上对其进行测试,并且无法理解有关SIGPIPE的以下内容.

I am designing and testing a client server program based on TCP sockets(Internet domain). Currently , I am testing it on my local machine and not able to understand the following about SIGPIPE.

*. SIGPIPE随机出现.可以确定吗?

*. SIGPIPE appears quite randomly. Can it be deterministic?

第一个测试涉及到客户端的单个小(25个字符)发送操作以及服务器上的相应接收操作.同一台计算机上的同一代码是否成功运行(SIGPIPE)完全超出了我的控制范围.失败率约为45%的次数(相当高).因此,我可以通过任何方式对机器进行调整以使其最小化.

The first tests involved single small(25 characters) send operation from client and corresponding receive at server. The same code, on the same machine runs successfully or not(SIGPIPE) totally out of my control. The failure rate is about 45% of times(quite high). So, can I tune the machine in any way to minimize this.

**.第二轮测试是从客户端向服务器发送40000条小消息(25个字符)(总共1MB的数据),然后服务器以实际接收到的数据总大小进行响应.客户端在紧密循环中发送数据,并且服务器上只有一个接收呼叫.它只能在发送的总数据中最多使用1200字节,并且再次出现这些不确定的SIGPIPE,大约是现在的70%(非常糟糕).

**. The second round of testing was to send 40000 small(25 characters) messages from the client to the server(1MB of total data) and then the server responding with the total size of data it actually received. The client sends data in a tight loop and there is a SINGLE receive call at the server. It works only for a maximum of 1200 bytes of total data sent and again, there are these non deterministic SIGPIPEs, about 70% times now(really bad).

有人可以建议我对设计进行一些改进(可能在服务器上).要求是,在与服务器建立单个套接字连接之后,客户端应能够通过中等到非常高的数据量(每个消息大约25个字符)发送数据. 我觉得针对单个接收的多次发送将始终是有损的,并且效率很低.我们是否应该合并消息并仅在一个send()操作中发送.那是唯一的方法吗?

Can some one suggest some improvement in my design(probably it will be at the server). The requirement is that the client shall be able to send over medium to very high amount of data (again about 25 characters each message) after a single socket connection has been made to the server. I have a feeling that multiple sends against a single receive will always be lossy and very inefficient. Shall we be combining the messages and sending in one send() operation only. Is that the only way to go?

推荐答案

当您尝试写入未连接的管道/套接字时,将发送SIGPIPE.安装信号处理程序将使send()返回错误.

SIGPIPE is sent when you try to write to an unconnected pipe/socket. Installing a handler for the signal will make send() return an error instead.

signal(SIGPIPE, SIG_IGN);

或者,您可以为套接字禁用SIGPIPE:

Alternatively, you can disable SIGPIPE for a socket:

int n = 1;
setsockopt(thesocket, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof(n));

此外,您提到的数据量不是很高.可能是某个地方的错误导致您的连接意外关闭,从而产生了SIGPIPE.

Also, the data amounts you're mentioning are not very high. Likely there's a bug somewhere that causes your connection to close unexpectedly, giving a SIGPIPE.

这篇关于TCP客户端服务器SIGPIPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆