Hi I noticed that neither of the two classes (TcpClient or Socket) have a timeout on connect. I mean a timeout you can set.
I hae a program that should check about 10.000 machines for some specific port open on them. The default timeout of 20 - 30 seconds is pretty bad because it would take me a long time to check all of the machines.
what I tried was something like this:
Socket conector = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint host = new IPEndPoint(Some_address,Some_Port);
conector.Blocking = false;
conector.Connect(host);
if (conector.Poll(1000,SelectMode.SelectRead)) //Try for one second to read from the socket
{
//port is ok on the host
}
else
{
throw new Exception("Timeout detected");
}
Hoewver this does not work as I would expect.. Meaning that sometimes it gives the TImeoud detected.. other times it sits for 30 secunds and then times-out .. But now even if the host I am connecting to is accepting my connection I will get either a socket
timeout or my own timeout exception.
Does any one know how to handle connect timeouts?
Any help is appreciated.. thank you
-
-
Hmmm.... why are you probing 10,000 machines?
-
Minh wrote:Hmmm.... why are you probing 10,000 machines?
Sorry for not cleairng that up.
I have a list of 10.000 proxy servers .. and I am sure that 9000 of them are bogus so I want to clear that list up a little.
The actual question about the timeout is a little older then this issue but this is the first time I actually need it
-
I don't know much about sockets, but is it maybe possible to spawn threads and kill them after a second if they don't complete?
I don't know however if the framework will timely release the connection.
Else use XP SP1 as it allows more open endpoints, or look for the patch for SP2.
Good luck,
Peter -
PeterF wrote:I don't know much about sockets, but is it maybe possible to spawn threads and kill them after a second if they don't complete?
I don't know however if the framework will timely release the connection.
Else use XP SP1 as it allows more open endpoints, or look for the patch for SP2.
Good luck,
Peter
I am looking into using threads now .. however while checking the socket they opereate on some variables so I have to lock { } then .. but for it to be efficient I have to use something like 20 - 40 threads at once and I have to look into the race conditions.
This would be one working alternative (Thank you
) but I would still like to know a way to set a connect Timeout (I mean .. another way)
Thank you
-
Realize, you are attempting to write a port scan. Not a good thing, but since you asked, here are some ideas:
1) Look at the asynchronous form of connect. (Socket.BeginConnect).
2) There is a limit to how many active threads you can have in .Net. If you exeed this, your blocking calls will hang while you wait for other threads to complete. By setting blocking to false, you are causing extra threads to be created. If you are going to go through the trouble to do this, then see #1 above.
3) Write your own timeout code: Basically, what you do is fire off a thread that will monitor the socket and if it is unable to connect after x seconds, kill the socket.
-
ScanIAm wrote:Realize, you are attempting to write a port scan. Not a good thing, but since you asked, here are some ideas:
1) Look at the asynchronous form of connect. (Socket.BeginConnect).
2) There is a limit to how many active threads you can have in .Net. If you exeed this, your blocking calls will hang while you wait for other threads to complete. By setting blocking to false, you are causing extra threads to be created. If you are going to go through the trouble to do this, then see #1 above.
3) Write your own timeout code: Basically, what you do is fire off a thread that will monitor the socket and if it is unable to connect after x seconds, kill the socket.
Great rules.. I'll try to follow them.
I don't think I am running a port scan.. But maybe I don't get the port scan thing.
I do not intent to see anything else but if the proxy servers are still active.
And I only intend to check that list once
Thanks a lot for your replyes .. they've been fast and helpfull.. As soon as I get to a good method of doing this.. I'll come back and post
-
ScanIAm wrote:2) There is a limit to how many active threads you can have in .Net. If you exeed this, your blocking calls will hang while you wait for other threads to complete. By setting blocking to false, you are causing extra threads to be created. If you are going to go through the trouble to do this, then see #1 above.
I don't think that's true. You can create as many threads as the system allows in .Net.
However, most asynchronous methods in the .Net BCL don't create new threads but instead use thread pooling, and of course there is a limit to the amount of threads in the pool. -
stefan.constantin wrote:
I am looking into using threads now .. however while checking the socket they opereate on some variables so I have to lock { } then .. but for it to be efficient I have to use something like 20 - 40 threads at once and I have to look into the race conditions.
The threadpool is very handy for tasks like this. I have an app that collects status from many machines (some are off line and time out) and the thread pool makes the task really simple.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadpoolclasstopic.asp
Stephen. -
Sven Groot wrote:

ScanIAm wrote: 2) There is a limit to how many active threads you can have in .Net. If you exeed this, your blocking calls will hang while you wait for other threads to complete. By setting blocking to false, you are causing extra threads to be created. If you are going to go through the trouble to do this, then see #1 above.
I don't think that's true. You can create as many threads as the system allows in .Net.
However, most asynchronous methods in the .Net BCL don't create new threads but instead use thread pooling, and of course there is a limit to the amount of threads in the pool.
I stand corrected.
I thought I said what you said, but I intead said what I said...instead.
I hope that makes sense. -
PerfectPhase wrote:

stefan.constantin wrote:
I am looking into using threads now .. however while checking the socket they opereate on some variables so I have to lock { } then .. but for it to be efficient I have to use something like 20 - 40 threads at once and I have to look into the race conditions.
The threadpool is very handy for tasks like this. I have an app that collects status from many machines (some are off line and time out) and the thread pool makes the task really simple.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadpoolclasstopic.asp
Stephen.
I am glad to see you people still posted.. I just finished the problem in a very easy way.
I created another method wich actually connected the socket ... had the main thread sleep for 2 seconds and then check on the connecting method (wich is run in a separate thread) if the socket was connected good otherwise throw an exception "Timed out " and that;s all.
Thanks again for the repleies.
-
stefan.constantin wrote:

PerfectPhase wrote: 
stefan.constantin wrote:
I am looking into using threads now .. however while checking the socket they opereate on some variables so I have to lock { } then .. but for it to be efficient I have to use something like 20 - 40 threads at once and I have to look into the race conditions.
The threadpool is very handy for tasks like this. I have an app that collects status from many machines (some are off line and time out) and the thread pool makes the task really simple.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadpoolclasstopic.asp
Stephen.
I am glad to see you people still posted.. I just finished the problem in a very easy way.
I created another method wich actually connected the socket ... had the main thread sleep for 2 seconds and then check on the connecting method (wich is run in a separate thread) if the socket was connected good otherwise throw an exception "Timed out " and that;s all.
Thanks again for the repleies.
Isn't there a SocketOption you can set with Socket.SetSocketOption to get the required behaviour?
-
No there isn't... you can set the Send or Receive Timeout there....
-
Ah that must have been it, memory playing tricks on me.
-
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect using a timeout (5 seconds)
IAsyncResult result = socket.BeginConnect( sIP, iPort, null, null );
bool success = result.AsyncWaitHandle.WaitOne( 5000, true );
if ( !success )
{
// NOTE, MUST CLOSE THE SOCKETsocket.Close();
throw new ApplicationException("Failed to connect server.");
}// Success
//...
-
stefan.constantin wrote:I have a list of 10.000 proxy servers .. and I am sure that 9000 of them are bogus so I want to clear that list up a little.
Um... this sounds fishy. What is the provenance of this list of 10,000 proxy servers?
-
Yikes! Thread necromancy alert!
-
Resurrect();
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.