您的位置:首页 > 其它

socket相关知识

2013-12-20 10:26 295 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: