Socket Programming - Receive Buffer Length

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danomano
    New Member
    • Nov 2009
    • 2

    Socket Programming - Receive Buffer Length

    The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that.

    So my question in general is how do I know what length to set the buffer? In specific, my question is what causes the 'completed' event in the OnSocketConnect Completed method to fire? Does it fire at end of a packet or does happen at some end of data marker?
    Code:
            public SocketClient(Page pg, Int32 port, Int32 length)
            {
                this.PAGE = pg;
                this.LENGTH = length;
                this.Q = new Queue<byte[]>();
    
                DnsEndPoint endPoint = new DnsEndPoint("111.222.333.444", port);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
                SocketAsyncEventArgs argz = new SocketAsyncEventArgs();
                argz.UserToken = socket;
                argz.RemoteEndPoint = endPoint;
                argz.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketConnectCompleted);
                socket.ConnectAsync(argz);
            }
    
            private void OnSocketConnectCompleted(object sender, SocketAsyncEventArgs e)
            {
                byte[] response = new byte[this.LENGTH];
                e.SetBuffer(response, 0, response.Length);
                e.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketReceive);
                Socket socket = (Socket)e.UserToken;
                socket.ReceiveAsync(e);
            }
    
            private void OnSocketReceive(object sender, SocketAsyncEventArgs e)
            {
                Q.Enqueue(e.Buffer);
    
                //Prepare to receive more data
                Socket socket = (Socket)e.UserToken;
                byte[] response = new byte[this.LENGTH];
                e.SetBuffer(response, 0, response.Length);
                socket.ReceiveAsync(e);
            }
    Last edited by Frinavale; Nov 25 '09, 04:09 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    OnSocketConnect Completed is fired when a connection is established. Data transfer has nothing to do with it

    Comment

    • danomano
      New Member
      • Nov 2009
      • 2

      #3
      Please reread question

      Originally posted by Plater
      OnSocketConnect Completed is fired when a connection is established. Data transfer has nothing to do with it
      Right but I did not ask anything about what causes the OnSocketConnect Completed event to fire. Please reread the question, I asked "what causes the completed' event in the OnSocketConnect Completed method to fire"

      Obviously I am trying to understand how to adjust the incoming buffer to match the incoming data. Please explain the issue if you can. Could you possibly consider answering again?

      I do appreciate the help.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by danomano
        Right but I did not ask anything about what causes the OnSocketConnect Completed event to fire. Please reread the question, I asked "what causes the completed' event in the OnSocketConnect Completed method to fire"
        Edit: ok I see the subtle difference. We got so many people here where english is not their best language, that I just understood it to be a translation issue.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by danomano
          Right but I did not ask anything about what causes the OnSocketConnect Completed event to fire. Please reread the question, I asked "what causes the completed' event in the OnSocketConnect Completed method to fire"

          Obviously I am trying to understand how to adjust the incoming buffer to match the incoming data. Please explain the issue if you can. Could you possibly consider answering again?

          I do appreciate the help.
          The best way to do this would be to append the length of the data being sent to the beginning of the data being sent.

          Add an Integer (or Int16 if you don't want to use the full 32) to the beginning of the data. Read this integer when you first receive a request and then adjust your buffer.

          One thing that you should know is that the data being sent could contain data for more than one "thing" that you are sending over the socket.

          In other words the data that you retrieve in your OnSocketConnect Completed method could be for more than one "thing". You have to know how big the thing is that you're sending or else you won't know when the first thing has finished sending and the next thing begins (and of course you need to adjust your buffer size)

          You can see the problems that I faced with this same issue in this thread: Determining the size of data being set from socket server

          -Frinny

          Comment

          Working...