473,383 Members | 1,863 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,383 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()) ;
}
}
}
Jul 21 '05 #1
3 9034
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:%2****************@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()) ;
}
}
}

Jul 21 '05 #2
The TIME_WAIT state for the closed socket is normal. Even your 'c' program should
have had sockets in the TIME_WAIT state after they where closed. Some links to
explanations of this:

http://support.microsoft.com/default...b;en-us;137984

"NOTE: It is normal to have a socket in the TIME_WAIT state for a long period of
time. The time is specified in RFC793 as twice the Maximum Segment Lifetime (MSL).
MSL is specified to be 2 minutes. So, a socket could be in a TIME_WAIT state for
as long as 4 minutes. Some systems implement different values (less than 2
minutes) for the MSL"

http://tangentsoft.net/wskfaq/articl...gging-tcp.html

"Problem: Netstat shows lots of sockets in the TIME_WAIT state. What's wrong?"
"Solution: Nothing's wrong. TIME_WAIT is absolutely normal. [clipped] Thus,
TIME_WAIT usually lasts 30-120 seconds"

On the other hand I'm not sure why your would die with 3k of socket outstanding.
That seems a little low although the max number is dependent on OS memory so it
varies from machine to machine.

There is a 'resuse address' option also for sockets but I'm not sure if that would
help.

Mike

--
POST BY: http://www.dotNET.us - Need .NET? Just ask, Please dotNET.us
Jul 21 '05 #3
TIME_WAIT is normal behaviour for proper TCP session closure.
Reference blurb from RFC 793 with explanation as to why.
The dirty way to close it is to use RST TCP packet header flag and kill the
connection instead of shutting it down. This is not something you would
consider to be "proper way"
The reason as to why TCP_WAIT is hanging around for 60 seconds - berkley
implementation of a socket states Maximum Segment Lifetime to be 30 seconds,
hence 2 times that is 60 (read below)

The protocol places no restriction on a particular connection being
used over and over again. A connection is defined by a pair of
sockets. New instances of a connection will be referred to as
incarnations of the connection. The problem that arises from this is
-- "how does the TCP identify duplicate segments from previous
incarnations of the connection?" This problem becomes apparent if the
connection is being opened and closed in quick succession, or if the
connection breaks with loss of memory and is then reestablished.

To avoid confusion we must prevent segments from one incarnation of a
connection from being used while the same sequence numbers may still
be present in the network from an earlier incarnation. We want to
assure this, even if a TCP crashes and loses all knowledge of the
sequence numbers it has been using. When new connections are created,
an initial sequence number (ISN) generator is employed which selects a
new 32 bit ISN. The generator is bound to a (possibly fictitious) 32
bit clock whose low order bit is incremented roughly every 4
microseconds. Thus, the ISN cycles approximately every 4.55 hours.
Since we assume that segments will stay in the network no more than
the Maximum Segment Lifetime (MSL) and that the MSL is less than 4.55
hours we can reasonably assume that ISN's will be unique.
"Daniel" wrote:
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:%2****************@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()) ;
}
}
}


Jul 21 '05 #4

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...
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: 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...
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();
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.