Here's my sender side code
void send_data(int port, int coords[4]){
struct sockaddr_in si_me, si_other;
int s, i, slen = sizeof(si_other), recv_len;
char buf[BUFLEN];
//create a UDP socket
if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
err_print("socket");
}
//zero out the structure
memset((char*)&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
//bind socket to the port
if(bind(s, (struct sockaddr *)&si_me, sizeof(si_me) ) == -1){
err_print("bind");
}
int size = sizeof(coords);
printf("Size: %d\n", size);
//Keep listening for data
while(1){
printf("Waiting for Data\n");
fflush(stdout);
memset(buf, 0, BUFLEN);
//try to receive some data, this is a blocking call
if((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*) &si_other, &slen)) == -1){
err_print("recvfrom()");
}
//print details of the client/peer and the data received
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("%s\n", buf);
if(!strcmp(buf, "coords")) {
printf("Sending Coordinates: \n");
//now reply the client with the same data
int siz = sendto(s, coords, sizeof(coords), 0, (struct sockaddr*) &si_other, slen);
if(siz == -1){
err_print("sendto()");
}
printf("Siz: %d\n", siz);
}
break;
}
}
close(s);
}
The value returned from sendto is 8 which is the correct size of the int array but at the receiver side recvfrom returns 0 although the array receives the first two integer elements and the rest is garbage values when I print the array. Here's the receiver side code:
void get_data(int port, int coords[]){
struct sockaddr_in si_other;
int s, i;
char message[BUFLEN];
if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
err_print("socket");
}
memset((char*)&si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(port);
si_other.sin_addr.s_addr = inet_addr(SERVER);
if(inet_aton(SERVER, &si_other.sin_addr) == 0){
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
socklen_t slen = sizeof(si_other);
int size = sizeof(coords);
printf("Size: %d\n", size);
int siz = 0;
while(1)
{
printf("Enter message : ");
strcpy(message, "coords");
//send the message
if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
err_print("sendto()");
}
//try to receive some data, this is a blocking call
if (siz = (recvfrom(s, coords, 8, 0, (struct sockaddr *) &si_other, &slen)) == -1)
{
err_print("recvfrom()");
}
printf("Siz: %d\n", siz);
break;
}
for(int j = 0; j<4; j++){
printf("C: %d\n", coords[j]);
}
close(s);
return;
}
And the output of coords array is on the receiver side : 1 2 4196592 0 Now, i thought perhaps, the whole array wasnt getting sent, but the size values returned by sendto is 8 and that of recvfrom is 0 even if it gets the first two elements right. Also, do note that on compilation, I get warning for sizeof: ‘sizeof’ on array function parameter ‘coords’ will return size of ‘int *’
What could be wrong here and what's the right way to send the array? And if possible, could a double array too be sent?