Return to Snippet

Revision: 1354
at September 27, 2006 14:37 by brentroady


Initial Code
#pragma pack(1)
typedef struct
{
    int id;
    char[50] text;
} MESSAGE;

// Send a message
MESSAGE msg;
msg.id = 1;
strcpy(msg.text, "This is a test");
send(socket, (char*)&msg);

// Receive a message
char buffer[100];
recv(socket, buffer, 100);
MESSAGE* msg = (MESSAGE*)buffer;
printf("id=%d\n", msg->id);
printf("text=%s\n", msg->text);

Initial URL
http://geekswithblogs.net/taylorrich/archive/2006/08/21/88665.aspx

Initial Description


Initial Title
Mapping a byte array to a struct in C

Initial Tags


Initial Language
C++