Sending struct over network - Programmer

This is a discussion on Sending struct over network - Programmer ; Hello, I'm trying to send a struct from a host computer over the network and recv on a client. Here's a sample of my code: typedef struct { int size; //holds size in characters of buffer to send char * ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Sending struct over network

  1. Sending struct over network

    Hello,

    I'm trying to send a struct from a host computer over the network and
    recv on a client.
    Here's a sample of my code:


    typedef struct {
    int size; //holds size in characters of buffer to send
    char * buffer; //message to send
    } rData, *prData;


    Host:
    (...)
    rData.buffer=new char[14]; //memory to buffer
    lstrcpy(rData.buffer,"Hello, World!"); //stores something in buffer to
    send
    rData.size=lstrlen(rData.buffer); //size of buffer
    pData=&rData; //pointer to struct
    if(send(socket,(PCHAR)pData,sizeof(rData),0)==SOCK ET_ERROR) //send
    it
    return false;
    (...)

    Client:

    int bytes=SOCKET_ERROR; //bytes received
    prData pData; //pointer to rData
    char msg[256]; //message to display (from recv)

    while(bytes==SOCKET_ERROR)
    bytes=recv(socket,(PCHAR)pData,sizeof(int),0); //receives size of
    buffer
    wsprintf(msg,"Size: %i",pData->size);
    MessageBox(hMainWnd,msg,"Message",MB_OK); //show size of buffer
    pData++; //moves pointer position
    while(bytes==SOCKET_ERROR)
    bytes=recv(socket,(PCHAR)pData,pData->size,0); //receives buffer
    wsprintf(msg,"Message: %s",pData->buffer);
    MessageBox(hMainWnd,msg,"Message",MB_OK); //show it
    (...)

    Boath host and client were build with same struct alignment (byte
    alignment).


    What is wrong with this code?
    Why don't I get the message?
    How can I do this work?
    Thanks,
    Direz

  2. Re: Sending struct over network

    Direz Gunstad wrote:
    > Hello,
    >
    > I'm trying to send a struct from a host computer over the network and
    > recv on a client.
    > Here's a sample of my code:
    >
    >
    > typedef struct {
    > int size; //holds size in characters of buffer to send
    > char * buffer; //message to send
    > } rData, *prData;
    >
    >
    > Host:
    > (...)
    > rData.buffer=new char[14]; //memory to buffer
    > lstrcpy(rData.buffer,"Hello, World!"); //stores something in buffer to
    > send
    > rData.size=lstrlen(rData.buffer); //size of buffer
    > pData=&rData; //pointer to struct
    > if(send(socket,(PCHAR)pData,sizeof(rData),0)==SOCK ET_ERROR) //send
    > it
    > return false;
    > (...)
    > Boath host and client were build with same struct alignment (byte
    > alignment).
    >
    >
    > What is wrong with this code?
    > Why don't I get the message?
    > How can I do this work?
    > Thanks,
    > Direz


    You are sending a pointer (rData.buffer) instead of sending the
    character string it points at. The pointer is useless on another
    computer. Also, you are sending only 8 bytes (sizeof(rData)) and
    somehow expecting it to hold both the length and the 14 character string!

    One way to get it to work would be to send the size, then send the
    character string.
    rData.size=lstrlen(rData.buffer);
    send(socket, (PCHAR)&rData.size, sizeof(rData.size));
    send(socket, rData.buffer,rData.size);

    --
    Scott McPhillips [VC++ MVP]


  3. Re: Sending struct over network

    That's it! Now it works!
    Thank's,
    Direz

+ Reply to Thread