TCP套接字-发送和接收问题 [英] TCP Socket - Send and receive question
问题描述
你好,
我正在尝试使用.net中的异步套接字发送消息并接收响应.在一个函数中,我执行以下操作
1.通过套接字发送消息
2. while(fullresponserecieved)= false,保持状态,当我收到0个字节(或消息末尾)时,此变量变为true.
3.然后执行其余功能.
我认为这导致线程出现问题.线程执行函数和异步套接字线程.这是正确的方法吗?还是我应该使用阻塞线程?可能需要一些时间才能收到整个响应,最多可能需要1分钟,我不希望我的应用在这段时间内没有响应.
谢谢,
-ren
Hello,
I am trying to send a message and receive a response using asynchonous sockets in .net. In a function I do the following
1. Send a message thru the socket
2. While(fullresponserecieved)=false, this holds state, this variable turns to true while I receive 0 bytes(or end of message).
3. Then I execute the rest of my function.
I think this is causing a problem with the threads. The thread executing function and the asynchronous socket threads. Is this the correct way to do this? Or should I be using a blocking thread? It may take some time to receive the entire response, up to about 1 minute, I wouldn''t want my application to become unresponsive for that period of time.
Thanks,
-ren
推荐答案
在我看来,线程使异步API变得多余.异步调用无论如何应该间接产生一个线程,或者具有等效的功能.将线程与同步阻塞调用一起使用是更直接,更可靠和更可靠的方法.
—SA
In my strong opinion, threads make asynchronous APIs redundant. Asynchronous calls should spawn a thread indirectly anyway, or have equivalent functionality. Using threads with synchronous blocking calls is more straightforward, robust and reliable way.
—SA
这篇关于TCP套接字-发送和接收问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!