Is it possible to receive data with recvfrom on 2 ports? I have a port which I use for user requests and another port for chat messages. Is it possible to bind two sockets,
No, it's not possible to read from both sockets using a single call to recvfrom, since that function only takes a single socket file descriptor parameter.
You either need a thread per port, or a mechanism (e.g. select, poll) to tell which sockets have data waiting to be read.
In the latter case, once you know which sockets have pending data you can then call recvfrom on that specific socket to get the data you need, e.g.:
// set up select parameters
fd_set socks;
FD_ZERO(&socks);
FD_SET(socket_fd, &socks);
FD_SET(socket_fd2, &socks);
// find out which sockets are read - NB: it might be both!
int nsocks = max(socket_fd, socket_fd2) + 1;
if (select(nsocks, &socks, (fd_set *)0, (fd_set *)0, 0) >= 0) {
if (FD_ISSET(socket_fd, &socks) {
// handle socket 1
recvfrom(socket_fd, ...);
}
if (FD_ISSET(socket_fd2, &socks) {
// handle socket 2
recvfrom(socket_fd2, ...);
}
}
NB: this is just a rough and ready sample - real code would have error checking, etc.