473,800 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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() ) ;
}
}
}
Nov 16 '05 #1
2 10775
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.c om> wrote in message
news:OI******** ******@TK2MSFTN GP09.phx.gbl...
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() ) ;
}
}
}

Nov 16 '05 #2
This is not a .Net issue. This is just how TCP/IP works. Your seeing two
issues

(1) TcpTimedWaitDel ay - This can be adjusted..

TcpTimedWaitDel ay
Key: Tcpip\Parameter s
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.c om> wrote in message
news:Oc******** *******@TK2MSFT NGP15.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.c om> wrote in message
news:OI******** ******@TK2MSFTN GP09.phx.gbl...
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() ) ;
}
}
}


Nov 16 '05 #3

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

Similar topics

4
4380
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 sequence with a server: connect write(50 bytes) read(2002 bytes) write(50 bytes) read(2002 bytes)
3
9115
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 the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP foobox:8888 localhost:2189 TIME_WAIT :0 TCP foobox:8888 localhost:2190 TIME_WAIT
7
7361
by: user | last post by:
Hello How can i read IP of incoming connection when i have TcpClient for that connection ? Thanx
0
1701
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 sending data. It is sending a web page but it doesn't use the Content-Length header to indicate the size. While I can use the DataAvailable property, it is well known that you cannot rely on this to know when no more data is available. What I...
3
28656
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 slowly, about 4 seconds between send and receive. In our production environment with hundreds of transactions it was truly costly. a while ago i changed de TcpClient object. Now i am using a Socket (System.Net.Sockets.Socket) object and it...
1
4541
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 closes the connection so I can exit my listener code. Here is what I have tried. First I asked MSDN and they said..
3
15533
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
1416
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 stream connection tcpClnt.EndConnect(Stm)>>> Error !!
4
20467
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 server response is received, the GetStream method will return will return other than wait forever. thanks in advance,
6
3595
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 fails by whatever reason. I tried to get the same results with Kamaelia's TCPClient component. If I start up the component and try to connect to a closed TCP port it fails and sends a message out of the signal box, that's okay.
0
9695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9555
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10514
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10287
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10042
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.