I have an assignment for school, and one part is to send a set of ints, chars char* from a client to a server using socket programming. Sending ints or chars works just fine, but is there any way to send the entire struct as one packet? Read about serializing, but I cant seem to make it work. Here's a code snippet:
The struct looks like this:
struct Msg
{
int a;
char b;
char *user;
};
Client:
init variables and such...
int netcom(char* ip, int port) {
sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
// clear the structure
bzero(&serveraddr, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
// add the server adress
inet_pton(AF_INET, ip, &serveraddr.sin_addr);
// add the port number
serveraddr.sin_port = htons(port);
// connect
connect (sd,(struct sockaddr*)&serveraddr, sizeof(struct sockaddr_in));
}
int sendPkg(struct Msg msg) {
send(sd, &msg, sizeof(msg), 0);
}
And the part of the server that receives looks like this:
char buf[100];
recv(sd[i], buf, sizeof(buf)-1, 0);
The client sends perfectly fine, and the server receives fine. But I have no idea what I'm sending, and how to read it properly. So that's my question. How to properly send over the data from a struct using sockets.