Should TcpClient.Connect take 5 seconds?

  • Thread starter Thread starter Jim Bernatowicz
  • Start date Start date
J

Jim Bernatowicz

I am developing a program that communicates with an SMTP server using
VS.NET 2002, Framework 1.0. The TcpClient.Connect(host, port) alone
takes 5 seconds to return, while an old COM object connects to the
same server with sub-second response. I have deployed the application
on a different LAN having a different SMTP server and get the same 5
second delay with .NET and sub-second response with the old COM
object.

Is this expected behavior? Is TcpClient inherently inefficient, or
can it be tuned to perform as well as unmanaged equivalents?

Thank you!
Jim Bernatowicz
 
Hi, Jim.

I can connect to any SMTP server in less than half a second (230
milliseconds on average, 15 milliseconds minimum) with a TcpClient. This
depends on the proximity of the server to your computer and how much stress
the server is under. Below you will find the code and sample output.

I noticed that if you use a hostname instead of it's IP address, it takes a
little longer. This is because you need to resolve the name first (via a DNS
query). Maybe this is what makes your TcpClient implementation take longer
than your COM object implementation.

To answer your question: No, TcpClient should not take 5 seconds to connect.

Hope that helps,
-JG


/////////////////////
/// SmtpConnect.cs //
/////////////////////

using System;
using System.IO;
using System.Net.Sockets;

namespace Cryptomaniac {
public class Crytographer {
static void Main(string[] args) {
DateTime beforeConnect, afterConnect;

TcpClient tc = new TcpClient();

beforeConnect = DateTime.Now;
tc.Connect(args[0], 25);
afterConnect = DateTime.Now;

TimeSpan connectTimeSpan = afterConnect.Subtract(beforeConnect);
Console.WriteLine("Connect time: {0} milliseconds.",
connectTimeSpan.Milliseconds);

NetworkStream ns = tc.GetStream();
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);

Console.WriteLine("SMTP server greeting: {0}", sr.ReadLine());

sw.WriteLine("quit");

ns.Close();
}
}
}

==================================
=== I ran the program 4 times, ===
=== here are the results: ===

C:\projects\SmtpConnect\bin\Release>SmtpConnect 127.0.0.1
Connect time: 15 milliseconds.
SMTP server greeting: 220 x-force Microsoft ESMTP MAIL Service, Version:
6.0.260 0.1106 ready at Sun, 26 Oct 2003 22:03:59 -0600

C:\projects\SmtpConnect\bin\Release>SmtpConnect mail.atrevido.net
Connect time: 156 milliseconds.
SMTP server greeting: 220 Atrevido SMTP

C:\projects\SmtpConnect\bin\Release>SmtpConnect 65.54.254.151
Connect time: 437 milliseconds.
SMTP server greeting: 220 mc4-f31.hotmail.com Microsoft ESMTP MAIL Service,
Version: 5.0.2195.5600 ready at Sun, 26 Oct 2003 19:58:56 -0800

C:\projects\Cryptomaniac\bin\Release>Cryptomaniac.exe 165.212.8.32
Connect time: 343 milliseconds.
SMTP server greeting: 220 cmsmail09.cms.usa.net ESMTP USA.NET-SMTA
vC8.MAIN.3.08F; Mon, 27 Oct 2003 03:56:58 GMT
 
Juan Gabriel Del Cid said:
Hi, Jim.

I can connect to any SMTP server in less than half a second (230
milliseconds on average, 15 milliseconds minimum) with a TcpClient. This
depends on the proximity of the server to your computer and how much stress
the server is under. Below you will find the code and sample output.

[...]

Juan,

I tried your snippet and got the same results on my LAN/SMTP server,
so I decided to try the SMTP servers in your post and got a snappy
responses just as you did. I thought I was eliminating variables by
deploying the client on two different LANs each with its own SMTP
server (both with no load, BTW,) but it turns out that both SMTP
servers serving those LANs appear to have some serious latency issues.
Hope that helps,
-JG

Thank you for providing the sample. It helped to uncover the true
cause of my problem.

Jim
 
Back
Top