TcpClient close() method socket leak

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()) ;
}
}
}
 
D

Daniel

also, even after the client executable is unloaded from memory the ever evil
socket entries remain for 60 seconds:
[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

is it that i can only do 3000 open, send, close in every 60 seconds per a
limitation w/ the TcpClient or is there something else i could try?

Daniel said:
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()) ;
}
}
}
 
A

Amy L.

This is not a .Net issue. This is just how TCP/IP works. Your seeing two
issues

(1) TcpTimedWaitDelay - This can be adjusted..

TcpTimedWaitDelay
Key: Tcpip\Parameters
Value Type: REG_DWORD - Time in seconds
Valid Range: 30-300 (decimal)
Default: 0xF0 (240 decimal)
Description: This parameter determines the time that a connection stays in
the TIME_WAIT state when it is closing. While a connection is in the
TIME_WAIT state, the socket pair cannot be re-used. This is also known as
the "2MSL" state. According to RFC, the value should be two times the
maximum segment lifetime on the network. See RFC793 for more details.

(2) Limit of client side ports under Windows -
http://support.microsoft.com/kb/q196271/

Amy..

Daniel said:
also, even after the client executable is unloaded from memory the ever evil
socket entries remain for 60 seconds:
[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

is it that i can only do 3000 open, send, close in every 60 seconds per a
limitation w/ the TcpClient or is there something else i could try?

Daniel said:
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()) ;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top