Home > .NET, .NET 2.0, C# 2.0 > The underlying connection was closed.

The underlying connection was closed.

December 20, 2006

There seems to be a big problem with the underlying code behind the sockets classes that Microsoft uses in their .NET framework. Many people are getting a ?The underlying connection was closed.? error message, but there doesn?t seem to be any consistency to it. I sent a simple test program using the code below to a friend in Singapore and he gets the error. Well, I decided to create a very simple Socket program to see if he gets the error with it as well, and sure enough he does. His Windows XP computer is fully patched too. Some people are getting this error when connecting to a web service, and they have found a way to get around it most of the time, but the error still comes back from time to time even with that patch. This however, isn?t a web service, just a simple WebClient piece of code that connects to a web server.

Microsoft has yet to release anything on this.

WebClient Class

a_url = ?http://www.google.com?;
System.Net.WebClient WC = new System.Net.WebClient();
data = WC.DownloadData(a_url);

The follow socket class also throws the error, not with me, but with some people that I send it too. It seems to be random.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
public class clnt
{
public clnt()
{
}
public string TestCon(string command)
{
string strx;
strx = ?Test?;
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine(?Connecting?..?);
tcpclnt.Connect(?www.joelcochran.com?, 80); // use the ipaddress as in the server program
Console.WriteLine(?Connected?);
// Console.Write(?Enter the string to be transmitted : ?);
String str = command;
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
strx= strx + Convert.ToChar(bb[i]);
tcpclnt.Close();
return strx;
}
catch (Exception e)
{
return "Error….. " + e.StackTrace;
}
}
}
Advertisements
Categories: .NET, .NET 2.0, C# 2.0
%d bloggers like this: