阻塞与非阻塞,异步与同步通信 [英] Blocking vs non-blocking, asynchronous vs. synchronous communication

查看:128
本文介绍了阻塞与非阻塞,异步与同步通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)使用非阻塞套接字比阻塞套接字有什么优势?
2)在带有阻塞套接字的情况下使用beginSend/beingReceive有什么优势?

我正在建立具有阻止套接字的客户端-服务器聊天应用程序..
现在,我试图抓住的是使用非阻塞套接字或异步方法的优点:
-如果我使用非阻塞,我仍然需要循环抛出该消息以发送所有消息.因此,如果使用阻塞套接字,它将阻塞,甚至可能不会完全循环一半,从而抛出发送操作
-如果我使用开始发送,则结束直到执行完为止(那是什么意思?),然后再检查是否所有数据包都已发送完,所以我需要在asynccallback方法中对其进行检查,并在必要时再次递归调用它,这看起来像据我了解,回调是在线程池上完成的,因此开销很大,因此消息必须等到线程可用后才能发送,而不是让线程无休止地循环并发送消息.
我可以看到使用beingSend/beginReceive获得程序响应的优势,但是我似乎找不到一种方法来返回从它发送的字节?
它返回IAsyncResual,但是有一种方法可以返回发送结果的字节吗?

1) What would be the advantages of using non-blocking sockets over blocking sockets?
2) What would be the advatages of using beginSend/beingReceive with a blocking socket ?

I am building a client-server chat applicaition with blocking sockets ..
now what im trying to grasp would be the advantages of using non-blocking sockets or asyncrounous methods:
- If I use non-blocking i still need to loop threw the message to send it all..So with blocking sockets it would block and maybe not half to loop at all threw the send operaition
- If I use begin send, the endsend blocks until done (so whats the point?) plus in order to check if all the packet was sent i need to check it in the asynccallback method and recursivly call it again if necessary,which seemes like a lot of overhead when as I understand the callback is done on the threadpool so the message would have to wait until a thread is available in order to send it, instead I got a thread looping endlessly and sending messages.
I could see the advantages of using beingSend / beginReceive for the reponsiveness of the program but I can''t seem to find a way to return the bytes sent from it ?
It returns IAsyncResual but is there a way to return the bytes that where sent threw the result ?

推荐答案

通常,您将使用beginSend发送所有数据,然后仅当您直到无法发送所有数据,然后才能为每个发送的数据包调用endSend时,您才能继续进行操作.这样,您就可以在发送过程中自由执行其他任务.

如果您必须知道哪个数据包导致了错误,则需要使用objectState来保存指向该数据包的信息.
You would normally send all the data with beginSend and then only when you reach a point where you cannot proceed unless all the data has been sent then and only then you call endSend for each of the packets sent. This way you are free to do other tasks while the Send is taking place.

If you must know which packet caused an error you need to use the objectState to hold the information that points to the packet.


这篇关于阻塞与非阻塞,异步与同步通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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