In the code below I have 3 functions (including main). The receive_loop function polls the socket bound to 127.0.0.1 and if it gets any message, it prints it. The sender_loop waits for user input using scanf and when there is an input, it sends it through the socket bound to 127.0.0.1. The main forks and runs sender_loop in child and receive_loop in the parent.
Now if I compile the code and run two instances of this executable, whichever instance starts up first receives the messages sent by either instance. So lets say, instance1 was started first, then instance1 receives messages sent by instance1 AND instance2. I cannot figure out why this is happening and I would appreciate if someone could explain it please (in detail). Thanks.
// INCLUDES NOT SHOWN
// recieve_loop
int receive_loop(int sock_fd, struct sockaddr *peer) {
int isDataReady = 0;
char buff[128] = {0};
struct pollfd poll_struct;
poll_struct.fd = sock_fd;
poll_struct.events = POLLIN;
printf("reciever up\n");
while (1) {
while (1) {
if (isDataReady > 0) break;
isDataReady = poll(&poll_struct, 1, 500);
}
socklen_t sock_len = sizeof(struct sockaddr);
recvfrom(sock_fd,
buff,
sizeof(buff),
0,
(struct sockaddr *)peer,
&sock_len);
printf("%s\n", buff);
}
return 0;
}
// sender_loop
int sender_loop(int sock_fd, struct sockaddr *peer) {
char buff[32] = {0};
printf("sender up\n");
while (1) {
scanf("%s", buff);
int bytes_sent = sendto(sock_fd, buff, sizeof(buff), 0, (struct sockaddr *)peer, sizeof(struct sockaddr_in));
if (bytes_sent <= 0)
printf("sending message failed\n");
}
return 0;
}
int main(int argc, const char *argv[]) {
// socket file descriptor to send data through
int sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
// fill in the peer's address, loopback in this case
struct sockaddr_in *peer = malloc(sizeof(struct sockaddr_in));
peer->sin_family = AF_INET;
peer->sin_port = htons(11110);
inet_pton(AF_INET, "127.0.0.1", &peer->sin_addr);
bind(sock_fd, (struct sockaddr *)peer, sizeof(struct sockaddr_in));
pid_t pid = fork();
if (pid < 0) {
printf("Couldn't fork, exiting");
return 1;
}
if (pid == 0) {
sender_loop(sock_fd, (struct sockaddr *)peer);
} else {
receive_loop(sock_fd, (struct sockaddr *)peer);
}
return 0;
}
Your second instance is failing on
bind()because it is trying to bind to a port already in use by the first instance.Change to this and see.