Calling Close on a System.Net.Sockets.TcpClient. Connection remains open

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

When i close a client connection by using
System.Net.Sockets.TcpClient.Close() method it remains open and viewable
with netstat -an.
public void Disconnect()

{

if (myClient == null) return;

myClient.Close();

myClient = null;

}

Tested with the above code.

Why is this please? Or should I be doing something else too?

thanks
 
System.Net.Sockets.TcpClient.Close() method it remains open and viewable
with netstat -an.

How open?

If it's in CLOSE_WAIT for example, the socket is closed but the OS keeps
it "reserved" for a while to avoid conflicts.

Greetings,
Wessel
 
try

{

myClient = new System.Net.Sockets.TcpClient(Address, PortNum);

myClient.NoDelay = true;

myStream = myClient.GetStream();

LogDebug(fnName, "Success");

InitializedOK = true;

}

catch(Exception e)

{

WinsockError = e.Message;

LogDebug(fnName, "Failed");

LogException(fnName, e);

}
 
myClient = new System.Net.Sockets.TcpClient(Address, PortNum);Sorry, I was not making myself clear.

If you use the "netstat -an" command, it will show you the state of the
socket. I was wondering wether that state might be WAIT_CLOSE.

Greetings,
Wessel
 
No, im looping back to 127.0.0.1 for client/server
server says LISTENING and client says ESTABLISHED

Still so after several minutes after going for a walk across room to bash
head against the wall.
;P
 
No, im looping back to 127.0.0.1 for client/server
server says LISTENING and client says ESTABLISHED

Still so after several minutes after going for a walk across room to bash
head against the wall.
;P
If you post a working sample I can see if I can reproduce the problem.
The fragments you posted so far don't contain enough information tho.

Greetings,
Wessel
 
Ive discovered the cure.
One needs to close the associated networkstream rather than the TCPClient
itself. Closing the stream automatically closes the socket.
A bit confusing to document it as if it were an option rather than
mandatory.

thanks for your help Wessel
:-)
 
Back
Top