PC Review


Reply
Thread Tools Rate Thread

networkStream.Close() not working

 
 
=?Utf-8?B?RGF2aWQgUHJlbnRpY2U=?=
Guest
Posts: n/a
 
      3rd Apr 2006
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




 
Reply With Quote
 
 
 
 
Vadym Stetsyak
Guest
Posts: n/a
 
      3rd Apr 2006
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
 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgUHJlbnRpY2U=?=
Guest
Posts: n/a
 
      4th Apr 2006
Fantastic - many thanks for your help. This is a great resource !

Dave

"Vadym Stetsyak" wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do.Cmd Close Not Working bw Microsoft Access Reports 2 17th Nov 2007 09:36 PM
NetworkStream.Close() - Last bit of data not being sent Tom Microsoft Dot NET Framework 1 29th Jun 2007 06:11 PM
knowing when data sent so i can close the streamwriter and networkstream Daniel Microsoft Dot NET 1 12th Feb 2005 08:17 PM
networkStream.Write waits until networkstream.close cmjman Microsoft Dot NET 1 7th Jan 2005 11:13 AM
NetworkStream .Write() method not working every time JeremyH Microsoft Dot NET Framework 2 2nd Apr 2004 10:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:53 PM.