Socket Programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LisaMargret
    New Member
    • Dec 2007
    • 1

    Socket Programming

    Hello I am Developing an API in C#
    which requires me to connect to a server and send and recieve data
    i am able to create a socket
    i need to send a data in the following Format (Protocol Frame)
    <0x03><0x20><LL LL>Data........ ............<0x 04>

    i am able to do everything except the Lenght (LLLL) field which need to be 4 Bytes and should be in Big Endian Format
    i tried doing everything but cudnt do it
    i used host to network order method but didnt knew how to append it to the message i am supposed to send.
    I'll appreciate any help on this issue


    Thanks,
    Lisa
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Sockets just send a stream of bytes.
    Create a byte[] and fill in the byte values?


    Note: I may have LLLL using the wrong << vs >> and it might need to count up not down.
    Code:
    byte[] myarray = new byte[7+LLLL];
    
    myarray[0]=0x03;
    myarray[1]=0x20;
    
    myarray[2]=  (byte)((Int64)(LLLL<<24));
    myarray[3]=  (byte)((Int64)(LLLL<<16));
    myarray[4]=  (byte)((Int64)(LLLL<<8));
    myarray[5]=  (byte)(LLLL)
    
    //fill in the rest with your data
    
    myarray[7+LLLL]=0x04;
    Also, it would probably be much easier if you made this a function that returned a byte[] and took in like a length and some data.

    Comment

    • setiarakesh
      New Member
      • Dec 2007
      • 10

      #3
      Dear Lisa,

      On reciving end

      for the String "<0x03><0x20><L LLL>Data....... .............<0 x04>"

      byte b[];
      byte a[];
      a[0]=b[13];
      a[1]=b[14];
      a[2]=b[15];
      a[3]=b[16];

      Since it is big endian so actual value will be iht string form will be.

      String lengthStr= ConvertintoStri ng(a[3]) + ConvertintoStri ng(a[2]) + ConvertintoStri ng(a[1]) + ConvertintoStri ng(a[0])

      int x= ConvertIntoInte ger(lengthStr)

      This x would give you the length now in interger form.

      Rakesh Setia

      Comment

      Working...