473,325 Members | 2,828 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,325 software developers and data experts.

Close() timeout in NetworkStream and Socket?

I have an application that wants to open a Socket, write data and close the
socket. A persistent connection would be nice, but it's intended to
operate in an environment where the network connection probably isn't
reliable.

So I do this:

// open socket
m_networkSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint them = new IPEndPoint(IPAddress.Parse(hostname), port);
m_networkSocket.Connect(them);
m_networkConnection = new NetworkStream(m_networkSocket, false);

// write
m_networkConnection.Write(arrayOfBytes, 0, arrayOfBytes.Length);
m_networkConnection.Flush();

// close
m_networkConnection.Flush();
m_networkConnection.Close();
m_networkSocket.Close();
All fine & good, but nothing shows up on the other side. If I put a delay
between Flush() and Close() it suddenly starts to work.

So is Flush() is a no-op?

Close() (for both Socket and NetworkStream) has an optional "timeout" value
(an Int32), which is 'the amount of time to wait for data to be sent'. I
_assume_ that's milliseconds, but does anyone know for sure?

That begs the question, however, of just how long do I wait? How do I know
my data has been sent and it's safe for me to close the connection?

Thanks!

al

--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131
Aug 27 '08 #1
3 4056
On Wed, 27 Aug 2008 12:16:19 -0700, A. W. Dunstan <no@spam.thankswrote:
[...]
All fine & good, but nothing shows up on the other side. If I put a
delay
between Flush() and Close() it suddenly starts to work.

So is Flush() is a no-op?

Close() (for both Socket and NetworkStream) has an optional "timeout"
value
(an Int32), which is 'the amount of time to wait for data to be sent'. I
_assume_ that's milliseconds, but does anyone know for sure?
From the MSDN documentation: "Wait up to timeout seconds to send any
remaining data, then close the socket". But that's not really what you
want.
That begs the question, however, of just how long do I wait? How do I
know
my data has been sent and it's safe for me to close the connection?
The correct way to manage the connection is to call
Socket.Shutdown(SocketShutdown.Send) when you're done sending data, and
then call Socket.Receive() until you get return value of 0.

I would not expect Stream.Flush() to do much of anything. The Socket
class is likely to be passing data off to the network driver as fast as it
can. Any delays in transmission are likely due to the Nagle algorithm,
which is responsible for coalescing data sent on a TCP connection, to
ensure efficient use of the connection.

Most likely what was happening in your example is that the socket was
being closed before the network driver got around to sending the data (it
was waiting a few hundred ms to see if you sent any more that it could
combine with what you'd already sent), aborting the transfer. There are
different ways to deal with this, but the best way is to manage the
connection in the expected way.

Pete
Aug 27 '08 #2
Peter Duniho wrote:
>Close() (for both Socket and NetworkStream) has an optional "timeout"
value
(an Int32), which is 'the amount of time to wait for data to be sent'. I
_assume_ that's milliseconds, but does anyone know for sure?

From the MSDN documentation: "Wait up to timeout seconds to send any
remaining data, then close the socket". But that's not really what you
want.
Ah - I'd been relying on the Visual Studio online help which made no mention
of units.
>That begs the question, however, of just how long do I wait? How do I
know
my data has been sent and it's safe for me to close the connection?

The correct way to manage the connection is to call
Socket.Shutdown(SocketShutdown.Send) when you're done sending data, and
then call Socket.Receive() until you get return value of 0.

I would not expect Stream.Flush() to do much of anything. The Socket
class is likely to be passing data off to the network driver as fast as it
can. Any delays in transmission are likely due to the Nagle algorithm,
which is responsible for coalescing data sent on a TCP connection, to
ensure efficient use of the connection.

Most likely what was happening in your example is that the socket was
being closed before the network driver got around to sending the data (it
was waiting a few hundred ms to see if you sent any more that it could
combine with what you'd already sent), aborting the transfer. There are
different ways to deal with this, but the best way is to manage the
connection in the expected way.
Thanks! I'll give that a try shortly.

My transmission is purely one-way - send only. After calling Shutdown() do
I still need to call Receive()? Or is Shutdown() followed by Close()
sufficient to tell the socket "I'm done sending - finish up"?

--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131
Aug 28 '08 #3
On Thu, 28 Aug 2008 07:05:10 -0700, A. W. Dunstan <no@spam.thankswrote:
[...]
My transmission is purely one-way - send only. After calling Shutdown()
do
I still need to call Receive()? Or is Shutdown() followed by Close()
sufficient to tell the socket "I'm done sending - finish up"?
No, you need to call Receive(). As an alternative, you can set the
DontLinger socket option to false and specify a non-zero timeout (see
Socket.SetSocketOption()). But even in that case, if the timeout is
exceeded, transmission of the data will be aborted. To ensure that your
end has done everything it can to send the data, you need to call
Receive() and wait for the 0 byte return value.

That assumes, of course, that you want that behavior. For some
applications, a timeout in completing the send is appropriate, and you
would in fact be better off with the DontLinger approach. It just depends
on what you want your network i/o to do.

Pete
Aug 28 '08 #4

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

Similar topics

7
by: drs | last post by:
Hi, I have a program which opens a socket server in a thread. I need for the server to listen on the socket for a certain amount of time (say, ten seconds or so) and then close it, and am...
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...
0
by: Steve - DND | last post by:
We are continually receiving timeout, and "Unable to write data to the transport connection" errors while using the System.Net.HttpWebRequest class from an ASP.Net web page. Below are the two...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
8
by: Claire | last post by:
I'm trying to debug my network application ie I want to check my error handling when the connection is broken. Im using 127.0.0.1 as the connection address. Unfortunately, the client socket goes...
3
by: Helge Jensen | last post by:
I am implementing a protocol which transmits messages. The messages are most naturally transferred using the a Stream so the protocol can communicate over serial-ports, network links, .... If...
4
by: Haim | last post by:
it is very strange for me that a simple event of closing socket that was in the the winsock object of vb6 , i didn't found yet in the vb.net the only way i found is to try to send something to...
0
by: uneasyrider | last post by:
OK I'm about to pull my hair out on this one. I've got a socket created that sends a network stream of data from a listview to an open port. It works great!! I'm getting ACK's back from the server...
2
by: Zytan | last post by:
I just had the problem occur again, with NetworkStream.Write() doing its thing with a timeout... and it just sits and waits and waits and waits... it never times outs. So, I shut the server down...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.