CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2002
    Posts
    3

    Question Slow Stream Socket Connection

    I'm connecting to a remote server via a stream socket and am experiencing exceptionally slow connection times. For example, using a valid IP address and port number, the following code finishes connecting to the server in 4 to 7 seconds. using System;
    using System.Net.Sockets;
    *****************************************
    namespace TCPClient
    {
    public class TCPClient
    {
    public TCPClient()
    {
    TcpClient client;
    client = new TcpClient();
    client.Connect("123.123.123.123", 99999);
    }
    static void Main()
    {
    new TCPClient();
    }
    }
    }
    *****************************************
    Corresponding code in Java, or C++ completes in a few milliseconds.

    Does anybody have any suggestions as to why this may be occurring?

  2. #2
    Join Date
    May 2002
    Location
    Atlanta,GA
    Posts
    262

    Re: Slow Stream Socket Connection

    Originally posted by stolentomato
    I'm connecting to a remote server via a stream socket and am experiencing exceptionally slow connection times. For example, using a valid IP address and port number, the following code finishes connecting to the server in 4 to 7 seconds. using System;
    using System.Net.Sockets;
    *****************************************
    namespace TCPClient
    {
    public class TCPClient
    {
    public TCPClient()
    {
    TcpClient client;
    client = new TcpClient();
    client.Connect("123.123.123.123", 99999);
    }
    static void Main()
    {
    new TCPClient();
    }
    }
    }
    *****************************************
    Corresponding code in Java, or C++ completes in a few milliseconds.

    Does anybody have any suggestions as to why this may be occurring?
    Have you tried using a plain Socket and connecting that way?
    Jared

  3. #3
    Join Date
    Jun 2002
    Posts
    3
    oh, so there's another way to "skin the cat". This is my first wack at an application with C#. I didn't realize there was an alternative. I went back and used the regular socket and saw dramatic improvements in the connection time.

    Thanks very much for the tip.

  4. #4
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    That still doesnt explain it though - TcpClient may have some overhead, but not 4-7 seconds.

    99999 is an invalid port number, by the way...

  5. #5
    Join Date
    May 2002
    Location
    Atlanta,GA
    Posts
    262
    Originally posted by Arild Fines
    That still doesnt explain it though - TcpClient may have some overhead, but not 4-7 seconds.

    99999 is an invalid port number, by the way...
    His original post said he was doing this with a valid port and IP
    Jared

  6. #6
    Join Date
    Jun 2002
    Posts
    3
    Yes, "99999" is an invalid port number.

    In the original post, the code uses bogus IPAddr, and port values. It's mentioned also that using "valid" IPAddr and port values the example code yields 4 to 7 second connection timings. I couldn't agree more that this is unacceptable in a production application.

    However, Jared pointed out that using a "plain" socket may be an avenue of pursuit. As it turned out, the plain socket hooked up to my server as expected; that is, subsecond speeds for the first pass at the connection, and connection speeds that were immeasurable for subsequent connections.

    Maybe the reason for a 4 to 7 second connection time hasn't been provided, but the desired result has been achieved.

    Thanks again for the help, Jared.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured