Is there a difference between the way the function recvfrom and sendto use the address structure.
When i use sento it works fine, but when I try and use recvfrom with the same variables it errors out with (bad address).
Or is there a better way to receive packets from a socket that i just sent to.
Theres some of my source code.
When i use sento it works fine, but when I try and use recvfrom with the same variables it errors out with (bad address).
Or is there a better way to receive packets from a socket that i just sent to.
Theres some of my source code.
Code:
struct sockaddr_in a;
a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(INADDR_ANY);
a.sin_port = htons(DefaultPort);
int sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
int fBroadcast = 1;
int err = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, "C", 5);
if (err == -1) { perror("setsockopt"); exit(1); }
//Binding the address to the socket
err = bind(sock, (struct sockaddr *)&a, sizeof(a));
if (err == -1) { perror("bind"); exit(1); }
//Putting the ip address in the socket output address structure
a.sin_addr.s_addr = inet_addr(ip);
//Sending the information to the specified port
if (sendto(sock, packet, len, 0, (struct sockaddr *)&a, sizeof(a)) == -1)
{ perror("sendto"); exit(1); }
int rcv;
char buf[300];
// Recieve
rcv = recvfrom(sock, buf, 208, MSG_WAITALL, (struct sockaddr *)&a , sizeof(a));
if(rcv == -1) { perror("recvfrom"); exit(1); }
Comment