Tech Off Thread

18 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

TcpClient or Socket Connect() timeout

Back to Forum: Tech Off
  • User profile image
    stefan.​constantin

    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

  • User profile image
    Minh

    Hmmm.... why are you probing 10,000 machines?

  • User profile image
    stefan.​constantin

    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

  • User profile image
    PeterF

    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

  • User profile image
    stefan.​constantin

    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 Smiley ) but I would still like to know a way to set a connect Timeout (I mean .. another way)
    Thank you

  • User profile image
    ScanIAm

    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.

  • User profile image
    stefan.​constantin

    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

  • User profile image
    Sven Groot

    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.

  • User profile image
    PerfectPhase

    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.

  • User profile image
    ScanIAm

    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.

  • User profile image
    stefan.​constantin

    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.

  • User profile image
    Rossj

    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?

  • User profile image
    stefan.​constantin

    No there isn't... you can set the Send or Receive Timeout there....

  • User profile image
    Rossj

    Ah that must have been it, memory playing tricks on me.

  • User profile image
    BiTTiR

    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 SOCKET

                socket.Close();
                throw new ApplicationException("Failed to connect server.");
    }

    // Success
    //...

  • User profile image
    Matthew van Eerde

    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?

  • User profile image
    Sven Groot

    Yikes! Thread necromancy alert!

  • User profile image
    Charles

    Resurrect();

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.