TCP Socket Performance issue

本文探讨了TCP协议中数据发送的效率问题,包括应用程序如何更高效地写入大量消息,TCP发送缓冲区大小对应用性能的影响,以及TCP_NODELAY选项的作用。文中还介绍了如何通过调整socket缓冲区大小来维持稳定的数据包流。
1.In general, it is more efficient for the application to write large messages and have them fragmented and reassembled by IP, than to have the application write multiple times.

原因就在于系统调用system_call从应用态转到内核态等系列复杂过程带来的性能的损耗

2.tcp_sendspace

TCP send buffer size can limit how much data the application can send before the application is put to sleep. The TCP socket send buffer is used to buffer the application data in the kernel using mbufs/clusters before it is sent beyond the socket and TCP layer. The default size of this buffer is specified by the parameter tcp_sendspace, but you can use the setsockopt() subroutine to override it.

If the amount of data that the application wants to send is smaller than the send buffer size and also smaller than the maximum segment size and if TCP_NODELAY is not set, then TCP will delay up to 200 ms, until enough data exists to fill the send buffer or the amount of data is greater than or equal to the maximum segment size, before transmitting the packets.

If TCP_NODELAY is set, then the data is sent immediately (useful for request/response type of applications). If the send buffer size is less than or equal to the maximum segment size (ATM and SP switches can have 64 K MTUs), then the application's data will be sent immediately and the application must wait for an ACK before sending another packet (this prevents TCP streaming and could reduce throughput).

TCP发送数据时首先要看发送缓冲区是否有足够的空间存放待发送消息,如果有则复制到发送缓冲区,并且需要等待ack回来才会删除该数据,如果发送缓冲区没有足够空间则会等待或者返回EWOULDBLOCK(非阻塞) ----TCPv2

Note: To maintain a steady stream of packets, increase the socket send buffer size so that it is greater than the MTU (3-10 times the MTU size could be used as a starting point).
If an application does nonblocking I/O (specified O_NDELAY or O_NONBLOCK on the socket), then if the send buffer fills up, the application will return with an EWOULDBLOCK/EAGAIN error rather than being put to sleep. Applications must be coded to handle this error (suggested solution is to sleep for a short while and try to send again).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值