In your code, after you create the socket, but before the bind call, invoke the following:
int val = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
Then call bind. The above will allow the socket bind to succeed even if the port is in use.
Two processes, attempting a recvfrom on the same port, will result in one of the processes receiving the packet, but not the other. And it’s not deterministic which one will. So make sure you don’t actually have two processes legitimately running and sharing the port.