Receiving 3144 bytes TCP/IP socket During debug mode, but during excecution 1024 byte

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

    Receiving 3144 bytes TCP/IP socket During debug mode, but during excecution 1024 byte

    I am Receiving 3144 bytes through TCP/IP socket During debug mode, but during excecution 1024 byte were received
    Why it is happen

    IPEndPoint ipEnd = new IPEndPoint(ipAd d, ipPort);
    Socket socket = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am, ProtocolType.Tc p);

    socket.Connect( ipEnd);
    byte[] sendData = Encoding.ASCII. GetBytes("msg") ;
    socket.Send(sen dData);

    int byteLen = 4096;
    byte[] RecData = new byte[byteLen];
    DataLen = socket.Receive( RecData, 0, RecData.Length, SocketFlags.Non e);
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Raj,

    You need to build a loop around your call to Receive.

    Since you have no idea of how many bytes make it through in a network packet, you'll have to wait for the whole message to come through.

    This is basically pseudo-code/java combined. Hopefully it'll help.
    Code:
    boolean msgComplete = false;
    int start = 0;
    while (! msgComplete)
    {
      int input = socket.Receive(RecData, start, (RecData.Length-start), SocketFlags.None);
      if (input = -1)
        throw new Exception("socket disconnect");
      else if (<message is not complete>)
        start += input;
      else
        msgComplete = true;
    }

    Comment

    • Praveen Raj V
      New Member
      • Sep 2010
      • 11

      #3
      yes, it works good. Without using socket.Availabl e how would we find available message(data) in the socket.

      boolean msgComplete = false;
      int start = 0;
      while (! msgComplete)
      {
      int input = socket.Receive( RecData, start, (RecData.Length-start), SocketFlags.Non e);
      if (input = -1)
      throw new Exception("sock et disconnect");
      else if (socket.Availab le != 0)
      start += input;
      else
      msgComplete = true;
      }

      Comment

      • Praveen Raj V
        New Member
        • Sep 2010
        • 11

        #4
        How would the available bytes in the socket could be found without using "socket.Availab le".

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          What messaging structure are you using?

          Are you using a message structure, where the first four bytes are the message length?

          Or, are you using a message structure where each record is terminated by a sentinel character, like '\n'?

          You sent the message, so you should know its structure. The whole point of the test I penciled in is that you have to provide that test.
          Code:
            else if (<message is not complete>)
          It's possible that the test code becomes
          Code:
            else if (isMessageComplete(RecData, (start+input)))
          I don't know your messaging protocol, so I can't make any stronger suggestions.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            By default the buffer is 1024. (Think of it as a "maximum block size") So the most you could read in a single read is 1024. (This is not always true, just common in .NET)
            You should always be checking how many bytes you get from read/write operation and ensuring they are all read/written

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              @Plater,

              I think Paraveen Raj V's issue is determining when a complete message is recieved. Without knowing what his communication protocol is, we can talk until we're blue in the face about blocking and such.

              From my perspective, the current issues are:
              1) Message Structure
              and
              2) Synchronous versus Asynchronous Messaging

              Without answering those questions, just grabbing bytes from the incoming data stream until "no more are available" is pointless.

              Comment

              • Praveen Raj V
                New Member
                • Sep 2010
                • 11

                #8
                I am using Synchronous Messaging. On using System.Threadin g.Thread.Sleep( 100);
                before the function call, it works properly and good.

                Why it happens? Why should we use System.Threadin g.Thread.Sleep( 100); before function call

                Iam sending a byte of messaage to server and then receiving 3144 bytes from server.
                How would you find that the message is completely read or not.

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  Ok, that's the basic protocol.

                  Does the inquiry byte carry any meaning, other than its existance?

                  Is the response length always known to be 3144 bytes, or can it vary?

                  If really all you care about is 3144 bytes, because the record if fixed length, then use something like the following:

                  Code:
                  int in;
                  for(int siz=0; siz < 3144; siz+=in) { 
                    in = socket.Receive(RecData, siz, (RecData.Length-siz), SocketFlags.None); 
                    if (in = -1) throw new Exception("socket disconnect"); 
                  }
                  Last edited by Oralloy; Sep 28 '10, 11:02 PM. Reason: Added crude example.

                  Comment

                  • Praveen Raj V
                    New Member
                    • Sep 2010
                    • 11

                    #10
                    Yes, the response length won't be same, it varies depending upon the data that I send. Hence we need to code it dynamic. The fixed length can't be used.
                    I don't know why it requires Thread.Sleep() method.

                    Comment

                    • Oralloy
                      Recognized Expert Contributor
                      • Jun 2010
                      • 988

                      #11
                      So, assume that cmd is the command byte you sent

                      Code:
                      int expct = -1;
                      if (cmd = 0x01) expct = 3000;
                      if (cmd = 0x02) expct = 4001;
                      if (cmd = 0x03) expct = 3144;
                      if (expct = -1) throw new Error("unknowed cmd");
                      
                      int in; 
                      for(int siz=0; siz < expct; siz+=in) {  
                        in = socket.Receive(RecData, siz, (RecData.Length-siz), SocketFlags.None);  
                        if (in = -1) throw new Exception("socket disconnect");  
                      }

                      Comment

                      • Praveen Raj V
                        New Member
                        • Sep 2010
                        • 11

                        #12
                        OK, How would you expect exact amount of bytes that your going to receive. Here I am receiving XML data and I am doing parsing over the XML data. Incase the received bytes goes more than one you expected, XML parsing would not happen, it through an exception.(ie I am telling about the XML tag)
                        The data inside the XML tags may varies. If that happen then your expectation would go wrong. As in your code you read only 3144 bytes for cmd = 0x03.If you getting more than 3144 what happens. For me the application freezes because it have been blocked to get remaining bytes.
                        so, I think that expecting a certain amount of bytes would not be dynamic.

                        Comment

                        • Oralloy
                          Recognized Expert Contributor
                          • Jun 2010
                          • 988

                          #13
                          Ouch!

                          You don't have a well defined protocol at all.

                          You have several options at this point - object streams, use a record terminator, or a send a record-length field of some sort.

                          Which do you prefer to do?

                          Comment

                          Working...