Thread: Question about sending a "packet" over sockets

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    Question about sending a "packet" over sockets

    Hi I have a socket connection already set up, my thing is what would be the best way to send a packet through sockets? My teacher wants us to convert the packet to bits first before we send it.

    I think he wants something like this:

    Code:
    struct packet{
    
    int header
    int message
    int flag
    
    }clientpacket;
    my question is how do we covert this to bits, before sending it. One of my friends said we can create a char[] array the size of the struct as a buffer. will memcpy() change the struct to bits if I copy it to the buffer?

    this is how is suppose to look in bits for the header field/column.

    Field : header
    Field size(bits): 8
    data format: Unsigned int
    value example: 1
    Value in bits: 0000 0001


    I am confused, I think I send a structure over the socket but I did not convert it to bits before I sent it so I still need ideas, and I have look everywhere but I could not find anything useful.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well it already is 'bits' before you do anything.

    What I think your teacher is looking for is something called serialisation. This removes all the 'struct' from the data and leaves you with pure data.

    Using a char buffer is one way.

    For example
    char buff[ sizeof(int)*3 ];

    memcpy( &buff[0], &clientpacket.header, sizeof(int) );
    // etc



    Ideally though, you use the htons / htonl functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This removes all the 'struct' from the data and leaves you with pure data.
    O_O

    That is easily the best jargonless description for serialisation I've ever heard.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Salem thanks for the help. so I will need to copy each structure element at a time, not the whole structure using memcpy().

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by newbc View Post
    Salem thanks for the help. so I will need to copy each structure element at a time, not the whole structure using memcpy().
    I don't see why you can't copy the whole struct using memcpy

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    In your first post you mentioned that the size would be eight bits for the header field. Assuming you're referring to the packed size and that CHAR_BIT is 8, I think you want the following:
    Code:
    unsigned char buf[] = { clientpacket.header, clientpacket.message, clientpacket.flag };
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    okay follow up on my question. So I am on the server side and I am trying to convert the char pbuffer to host byte order

    Code:
    pbuffer[20] = ntohl(pbuffer[20]);
    
    			
    			if (sendto(sockfd, pbuffer, sizeof(pbuffer), 0, (struct sockaddr *)&cli_addr, cli_len) < 0) {
    	perror("sendto failed");
    	//return 0;
    	}
    I keep getting expecting expression before ')' token. bpuffer is the char array I created data from my packet structure.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As a complete guess, make sure you are #include'ing <socket.h> in this file so that the compiler knows what a "struct sockaddr" is.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    okay so I am trying this code. It is suppose to copy the data from my structure to a char array but why can't I get the same values for the last long long int position [7]?

    Code:
    	struct packet 
    	{
    	unsigned int type : 8;
        unsigned int version : 8;
        unsigned int length : 8;
        unsigned int operand1 : 32;
        unsigned int operand2 : 32;
        unsigned int opcode : 7;
        unsigned int flag : 1;
        unsigned long long int result : 64;
    	
    	};
    	
    struct packet newpacket ={5,2,3,50,32,33,1,2033};
    	
    	
     unsigned char pbuffer[]={newpacket.type,newpacket.version, newpacket.length,newpacket.operand1,
    newpacket.operand2, newpacket.opcode,newpacket.flag, newpacket.result	};
    
    printf("value of result is .. %u\n", pbuffer[0]);
    printf("value of result is .. %u\n", pbuffer[1]);
    printf("value of result is .. %u\n", pbuffer[2]);
    printf("value of result is .. %u\n", pbuffer[3]);
    printf("value of result is .. %u\n", pbuffer[4]);
    printf("value of result is .. %u\n", pbuffer[5]);
    printf("value of result is .. %u\n", pbuffer[6]);
    printf("value of result is .. %d\n", pbuffer[7]);
    printf("value of result is .. %llu\n", newpacket.result);

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    2033 isn't going to fit into a char, so when you create newpacket that value gets mangled. (operand1 and operand2 would get mangled as well, except that in this case they happen to be small enough to fit.)

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    well like I said my idea was to copy the struct to a char buffer, like the using the code above for sendto(). what easy was do you suggest? that was my easy way...

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nothing wrong with copying the struct to a char buffer, but a long long int is going to take more than one character. You could probably use something like memcpy; or if you want to get network byte-order you can use whatever extension of htonl you've got on your system.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Here is a way that I have done this:

    Code:
    char* serialized = malloc(3*sizeof(int));  // allocate enough memory to store your entire struct
    bzero (serialized, 3*sizeof(int));  // or use memset
    sprintf(serialized, "%d,%d,%d", clientpacket.header, clientpacket.message, clientpacket.flag);
    sendto(sd, serialized, (int) strlen(serialized), 0, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr)); //or however you are sending data
    Hopefully this helps. Good luck.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    yes it did thank you. One questions what is this line doing? copying the values to you char*?

    Code:
    sprintf(serialized, "%d,%d,%d",clientpacket.header,clientpacket.message, clientpacket.flag);
    if how how did you copy the values on the server side?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IP header "total length" vs. packet size
    By MK27 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-04-2009, 07:45 AM
  2. pcap "packet" processing and storage
    By ForensicsGuy817 in forum C Programming
    Replies: 3
    Last Post: 04-11-2008, 12:28 PM
  3. Programmatically sending "enter" keystroke to Outlook
    By Mr. Kamikaze in forum C# Programming
    Replies: 6
    Last Post: 09-17-2004, 10:52 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM