networkStream.Close() not working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a TCP/IP service.

Server side connections are accepted asynchronously:

//code from AsyncCallback passed to beginAccept
Socket ClientSocket = _AcceptorSocket.EndAccept(ar);

//I then wrap the socket in a network stream:
Stream s = new NetworkStream(ClientSocket);

//I then issue async reads to the stream
s.BeginRead(InBuffer, ByteOffset, Length, new AsyncCallback(OnReadComplete),
InBuffer);

When I come to stop the service, I close the listening socket and itterate
through my collection of NetworkStreams calling

s.Close();
s.Dispose();

Unfortunately the underlying sockets are not actually being closed (i.e. the
clients are not disconnected and netstat reports that the underlying TCP
connections still exist.).

Any ideas?

Dave
 
Hello, David!

DP> Unfortunately the underlying sockets are not actually being closed
DP> (i.e. the clients are not disconnected and netstat reports that the
DP> underlying TCP connections still exist.).

DP> Any ideas?

It seems to me that you should use other NetworkStream constructor
Stream s = new NetworkStream(ClientSocket, true);
Second parameter specifies if the stream owns the socket. And if you want to close socket from the stream, you must specify that stream as socket owner...

ps. there is no need calling dispose manually, Close() will be sufficient ( it calls Dispose internally )
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Back
Top