D
Daniel
TcpClient close() method socket leak
when i use TcpClient to open a connection, send data and close the TcpClient
with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket
on the client machine to close per my network app the computer fills up w/
thousands of these
[System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2189 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2190 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2191 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2192 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2193 TIME_WAIT
until the "Only one usage of each socket address" error occures. How to
work around this? I need to open connect, send data, and close it. i can not
share connections in my case i have to open, send and close and have the
close truely close. and i have to do more then 3k of these in 60 seconds. I
know this is possible because I have a c version of the client that uses c
sockets and it works fine and does not fill with thousands of:
[System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
Is this a limitation of TcpClient ? or is there a way to truely close a
TcpClient imediatly? i tried setting linger option false, no delay true,
timeout 0 etc. still no progress the client sockets all get used up w/
thousands of
[System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2189 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2190 TIME_WAIT
until the "Only one usage of each socket address" error occures.
here is what my client code looks like:
TcpClient myclient;
myclient = new TcpClient();
LingerOption lingerOption = new LingerOption (false, 0);
myclient.LingerState = lingerOption;
myclient.NoDelay = true;
myclient.ReceiveTimeout = 0;
myclient.Connect("foobox", 8888);
NetworkStream networkStream ;
networkStream = myclient.GetStream();
StreamWriter streamWriter ;
streamWriter = new StreamWriter(networkStream);
string strData = "";
strData += "helloworld\0";
streamWriter.WriteLine(strData);
streamWriter.Flush();
streamWriter.Close() ;
networkStream.Close();
myclient.Close();
something missing? should i do more then close? i tried shutdown too.. no
progress
here is the dummy server code if it matters:
public static void Main()
{
TcpListener tcpListener = new TcpListener(8082);
tcpListener.Start();
Console.WriteLine("Server Started") ;
while(true)
{
Socket socketForClient = tcpListener.AcceptSocket();
try
{
if(socketForClient.Connected)
{
Console.WriteLine("Client connected");
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamReader streamReader = new StreamReader(networkStream);
string line = streamReader.ReadLine();
streamReader.Close();
Console.WriteLine("Read:" +line);
}
socketForClient.Shutdown(System.Net.Sockets.SocketShutdown.Both);
socketForClient.Close();
Console.WriteLine("Client disconnected");
GC.Collect();
Console.WriteLine("Garbage Collected");
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
}
}
when i use TcpClient to open a connection, send data and close the TcpClient
with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket
on the client machine to close per my network app the computer fills up w/
thousands of these
[System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2189 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2190 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2191 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2192 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2193 TIME_WAIT
until the "Only one usage of each socket address" error occures. How to
work around this? I need to open connect, send data, and close it. i can not
share connections in my case i have to open, send and close and have the
close truely close. and i have to do more then 3k of these in 60 seconds. I
know this is possible because I have a c version of the client that uses c
sockets and it works fine and does not fill with thousands of:
[System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
Is this a limitation of TcpClient ? or is there a way to truely close a
TcpClient imediatly? i tried setting linger option false, no delay true,
timeout 0 etc. still no progress the client sockets all get used up w/
thousands of
[System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2189 TIME_WAIT
[System Process]:0 TCP foobox:8888 localhost:2190 TIME_WAIT
until the "Only one usage of each socket address" error occures.
here is what my client code looks like:
TcpClient myclient;
myclient = new TcpClient();
LingerOption lingerOption = new LingerOption (false, 0);
myclient.LingerState = lingerOption;
myclient.NoDelay = true;
myclient.ReceiveTimeout = 0;
myclient.Connect("foobox", 8888);
NetworkStream networkStream ;
networkStream = myclient.GetStream();
StreamWriter streamWriter ;
streamWriter = new StreamWriter(networkStream);
string strData = "";
strData += "helloworld\0";
streamWriter.WriteLine(strData);
streamWriter.Flush();
streamWriter.Close() ;
networkStream.Close();
myclient.Close();
something missing? should i do more then close? i tried shutdown too.. no
progress
here is the dummy server code if it matters:
public static void Main()
{
TcpListener tcpListener = new TcpListener(8082);
tcpListener.Start();
Console.WriteLine("Server Started") ;
while(true)
{
Socket socketForClient = tcpListener.AcceptSocket();
try
{
if(socketForClient.Connected)
{
Console.WriteLine("Client connected");
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamReader streamReader = new StreamReader(networkStream);
string line = streamReader.ReadLine();
streamReader.Close();
Console.WriteLine("Read:" +line);
}
socketForClient.Shutdown(System.Net.Sockets.SocketShutdown.Both);
socketForClient.Close();
Console.WriteLine("Client disconnected");
GC.Collect();
Console.WriteLine("Garbage Collected");
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
}
}