[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Socket has completed sending and implementing timeouts.

Posted on 2006-05-05
Medium Priority
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
  • 2
  • 2
LVL 10

Accepted Solution

rivusglobal earned 1000 total points
ID: 16616406
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 = state.sb.ToString();
                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

ID: 16617115
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

ID: 16617458
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.

Assisted Solution

Chipzter earned 1000 total points
ID: 16621090
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

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

873 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