A
Adam Balgach
Greetings everyone, ive got a problem ive been working with for quite
a while and need some help.
ive got a structure:
struct Data {
char *data1;
char *data2;
int val1;
int val2;
};
int main() {
Data myData;
myData.data1="this is test1";
myData.data2="this is test2";
myData.val1=23;
myData.val2=78;
now lets assume i have all the code correctly to open and bind a
datagram packet (UDP) now how do i send this structures values over
the socket...
ive been using this code:
if (sendto(sock, (char *)&myData, sizeof(myData), 0, (struct sockaddr
*)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");
assuming sock is an int already opened, and GatewayAddr is a structure
holding the address of the server.
why isnt this working?
ive also tried to manually turn the structure into a char* stream by
doing:
char *toSend;
toSend=(char*)malloc(sizeof(myData)+1);
memcpy(toSend,&myData,sizeof(myData));
toSend[sizeof(myData)]='\0';
printf("Packet: %s\n",toSend); //Displays nothing but 'Packet: '
if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");
which of course also doesnt work.
Does anyone know how i can accomplish this seemingly simple task?
Thanks so much.
Cheers,
Adam.
a while and need some help.
ive got a structure:
struct Data {
char *data1;
char *data2;
int val1;
int val2;
};
int main() {
Data myData;
myData.data1="this is test1";
myData.data2="this is test2";
myData.val1=23;
myData.val2=78;
now lets assume i have all the code correctly to open and bind a
datagram packet (UDP) now how do i send this structures values over
the socket...
ive been using this code:
if (sendto(sock, (char *)&myData, sizeof(myData), 0, (struct sockaddr
*)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");
assuming sock is an int already opened, and GatewayAddr is a structure
holding the address of the server.
why isnt this working?
ive also tried to manually turn the structure into a char* stream by
doing:
char *toSend;
toSend=(char*)malloc(sizeof(myData)+1);
memcpy(toSend,&myData,sizeof(myData));
toSend[sizeof(myData)]='\0';
printf("Packet: %s\n",toSend); //Displays nothing but 'Packet: '
if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");
which of course also doesnt work.
Does anyone know how i can accomplish this seemingly simple task?
Thanks so much.
Cheers,
Adam.