C# Applet, TcpClient class. Why does this one line of code take so long to execute?

  • Thread starter Thread starter C# Man
  • Start date Start date
C

C# Man

As part of a web application written in C#, I'm using the TcpClient class to connect to port 2112 on a remote machine. If I compile the code as a regular EXE and run it on my local machine, it works fine. But if I compile as a DLL and run it inside a web page (which is my end goal), the constructor for the TcpClient class takes a long time (100 seconds) to execute.
Clues which may help solve this problem:
My Network Admin ran a packet sniffer program to monitor what was going on. He says that TcpClient first opens a connection to port 80, but does not send any data. Then, when the connection to port 80 times out (I have no idea why it is trying to connect to port 80!), it will then quickly connect to port 2112.

We then changed the active port for the IIS web server from 80 to 81. Now the TcpClient class constructor runs quickly. While this solutions works, it's obviously a "band-aid" approach as I can't ask all of my clients to use port 81!

I've developed a small class to demonstrate this strange behaviour.

Below is the HTML code that is used to invoke this DLL on a web page:

<object classid="http:WhyDoesThisTakeSoLong.dll#WhyDoesThisTakeSoLong.PleaseTellMe" width="100%" height="100%"></object>
And here is the C# class:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace WhyDoesThisTakeSoLong
{
public class PleaseTellMe : System.Windows.Forms.UserControl
{
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.Container components = null;

// This class uses the TcpClient class to send and receive bytes
// over a socket stream. The code works perfectly when compiled
// as an EXE and run as a normal application. However, if it is
// compiled and then included as an OBJECT on a web page, the
// line of code that instantiates the TcpClient will take almost
// exactly 100 seconds to execute. After the first TcpClient is
// initialized, additional TcpClient connections will be processed quickly.
//
// Notes:
// The web page is fetched from an IIS server, although I have
// verified that the exact same problem occurs on other brands
// of web servers.
//
// I'm using .NET Framework version 1.1.4322.573
//
// Also note that, in order to run the DLL inside your browser, you will have
// to go into your Microsoft .NET Framework 1.1 Wizard and increase the level of
// trust for the website/web-page/assembly.

public PleaseTellMe()
{
// CODE EXECUTION STARTS HERE
InitializeComponent(); // Set-up the form on the screen.
InitializeListen(); // Set-up a thread that will listen on port 2112 and display any received bytes.

listBox1.Items.Add("Start..."); // Show some output in the listBox

/////////////////////////////////////////////////////////////////
// This next line of code will take 100 seconds to execute. Why?
// ????? ????? ????? ????? ????? ????? ????? ????? ????? ?????
/////////////////////////////////////////////////////////////////
TcpClient tcpClient=new TcpClient("localhost",2112);

// From this point on, everything runs quickly, as expected.
listBox1.Items.Add("tcpClient initialized...");
NetworkStream ns=tcpClient.GetStream();

// Write the word "Hello" to port 2112
byte[] myText={(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o'};
ns.Write(myText,0,myText.Length);

ns.Close();
tcpClient.Close();
}

private void InitializeListen() {
// Create a new thread which will listen on port 2112 and display any data recieved.
// (This part works perfectly, and is no concern to us).
Thread thread=new Thread(new ThreadStart(Listen));
thread.Start();
}

public void Listen()
{
TcpListener tcpListener=new TcpListener(2112);
tcpListener.Start();
TcpClient tcpClient=tcpListener.AcceptTcpClient();
NetworkStream ns=tcpClient.GetStream();
StreamReader sr=new StreamReader(ns);
string result=sr.ReadToEnd();
listBox1.Items.Add("Read from stream: " + result);
tcpClient.Close();
tcpListener.Stop();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null ) components.Dispose();
}
base.Dispose( disposing );
}

#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(248, 212);
this.listBox1.TabIndex = 0;
//
// PleaseTellMe
//
this.Controls.Add(this.listBox1);
this.Name = "PleaseTellMe";
this.Size = new System.Drawing.Size(264, 240);
this.ResumeLayout(false);

}
#endregion
}
}
 
Hi,

Thanks for your post. I am checking this issue and will update you with my
findings.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Not quite sure if this is of help, But I found that using IPEndPoints help to aleviate a lot of the issues with TcpClient.

<jobelobes>
 
Back
Top