Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Everyone,


I am using socket programming to connect to the TCP/IP server using c++.
when user selects an invalid IP address of TCP/IP server. The connect() API takes so much time.

I want my application <small></small>to wait for the response from that API for specific time. If no response comes in that specific period. My application should not try more for connecting to that IP address.

Please suggest me some ways to achieve this.
Posted

There is unfortunately no timneout property, however, there is an easy way to time the connection. With the follwoing steps:

1) Create a variable called Connected, set to false
2) Where you want to call connect:
1) Set Connected to false
2) Call Socket.BeginConnect
3) Use an ManualResetEvent.WaitOne(*length of timeout*)
3) When the AsynConnect event is called set Connected to true or false and set the waiting ManualResetEvent
4) If the waiting ManualResetEvent timesout then Connected will be false and you can handle it or if the connection fails Connected will be false and you can then handle or, if all succeeded, Connected will be true and you can continue using that connection.

It is a simple solution with little, practically no, need for threading.

Hope this helps,

Ed :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Feb-11 21:49pm    
This is _very_ fortunate that there is not timeout on this API, designed specially to avoid abuse you try to do.
--SA
Well the problem is that you can not specify the timeout (unfortunately) on the connection method. The only way to do this to change the environmental values of the PC, I do not recommend this since this change will apply to every application that uses that function on that particular pc.

What you could do is create a separate thread for every call to the connect method if the thread doesn’t return according your specified timeout value ignore the result. (I suggest using a thread pool)

D, Kurt.
 
Share this answer
 
v3
Comments
Ed Nutting 21-Feb-11 7:05am    
Change the value for the whole computer? Whoaa...hold on there.. As you rightly stated that will affect every program and I would never trust every program to have been well written so that the timeout didn't matter... My point is that it'd be far better to do your second suggestion than to go changing the computer's settings. Furthermore, if this is a practical applicatioon it is impractical to ask a user or administrator to change their computers' settings. Also, I think there is a better way, please see my answer.
Kurt Degiorgio 21-Feb-11 8:58am    
thats why i stated "I do not recommend this" but it can be done
Sergey Alexandrovich Kryukov 21-Feb-11 21:48pm    
This is _very_ fortunate that there is not timeout on this API, designed specially to avoid abuse you try to do.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900