socket相关知识

本文详细解释了阻塞与非阻塞IO的区别,包括同步与异步I/O的概念,以及如何通过设置SO_RCVBUF和SO_SNDBUF优化TCP堆栈接收和发送缓冲区性能。同时,介绍了在Linux环境下如何设置SO_SNDBUF来提高数据发送效率。
摘要由CSDN通过智能技术生成

1.      Blocking/Non-blocking   vs  Sync/Async

- Blocking I/O means that thecalling system does not return control to the caller until the operation isfinished. As a result, the caller is blocked and cannot perform otheractivities during that time.
- Non-blocking Synchronous I/O means that call returnscontrol to the caller immediately and the caller is not made to wait. Theinvoked system immediately returns one of two responses: If the call wasexecuted and the results are ready, then the caller is told of that.Alternatively, the invoked system can tell the caller that the system has noresources (no data in the socket) to perform the requested action.
- Non-blocking Asynchronous I/O means that the callingfunction returns control to the caller immediately, reporting that therequested action was started. The invoked system will notify the caller (bycallback for example), when the result is ready for processing.

- Asynchronousrefers to something done in parallel,say is another thread.

- Non-blockingoften refers to polling, i.e.checking whether given condition holds



http://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking

http://www.ibm.com/developerworks/linux/library/l-async/

 

2.      SO_RCVBUF&& SO_SNDBUF

If you use the SO_RCVBUF and SO_SNDBUF optionto set zero TCP stack receive and send buffer, you basically instructthe TCP stack to directly perform I/O using the buffer provided in yourI/O call. Therefore, in addition to the non-blocking advantage of theoverlapped socket I/O, the other advantage is better performance because yousave a buffer copy between the TCP stack buffer andthe user buffer for each I/O call.

When SO_SNDBUFis set to 0, which hasnice effect of causing send() and sendto() to not return until thepacket has been sent. This operation became blocked, because system used your(app) buffer instead of copy your buffer into winsock buffer ( which set to 0 )and return from the send()/sendto() immediately ( in the case of non-blockedsocket ). The ACK here will play the role when you have data more than MTU (1460 for ethernet ) , so data have to be divided and each next packet will besend only after ACK of previous , so in this case that not only blocked up todata copied into winsock buff but up to all the data will send.

http://xcybercloud.blogspot.com/2009/06/network-io-blockingnon-blocking-vs.html

 

3.      SetSO_SNDBUF on Linux.

Sets or gets the maximum socket send bufferin bytes.  The kernel doubles this value (to allow space for bookkeepingoverhead) when it is set using setsockopt(), and  this doubled  value  is returned by  getsockopt().  The default value is set by the wmem_defaultsysctl and the maximum allowed value is set by the wmem_max sysctl.  The minimum (doubled) value for this optionis 2048.

http://stackoverflow.com/questions/2031109/understanding-set-getsockopt-so-sndbuf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值