Click here to Skip to main content
12,395,143 members (60,733 online)
Rate this:
Please Sign up or sign in to vote.
See more: Sockets
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 21-Feb-11 0:34am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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 Smile | :)
SAKryukov 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
EdMan196 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
SAKryukov 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 21 Feb 2011
Copyright © CodeProject, 1999-2016
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