473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

TcpClient close() method socket leak

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.Socket Shutdown.Both);
socketForClient.Close();
Console.WriteLine("Client disconnected");
GC.Collect();
Console.WriteLine("Garbage Collected");
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
}
}
Nov 16 '05 #1
2 10739
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" <so*******************@yahoo.com> wrote in message
news:OI**************@TK2MSFTNGP09.phx.gbl...
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.Socket Shutdown.Both);
socketForClient.Close();
Console.WriteLine("Client disconnected");
GC.Collect();
Console.WriteLine("Garbage Collected");
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
}
}

Nov 16 '05 #2
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" <so*******************@yahoo.com> wrote in message
news:Oc***************@TK2MSFTNGP15.phx.gbl...
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" <so*******************@yahoo.com> wrote in message
news:OI**************@TK2MSFTNGP09.phx.gbl...
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.Socket Shutdown.Both);
socketForClient.Close();
Console.WriteLine("Client disconnected");
GC.Collect();
Console.WriteLine("Garbage Collected");
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
}
}


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher H. Laco | last post by:
I'm having a problem with the TcpClient that I can only conclude is either a feature, or a complete misunderstanding of the docs on my part. In a nutshell, I'm simply performing the following...
3
by: Daniel | last post by:
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...
7
by: user | last post by:
Hello How can i read IP of incoming connection when i have TcpClient for that connection ? Thanx
0
by: Johann Blake | last post by:
I am using the TcpClient to connect to a web site. I then open a NetworkStream and read the contents that are being sent back. The problem is that I have no idea when the remote host is finished...
3
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
1
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
3
by: Ryan Liu | last post by:
Hi, I use Server: Use an endless thread to lisiten to clients requests: while(true) { TcpClient client = myListener.AcceptTcpClient();
1
by: Jordi | last post by:
Hi, I have my socket class using tcpClient framework 2.0... The socket is running perfectly but I've a problem for close the socket connection with the server! I try with this: Stm is the...
4
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am wondering if I am using TCPClient class in C#, how to setup timeout value? Timeout I mean, when connects to server for the 1st time, and during <timeoutinterval, if no...
6
by: Bjoern Schliessmann | last post by:
Hello, I'm currently trying to implement a simulation program with Kamaelia and need a reliable TCP connection to a data server. From Twisted, I know that a method is called if the connection...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.