<p>Despite the documentation, NetworkStream.Write does not appear to wait until the data has been sent. Instead, it waits until the data has been copied to a buffer and then returns. That buffer is transmitted in the background.</p>

<p>This is the code I have at the moment. Whether I use ns.Write or ns.BeginWrite doesn't matter - both return immediately. The EndWrite also returns immediately (which makes sense since it is writing to the send buffer, not writing to the network).</p>

<pre><code>    bool done;
    void SendData(TcpClient tcp, byte[] data)
    {
        NetworkStream ns = tcp.GetStream();
        done = false;
        ns.BeginWrite(bytWriteBuffer, 0, data.Length, myWriteCallBack, ns);
        while (done == false) Thread.Sleep(10);
    }
   
    public void myWriteCallBack(IAsyncResult ar)
    {
        NetworkStream ns = (NetworkStream)ar.AsyncState;
        ns.EndWrite(ar);
        done = true;
    }
</code></pre>

<p>How can I tell when the data has actually been sent to the client?</p>

<p>I want to wait for 10 seconds(for example) for a response from the server after sending my data otherwise I'll assume something was wrong. If it takes 15 seconds to send my data, then it will always timeout since I can only start counting from when NetworkStream.Write returns - which is before the data has been sent. I want to start counting 10 seconds from when the data has left my network card.</p>

