Socket has completed sending and implementing timeouts.

Posted on 2006-05-05
Last Modified: 2010-04-16
Hi Experts,
I would like to know how to find out if the socket has completed sending the data.
I am using Socket.Send(bytes) to send data and this send method returns int, that specifies number of bytes sent....How Can I be sure if all the data has been sent?
What I need to do is
1. We want to start a timeout only after we finish sending the entire data. We can use setsocket option ,send time out to acheive a send timeout but
We are using asynchrous receive, and we would like to only wait for say 60 secs to receive anything from the client.
What is teh best way to accomplish this?

Question by:Umamahez
    LVL 10

    Accepted Solution

    You could use an End of file tag to specify blocks of data. When using <EOF>, you can test to see if all the data has been sent such as,

                // Read data from the client socket.
                int bytesRead = handler.EndReceive(ar);

                if (bytesRead > 0)
                    // There  might be more data, so store the data received so far.
                        state.buffer, 0, bytesRead));

                    // Check for end-of-file tag. If it is not there, read
                    // more data.
                    content =;
                    if (content.IndexOf("<EOF>") > -1)
                        // All the data has been read from the
                        // client. Display it on the console.
                        Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                            content.Length, content);
                        // Echo the data back to the client.
                        Send(handler, content);
                        // Not all data received. Get more.
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReadCallback), state);

    I've used the example from the MSDN Asynchronous Server Socket example. Check it out at:

    Hope this helps. Regards,


    Author Comment

    Hi  Owen,
    Thank you so much for a quick response but I  was looking for something along the lines of send complete event , of tcpSocket in VB 6.0.
    In order to use you rexample, the client must send <EOF> tag to specify end of file, unfortunately the client will not be sending us anything like that , so I was looking for some kind of socket property or method to signal taht it has sent all the data.
    Please help.
    LVL 10

    Expert Comment

    If there is a SendComplete Event similar to the one in VB 6.0 available, i'm not aware of it.

    I'm not sure what type of data your sending, but there must be some test you can do to make sure it's completed. As a suggestion , you could attach a couple of bytes "a header" at the beginning of each data block containing the total length.
    LVL 3

    Assisted Solution

    Have you tried using BeginSend() instead of Send()? Then you can specify an AsyncCallback that will be called with an IAsyncResult object argument which you can check the IsCompleted property of. Or if you prefer polling or just need to check periodically you could just store the IAsyncResult that is returned by the BeginSend() method and check its IsCompleted property when needed.

    Author Comment

    Thanks for replying to my query. Thats why I gave points.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now