Accept()需要2-3秒 [英] Accept() takes 2-3 seconds

查看:116
本文介绍了Accept()需要2-3秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在Windows中的接受功能有问题.
从另一个第三方应用程序接受套接字需要2-4秒.

这是我的代码:

Hi,
I''m having a problem with accept function in Windows.
It takes 2-4 seconds to accept a socket from another 3rd party application.

Here is my code:

DWORD dwStart = GetTickCount();
AcceptSocket = accept(ListenSocket, NULL, NULL);
DWORD dwEnd = GetTickCount();
DWORD time = dwEnd - dwStart;
printf("Accept took: %u",time);



请帮助我解决这个问题:)

谢谢,
AR



Please help me with this issue :)

Thanks,
AR

推荐答案

看看关于accept的评论:



如果队列上不存在任何挂起的连接,并且套接字被标记为阻塞,那么accept函数可以阻塞调用者,直到出现连接为止.如果套接字被标记为非阻塞并且不存在挂起的连接,则在队列上,accept返回错误,如下所述.成功完成accept返回新的套接字句柄后,不能将接受的套接字用于接受更多连接.原始套接字保持打开状态,并侦听新的连接请求.




http://msdn.microsoft.com/en-us/library/ms737526% 28VS.85%29.aspx [ ^ ]

您可能希望通过使用线程来防止阻塞来缩短响应时间.您可以使用事件通知主线程,但请确保正确调用此事件,以避免该区域出现问题.

http://members.cox.net/doug_web/threads.htm [
Have a look at the remarks on accept:



The accept function can block the caller until a connection is present if no pending connections are present on the queue, and the socket is marked as blocking. If the socket is marked as nonblocking and no pending connections are present on the queue, accept returns an error as described in the following. After the successful completion of accept returns a new socket handle, the accepted socket cannot be used to accept more connections. The original socket remains open and listens for new connection requests.




http://msdn.microsoft.com/en-us/library/ms737526%28VS.85%29.aspx[^]

You might want to improve response time by using a thread to prevent blocking. You could notify the main thread using an event but be sure to invoke this properly to avoid problems in that area.

http://members.cox.net/doug_web/threads.htm[^]

Good luck!


这篇关于Accept()需要2-3秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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