TcpClient close() method socket leak
when i use TcpClient to open a connection, send data and close the TcpClient
with myTcpClientInst ance.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.Linger State = lingerOption;
myclient.NoDela y = true;
myclient.Receiv eTimeout = 0;
myclient.Connec t("foobox", 8888);
NetworkStream networkStream ;
networkStream = myclient.GetStr eam();
StreamWriter streamWriter ;
streamWriter = new StreamWriter(ne tworkStream);
string strData = "";
strData += "helloworld \0";
streamWriter.Wr iteLine(strData );
streamWriter.Fl ush();
streamWriter.Cl ose() ;
networkStream.C lose();
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(808 2);
tcpListener.Sta rt();
Console.WriteLi ne("Server Started") ;
while(true)
{
Socket socketForClient = tcpListener.Acc eptSocket();
try
{
if(socketForCli ent.Connected)
{
Console.WriteLi ne("Client connected");
NetworkStream networkStream = new NetworkStream(s ocketForClient) ;
StreamReader streamReader = new StreamReader(ne tworkStream);
string line = streamReader.Re adLine();
streamReader.Cl ose();
Console.WriteLi ne("Read:" +line);
}
socketForClient .Shutdown(Syste m.Net.Sockets.S ocketShutdown.B oth);
socketForClient .Close();
Console.WriteLi ne("Client disconnected");
GC.Collect();
Console.WriteLi ne("Garbage Collected");
}
catch(Exception e)
{
Console.WriteLi ne(e.ToString() ) ;
}
}
}
when i use TcpClient to open a connection, send data and close the TcpClient
with myTcpClientInst ance.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.Linger State = lingerOption;
myclient.NoDela y = true;
myclient.Receiv eTimeout = 0;
myclient.Connec t("foobox", 8888);
NetworkStream networkStream ;
networkStream = myclient.GetStr eam();
StreamWriter streamWriter ;
streamWriter = new StreamWriter(ne tworkStream);
string strData = "";
strData += "helloworld \0";
streamWriter.Wr iteLine(strData );
streamWriter.Fl ush();
streamWriter.Cl ose() ;
networkStream.C lose();
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(808 2);
tcpListener.Sta rt();
Console.WriteLi ne("Server Started") ;
while(true)
{
Socket socketForClient = tcpListener.Acc eptSocket();
try
{
if(socketForCli ent.Connected)
{
Console.WriteLi ne("Client connected");
NetworkStream networkStream = new NetworkStream(s ocketForClient) ;
StreamReader streamReader = new StreamReader(ne tworkStream);
string line = streamReader.Re adLine();
streamReader.Cl ose();
Console.WriteLi ne("Read:" +line);
}
socketForClient .Shutdown(Syste m.Net.Sockets.S ocketShutdown.B oth);
socketForClient .Close();
Console.WriteLi ne("Client disconnected");
GC.Collect();
Console.WriteLi ne("Garbage Collected");
}
catch(Exception e)
{
Console.WriteLi ne(e.ToString() ) ;
}
}
}
Comment