The server I am building uses the UDP transport layer protocol. I have a scenario where multiple clients can send datagrams to my server. But each client may send the data across multiple datagrams, and before starting to process the data, I need to have the entire data at the server. From the man page of recvfrom()
, it sounds like it reads the datagrams from any UDP client. Are there any recv*()
family functions that enable us to read from a specific UDP client?
#include <netinet/in.h>
#include <sys/socket.h>
int append_data(void* buf, int size);
int main()
{
/* Create a UDP socket and bind to a port */
int sockfd;
char rd_buf[100];
struct sockaddr_in cliaddr;
for ( ; ; ) {
/* Recvfrom any client */
int addr_len = sizeof(cliaddr);
int rd_status = recvfrom(sockfd, rd_buf, sizeof(rd_buf), 0, (struct sockaddr*) &cliaddr, &addr_len);
if (append_data(rd_buf, rd_status) == DATA_COMPLETE)
continue;
/* Recvfrom a particular client */
for ( ; ; ) {
/* Any function similar to recvfromcli() ? */
int rd_status = recvfromcli(sockfd, rd_buf, sizeof(rd_buf), 0, (struct sockaddr*) &cliaddr, addr_len);
if (append_data(rd_buf, rd_status) == DATA_COMPLETE)
break;
}
}
}
If not what are my other alternatives?
You can create a separate UDP socket that is bind()
'ed to the local IP/Port and connect()
'ed to a specific client IP/Port, then you can recv()
using that socket, and it will give you datagrams from only that client.