Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ socket TCP/IP
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);
 
Please help me with this issue Smile | :)
 
Thanks,
AR
Posted 1-Dec-10 4:00am
Edited 1-Dec-10 4:01am
Henry Minute223.9K
v2
Comments
Lloyd Atkinson at 1-Dec-10 11:01am
   
Perhaps it's simply because it blocks, and then waits for a connection which could take anything from a few milliseconds to several seconds depending on how fast/reliable the network is.
Alexeirob at 2-Dec-10 2:37am
   
But how can it be? The connect is coming 0.5 seconds before the accept. And the connection is through the loopback. Can it be related to blocking/non-blocking socket?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
v2
Comments
Dalek Dave at 1-Dec-10 10:00am
   
Good call.
Alexeirob at 2-Dec-10 3:22am
   
Thanks E.F. Nijboer.
I've looked at the accept remarks, and the connect() function (in the client side) called before the accept, so that there are pending connections.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 334
2 CPallini 275
3 DamithSL 230
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 1 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100