To find number of bytes available in socket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Praveen Raj V
    New Member
    • Sep 2010
    • 11

    To find number of bytes available in socket

    "socket.Availab le" is not working.

    How would you Determine number of bytes present in a TCP/IP socket before reading those bytes without using socket.Availabl e
    Code:
    void Receive(Socket socket, byte[] buffer, int offset)
            {
                int sockAvail = socket.Available;//the value of socket.Available does not asigned to sockAvail.
                int received = 0;
                do
                {
                    try
                    {
                        received += socket.Receive(buffer, offset + received, sockAvail - received, SocketFlags.None);
                    }
                    catch (Exception e) { Messagebox.Message(e.Message); }
                } while (received < sockAvail);
                MessageBox.Show(socket.Available.ToString());//Here 0 is displayed always
            }
    Is there any fault in tool? suggest me without using socket.Availabl e how do I find number of bytes available in TCP/IP socket.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You are probably checking the value of socket.Availabl e before and data is actually received.
    If you add a System.Threadin g.Thread.Sleep( 10); right before the that line, does sockAvail become a value?

    Comment

    • Praveen Raj V
      New Member
      • Sep 2010
      • 11

      #3
      After adding System.Threadin g.Thread.Sleep( 10);
      the value of socket.Availabl e is assigned to sockAvail
      and the code works fine.

      What is the reason behind it?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Your code executes too fast. The socket object hasn't had the chance to receive any reply bytes before you request them

        Comment

        Working...