Thread: dynamic packet size.

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    dynamic packet size.

    if i have a packet like so...
    Code:
    struct PACKET
    {
        unsigned char message;
        unsigned short length;
        char* message;
    }
    how would i go about recieving it, since the size can be anywhere from 3bytes - ???bytes?

    I think to send it i would go (havent tested it because i dont know how to recieve it yet.)
    Code:
    PACKET* pack = new PACKET;
    ...
    pack->length = strlen(pack->message);
    ...
    send(socket, pack, pack->length + 3, 0);

    Any Ideas?

  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
    You have two fields with the same name.

    Send the length, then send the message
    Code:
    send(socket, &pack->length, sizeof(pack->length), 0);
    send(socket, pack->message, pack->length, 0);
    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
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Your first step would be to not cross post your questions.
    Second, do what Salem said.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    the problem is im using UDP (its for a game) so there is no guarantee that the size will arive before the message.

    EDIT: woops, i thought i posted it, but when i came here to look it wasnt there so i thought i messed it up.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the problem is im using UDP (its for a game) so there is no guarantee that the size will arive before the message.
    In which case you need to pack the message into a new buffer, then send that.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    could you explain what you mean by that.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    char *buffer;
    char *message = whatever;
    int messagelen = strlen(message);
    buffer = malloc(sizeof(int) + messagelen + 1);
    messagelen = htonl(messagelen);
    memcpy(buffer,&messagelen,sizeof(int));
    strcpy(buffer + sizeof(int),message);
    send(socket,buffer,sizeof(int) + messagelen + 1,0);
    When you receive the packet, read the first sizeof(int) bytes to know how long the message will be. If the machine sending has a different int size than the machine receiving, then you need to account for that.

    One problem that you could run into is if your message is long enough, the packet could get broken up into 2 or more packets. These packets could then take different paths through the network and arrive at the destination in the wrong order to be reassembled. The lesson here is to keep your UDP packet sizes small.
    Last edited by bithub; 05-31-2006 at 09:46 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    oh, i think i get it now.

    so on the recieving end it would be something like...

    Code:
    int messagesize;
    char* buffer;
    read(socket, messagesize, sizeof(int),0);
    read(socket, buffer, messagesize),0);

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    close, but don't try to read into "buffer" without allocating "messagesize" bytes (or whatever units you choose to use) of memory first.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok,
    thanks for the help.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok im having a few issues with this. I pack the message length and the message into the buffer and send it, the size of the message is 14. but when it gets to the other side and i read in the size it says its 1681989646. here is the send and recv code for this.

    Code:
    //packing and sending
    char *message;
    message = (char *)malloc(strlen(username) + strlen(password) + 3);
    
    memset(message, '\0', sizeof(message));
    message[0] = LOGIN;	strcat(message, " "); 
    strcat(message, username);	strcat(message, " ");
    strcat(message, password);
    
    char *buffer;
    
    int size = sizeof(int) + strlen(message);
    buffer = (char *)malloc(size);
    
    memset(buffer, '\0', sizeof(buffer));
    memcpy(buffer, &size, sizeof(int));
    strcat(buffer, message);
    
    allegro_message("size: %d", size);
    allegro_message("Message to server: %s", buffer);
    
    nlWrite(LocalSocket, buffer, sizeof(buffer)); //same as send(LocalSocket, buffer, sizeof(buffer), 0);
    and

    Code:
    //recieve and unpack
    int messagelength;
    while(nlRead(newsock, (void *)&messagelength, sizeof(int)) <= 0) //same as recv(newsoc, (void *)&messagelength, sizeof(int), 0);
    {}
    cout<<"Messagelength: "<<messagelength<<endl;
    char* buffer = (char *)malloc(messagelength);
    while(nlRead(newsock, buffer, messagelength) <= 0)
    {}
    cout<<"Buffer: "<<buffer<<endl;
    any ideas why im getting the wrong number?

    thanks

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > message = (char *)malloc(strlen(username) + strlen(password) + 3);
    Not enough memory here (and stop casting malloc), or start using new if this is C++
    LOGIN, two spaces and a \0 is 4 bytes extra.

    > memset(message, '\0', sizeof(message));
    1. It's a complete waste of time
    2. sizeof on a pointer tells you how big the pointer is, not how much memory is allocated.

    > message[0] = LOGIN; strcat(message, " ");
    message[0] = LOGIN
    strcpy( &message[1], " " );

    > memcpy(buffer, &size, sizeof(int));
    > strcat(buffer, message);
    Here's where you're going wrong - the strcat isn't taking into account the \0 bytes in your integer.
    So do
    memcpy(buffer, &size, sizeof(int));
    strcpy(&buffer[4], message);
    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.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    thank you again salem.

    EDIT: i seem to be having some problems with the mem* and str* functions... i have the following code:

    Code:
    int size = strlen(message) + sizeof(int);
    char *buffer = new char[size];
    when size is printed to the screen it is 14 when strlen(buffer) is printed to the screen it si 27. any ideas what causes this?

    and another thing after doing this:

    Code:
    int size2 = strlen(message);
    memcpy(buffer, &size2, sizeof(int));
    strlen(buffer) becomes 1.

    any suggestions or turorials i could read on the subject?
    Last edited by c++.prog.newbie; 06-03-2006 at 06:34 PM.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    when size is printed to the screen it is 14 when strlen(buffer) is printed to the screen it si 27. any ideas what causes this?
    buffer is not a string, dont use strlen() on it.

    strlen(buffer) becomes 1.
    buffer is not a string.

    You need to learn what strings are in C. In C, strings are just continuous bytes of memory terminated by a byte with the value of 0.

    Code:
    char *buffer = new char[size];
    What does buffer contain after this line? The answer is we dont know. Therefore what happens when you use strlen() on it? The answer again is that we don't know.

    Code:
    memcpy(buffer, &size2, sizeof(int));
    What does buffer contain after this line? If the value of size2 is 14, then the memory probably looks something like:
    0x0E 0x00 0x00 0x00 [the rest of the bytes are undefined]
    Thus when you run strlen() on that memory, how many bytes will it count before it reaches a byte with a value of 0? The answer is 1, and that's why strlen() is giving you 1 right here.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    yes, thank you. i just realized what i was trying to do, wouldnt work how i thought it would.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to know dynamic size of struct
    By unikgila in forum C Programming
    Replies: 16
    Last Post: 10-20-2008, 08:56 PM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. total size of dynamic memory allocated array
    By trekker in forum C Programming
    Replies: 10
    Last Post: 03-10-2002, 12:59 PM