<p>The amount of data and the time to send it could vary - it could take 1 second to send it, it could take 10 seconds to send it, it could take a minute to send it. The server does send an response when it has received the data (it's a smtp server), but I don't want to wait forever if my data was malformed and the response will never come, which is why I need to know if I'm waiting for the data to be sent, or if I'm waiting for the server to respond.</p>

<p>I might want to show the status to the user - I'd like to show "sending data to server", and "waiting for response from server" - how could I do that?</p>

[{'answer_id': 67773, 'author': 'palehorse', 'author_id': 312, 'author_profile': 'https://Stackoverflow.com/users/312', 'pm_score': 0, 'selected': False, 'text': '<p>How about using the Flush() method.</p>\n\n<pre><code>ns.Flush()\n</code></pre>\n\n<p>That should ensure the data is written before continuing.</p>\n'}, {'answer_id': 67804, 'author': 'GEOCHET', 'author_id': 5640, 'author_profile': 'https://Stackoverflow.com/users/5640', 'pm_score': 1, 'selected': False, 'text': '<p>In general, I would recommend sending an acknowledgment from the client anyway. That way you can be 100% sure the data was received, and received correctly.</p>\n'}, {'answer_id': 67825, 'author': 'Craig Eddy', 'author_id': 5557, 'author_profile': 'https://Stackoverflow.com/users/5557', 'pm_score': 1, 'selected': False, 'text': "<p>If I had to guess, the NetworkStream considers the data to have been sent once it hands the buffer off to the Windows Socket. So, I'm not sure there's a way to accomplish what you want via TcpClient.</p>\n"}, {'answer_id': 67856, 'author': 'sbeskur', 'author_id': 10446, 'author_profile': 'https://Stackoverflow.com/users/10446', 'pm_score': -1, 'selected': False, 'text': '<p>Perhaps try setting\n<code>tcp.NoDelay = true</code></p>\n'}, {'answer_id': 67944, 'author': 'Greg Cottman', 'author_id': 10496, 'author_profile': 'https://Stackoverflow.com/users/10496', 'pm_score': 3, 'selected': False, 'text': '<p>TCP is a "reliable" protocol, which means the data will be received at the other end if there are no socket errors.  I have seen numerous efforts at second-guessing TCP with a higher level application confirmation, but IMHO this is usually a waste of time and bandwidth.</p>\n\n<p>Typically the problem you describe is handled through normal client/server design, which in its simplest form goes like this...</p>\n\n<p>The client sends a request to the server and does a blocking read on the socket waiting for some kind of response.  If there is a problem with the TCP connection then that read will abort.  The client should also use a timeout to detect any non-network related issue with the server.  If the request fails or times out then the client can retry, report an error, etc.</p>\n\n<p>Once the server has processed the request and sent the response it usually no longer cares what happens - even if the socket goes away during the transaction - because it is up to the client to initiate any further interaction.  Personally, I find it very comforting to be the server.  :-)</p>\n'}, {'answer_id': 68166, 'author': 'Alex Lyman', 'author_id': 5897, 'author_profile': 'https://Stackoverflow.com/users/5897', 'pm_score': 1, 'selected': False, 'text': "<p>I can not think of a scenario where NetworkStream.Write wouldn't send the data to the server as soon as possible.  Barring massive network congestion or disconnection, it should end up on the other end within a reasonable time.  Is it possible that you have a protocol issue?  For instance, with HTTP the request headers must end with a blank line, and the server will not send any response until one occurs -- does the protocol in use have a similar end-of-message characteristic?</p>\n\n<p>Here's some cleaner code than your original version, removing the delegate, field, and Thread.Sleep.  It preforms the exact same way functionally.</p>\n\n<pre><code>void SendData(TcpClient tcp, byte[] data) {\n    NetworkStream ns = tcp.GetStream();\n    // BUG?: should bytWriteBuffer == data?\n    IAsyncResult r = ns.BeginWrite(bytWriteBuffer, 0, data.Length, null, null);\n    r.AsyncWaitHandle.WaitOne();\n    ns.EndWrite(r);\n}\n</code></pre>\n\n<p>Looks like the question was modified while I wrote the above.  The .WaitOne() may help your timeout issue.  It can be passed a timeout parameter.  This is a lazy wait -- the thread will not be scheduled again until the result is finished, or the timeout expires.</p>\n"}, {'answer_id': 69681, 'author': 'Lex Li', 'author_id': 11182, 'author_profile': 'https://Stackoverflow.com/users/11182', 'pm_score': 1, 'selected': False, 'text': '<p>I try to understand the intent of .NET NetworkStream designers, and they must design it this way. After Write, the data to send are no longer handled by .NET. Therefore, it is reasonable that Write returns immediately (and the data will be sent out from NIC some time soon).</p>\n\n<p>So in your application design, you should follow this pattern other than trying to make it working your way. For example, use a longer time out before received any data from the NetworkStream can compensate the time consumed before your command leaving the NIC.</p>\n\n<p>In all, it is bad practice to hard code a timeout value inside source files. If the timeout value is configurable at runtime, everything should work fine.</p>\n'}, {'answer_id': 79676, 'author': 'Glyph', 'author_id': 13564, 'author_profile': 'https://Stackoverflow.com/users/13564', 'pm_score': 4, 'selected': False, 'text': '<p>I\'m not a C# programmer, but the way you\'ve asked this question is slightly misleading.  The only way to know when your data has been "received", for any useful definition of "received", is to have a specific acknowledgment message in your protocol which indicates the data has been fully processed.</p>\n\n<p>The data does not "leave" your network card, exactly.  The best way to think of your program\'s relationship to the network is:</p>\n\n<blockquote>\n  <p>your program -> lots of confusing stuff -> the peer program</p>\n</blockquote>\n\n<p>A list of things that might be in the "lots of confusing stuff":</p>\n\n<ul>\n<li>the CLR</li>\n<li>the operating system kernel</li>\n<li>a virtualized network interface</li>\n<li>a switch</li>\n<li>a software firewall</li>\n<li>a hardware firewall</li>\n<li>a router performing network address translation</li>\n<li>a router on the peer\'s end performing network address translation</li>\n</ul>\n\n<p>So, if you are on a virtual machine, which is hosted under a different operating system, that has a software firewall which is controlling the virtual machine\'s network behavior - when has the data "really" left your network card?  Even in the best case scenario, many of these components may drop a packet, which your network card will need to re-transmit.  Has it "left" your network card when the first (unsuccessful) attempt has been made?  Most networking APIs would say no, it hasn\'t been "sent" until the other end has sent a TCP acknowledgement.</p>\n\n<p>That said, <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.write.aspx" rel="noreferrer">the documentation for NetworkStream.Write</a> seems to indicate that it will not return until it has at least initiated the \'send\' operation:</p>\n\n<blockquote>\n  <p>The Write method blocks until the requested number of bytes is sent or a SocketException is thrown.</p>\n</blockquote>\n\n<p>Of course, "is sent" is somewhat vague for the reasons I gave above.  There\'s also the possibility that the data will be "really" sent by your program and received by the peer program, but the peer will crash or otherwise not actually process the data.  So you should do a <code>Write</code> followed by a <code>Read</code> of a message that will only be emitted by your peer when it has actually processed the message.</p>\n'}, {'answer_id': 707581, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Bellow .net is windows sockets which use TCP.\nTCP uses ACK packets to notify the sender the data has been transferred successfully. \nSo the sender machine knows when data has been transferred but there is no way (that I am aware of) to get that information in .net.</p>\n\n<p>edit:\nJust an idea, never tried:\nWrite() blocks only if sockets buffer is full. So if we lower that buffers size (SendBufferSize) to a very low value (8? 1? 0?) we may get what we want :)</p>\n'}]
['https://Stackoverflow.com/questions/67761', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4495/']