Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: socket
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 1:34am
Rate this: bad
good
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 | :)
  Permalink  
Comments
SAKryukov at 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
Rate this: bad
good
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.
  Permalink  
v3
Comments
EdMan196 at 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 at 21-Feb-11 8:58am
   
thats why i stated "I do not recommend this" but it can be done
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 299
1 Maciej Los 295
2 Sergey Alexandrovich Kryukov 205
3 Aajmot Sk 202
4 Sinisa Hajnal 166
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 21 Feb 2011
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