Why doesn't select() react on received messages? - c

I'm trying to abort sending UDP data to specific hosts when I receive data back from any of them.
So far what I've tried is creating a thread inside other function that sends data.
Thread is supposed to be checking for incoming data on socket descriptors for maximum of 100 secs. However select() function only waits for timeout and doesn't respond when data is received. Sockets are already created in calling function, here I'm only passing file descriptors. Also this is modified code snippet from Beej's Guide to Network Programming.
void *bot_listen(void *arg){
printf("Thread starting\n");
size_socks *sock_size=(size_socks*)arg;
int n, rv;
fd_set readfds;
struct timeval tv;
thr_running=1;
FD_ZERO(&readfds);
for(int i=0;i<sock_size->size;i++) {
FD_SET(sock_size->sockets[i], &readfds);
}
n = sock_size->size + 1;
tv.tv_usec = 0;
tv.tv_sec = 100;
rv = select(n, &readfds, NULL, NULL, &tv);
if (rv == -1) {
perror("select"); // error occurred in select()
} else if (rv == 0) {
printf("Timeout occurred! No data after 100 seconds.\n");
} else {
bot_recv_message=1;
}
thr_running=0;
}
void send_dgrams(int num_ip, struct message ip_port_pairs, char *message){
struct addrinfo hints, *res;
struct sockaddr_in *dest;
char udp_ip[INET_ADDRSTRLEN];
char str[INET6_ADDRSTRLEN];
char udp_port[22];
char *p;
int *sockfd, time_elapsed=0;
size_socks ss;
printf("Message, beginning of send_dgrams: %s\n", message);
pthread_t listen_thread;
sockfd = (int*)malloc(sizeof(int)*num_ip);
dest = malloc(sizeof(struct sockaddr_in)*num_ip);
p=&(ip_port_pairs.command);
p++;
for(int i =0; i<num_ip;i++) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
strcpy(udp_ip, p);
p+=INET_ADDRSTRLEN;
strcpy(udp_port,p);
p+=22;
getaddrinfo(udp_ip, udp_port, &hints, &res);
sockfd[i] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
dest[i].sin_family = res->ai_family;
dest[i].sin_port = ((struct sockaddr_in *) res->ai_addr)->sin_port;
dest[i].sin_addr = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
memset(dest[i].sin_zero, '\0', sizeof(dest[i].sin_zero));
}
ss.size=num_ip;
ss.sockets=sockfd;
pthread_create(&listen_thread, NULL, bot_listen, (void*)&ss);
while(bot_recv_message==0 && time_elapsed<100) {
for (int i = 0; i < num_ip; i++) {
printf("Sending this message: %s to socket %d\n", message, sockfd[i]);
inet_ntop(AF_INET, &(dest[i].sin_addr), str, INET_ADDRSTRLEN);
printf("Sending to this address: %s\n", str);
printf("Sending to this port: %d\n", ntohs(dest[i].sin_port));
printf("Did bot receive message? :%d\n", bot_recv_message);
sendto(sockfd[i], message, strlen(message) + 1, 0, (struct sockaddr *) &dest[i], sizeof(dest[i]));
}
sleep(1);
time_elapsed+=1;
}
for(int i=0;i<num_ip;i++){
close(sockfd[i]);
}
bot_recv_message=0;
}

This could be the problem:
n = sock_size->size + 1;
...
rv = select(n, &readfds, NULL, NULL, &tv);
Value of the 1st parameter of select() won't be big enough.
Man page of the select() says the following about the 1st parameter:
nfds should be set to the highest-numbered file descriptor in any of
the three sets, plus 1. The indicated file descriptors in each set
are checked, up to this limit (but see BUGS).
So try something like this:
int max_fd = -1;
for(int i=0;i<sock_size->size;i++) {
FD_SET(sock_size->sockets[i], &readfds);
if (sock_size->sockets[i] > max_fd)
max_fd = sock_size->sockets[i];
}
...
rv = select(max_fd + 1, &readfds, NULL, NULL, &tv);

Related

C, TCP, recvAll block is not receing anything

I am new to both C and socket programming, so please bear with me. The following code is mostly from Beej networking guide, with some changes. I have the receiver code attached (which is TCP server in this case), that listens to multiple TCP connections. I have a transmitter (client) who is constantly sending fixed chunks of data to this receiver. This code (which I cleaned and removed some function definitions unrelated to my issue) works if instead of calling recv_all function, I only call recv(). But the problem with that I need to do processing on each chunk of received data, so I need the whole chunk. So I thought I should use the recv_all().
Now the problem is it gets stuck in an infinite loop in the while in recv_all(), because n is always 0. I truly appreciate your help.
#define PORT "3490" // the port users will be connecting to
#define BACKLOG 20 // how many pending connections queue will hold
#define MAXDATASIZE 801 // max number of bytes we can get at once
int recv_all(int socket, char *buffer, int *length)
{
int total = 0; // how many bytes we've sent
int bytesleft = *length; // how many we have left to send
int n;
while(total < *length) {
n = recv(socket, buffer+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*length = total; // return number actually received here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
int main(void)
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;
double buf[MAXDATASIZE];
int lenRecv;
struct sockaddr_in local_addr; // For the new addition to bind it to an interface
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
freeaddrinfo(servinfo); // all done with this structure
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
printf("server: waiting for connections...\n");
while(1) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
printf("server: got connection from %s\n", s);
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
for (int i=0; i<1000000; i++) {
int rowInfoSize;
//if ((numbytes = recv(sockfd, buf, sizeof (buf), 0)) == -1) {
//if ((numbytes = recv(new_fd, buf, sizeof (buf), MSG_WAITALL)) == -1) { // I THINK THE BETTER WAY IS to CHECK THE OUTPUT AND LOOP UNTIL COMPLETE.
lenRecv = sizeof (buf);
//if (recv_all(new_fd, (char *)buf, &lenRecv) == -1) {
if (recv_all(new_fd, buf, &lenRecv) == -1) {
perror("sendall");
printf("We only sent %d bytes because of the error!\n", lenRecv);
}
}
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}
return 0;
}
You are ignoring end of stream. If n == 0 the peer has disconnected. Your code will loop forever.
Throw it all away and use recv() with the MSG_WAITALL option.
give recv_all a timeout value
int recv_all(int socket, char *buffer, int *length, int timeout)
and use select before recv()
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
looks like this
int recv_all(int socket, char *buffer, int *length)
{
int total = 0; // how many bytes we've sent
int bytesleft = *length; // how many we have left to send
int n = -1;
struct timeval timeout;
fd_set rset;
int rc;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while(total < *length) {
FD_ZERO(&rset);
FD_SET(socket, &rset);
rc = select(socket + 1, &rset, NULL, NULL, &timeout);
if(rc < 0)
{
// errno
n = -1;
break;
}
if(0 == rc)
{
// timeout
n = -1;
break;
}
if(FD_ISSET(socket, &rset))
{
n = recv(socket, buffer+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
}
}
*length = total; // return number actually received here
return n==-1?-1:0; // return -1 on failure, 0 on success
}

Receive latest packet in udp server - C

UDP server is receiving packets with the select system call. And I want to receive the latest packet from each UDP-client. (I also want to listen to multiple UDP client packets).
The codes of my simple UDP-server:
int main(void) {
int fd;
int port = 5678;
char buffer[1024];
fd_set readfs;
socklen_t client_length;
struct timeval timeout_interval;
struct sockaddr_in6 server_addr;
struct sockaddr_in6 client_addr;
int result;
int recv;
char client_addr_ipv6[100];
fd = socket(PF_INET6, SOCK_DGRAM, 0);
printf(" \e[1m \e[34m ---------------------------------------- \n-------------------- UDP SERVER --------------------\n \e[39m \e[0m \n");
printf("Process: \e[34m %d \e[49m Port ..\n", port);
if (fd < 0) {
printf("ERR: fd < 0");
} else {
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin6_family = AF_INET6;
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(port);
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin6_family = AF_INET6;
client_addr.sin6_addr = in6addr_any;
client_addr.sin6_port = htons(port);
if (bind(fd, (struct sockaddr *) &server_addr, sizeof(server_addr))
>= 0) {
printf("\e[1m INFO: \e[0m \e[34m Bind success.. \e[39m\n");
} else {
printf("Bind.");
return -1;
}
for (;;) {
FD_ZERO(&readfs);
FD_SET(fd, &readfs);
int max_fd = MAX(0, fd);
timeout_interval.tv_sec = 3;
timeout_interval.tv_usec = 50000000;
result = select(max_fd + 1, &readfs, NULL, NULL, &timeout_interval);
//printf("\n %d \t %d \n", result, fd);
if (result < 0) {
printf("ERR\n");
} else if (result == 0) {
printf("\nTimeout\n");
} else {
if (FD_ISSET(fd, &readfs)) {
client_length = sizeof(client_addr);
if ((recv = recvfrom(fd, buffer, sizeof(buffer), 0,
(struct sockaddr *) &client_addr, &client_length))
< 0) {
printf("Recv-ERR!");
break;
}
inet_ntop(AF_INET6, &(client_addr.sin6_addr), client_addr_ipv6, 100);
//printf("Client IP/Port : %s ",client_addr_ipv6);
printf("\n ------------------------------------------ \n");
printf("\e[1m Data: \e[0m \e[32m %.*s \n Client IP/Port : \e[34m %s / %d \n\e[39m", recv, buffer,client_addr_ipv6,ntohs(client_addr.sin6_port));
}
}
}
}
}
The best way to handle this is to have the sender put a sequence number in each packet. For each packet that is sent out, the sequence number increases by 1.
On the receiver side, you would keep track of the counter of the last packet you received. If the next packet that comes in has a larger counter, it's the latest. If it's not larger, it's an older packet and you can handle it in an appropriate manner for your application.
Also, you should rename you recv variable to something like recv_len. recv is the name of a socket function that would be masked by this variable definition.

recvfrom function is blocked

I have following two files
Client.c
int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
exit(-1);
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SRV_IP, &si_other.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
for (i=0; i<NPACK; i++)
{
printf("Sending packet %d\n", i);
sprintf(buf, "This is packet %d\n", i);
if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
exit(1);
}
sleep(10);
close(s);
return 0;
}
Server.c
int tries=0; /* Count of times sent - GLOBAL for signal-handler access */
void diep(char *s)
{
perror(s);
exit(1);
}
void CatchAlarm(int ignored) /* Handler for SIGALRM */
{
tries += 1;
}
void DieWithError(char *errorMessage)
{}
/* Error handling function */
void *print_message_function( void *ptr )
{
char *message;
usleep(6200*1000);
message = (char *) ptr;
printf("%s \n", message);
sleep(20);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
struct sigaction myAction; /* For setting signal handler */
const char *message1 = "Thread 1====================================";
char buf[BUFLEN];
pthread_t thread1, thread2;
pthread_create( &thread1, NULL, print_message_function, (void*) message1);
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
myAction.sa_handler = CatchAlarm;
if (sigfillset(&myAction.sa_mask) < 0) /* block everything in handler */
DieWithError("sigfillset() failed");
myAction.sa_flags = 0;
if (sigaction(SIGALRM, &myAction, 0) < 0)
DieWithError("sigaction() failed for SIGALRM");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");
alarm(TIMEOUT_SECS);
for (i=0; i<NPACK; i++) {
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
{
printf("Inside eagain %d\n",errno);
if(errno == EINTR)
{
alarm(TIMEOUT_SECS); /* Set the timeout */
}
else
exit(-1);
}
else
printf("Received packet from %s:%d\nData: %s\n\n",
inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
}
alarm(0);
pthread_join( thread1, NULL);
close(s);
return 0;
}
I am running Server first then Client. In some occurrences Server is not able to receive the message sent my client. Though client sent it successfully. Even EINTR error also I am getting because of alarm but still recvfrom function is getting blocked in between
I solved the problem. The reason is in my system the value of net.core.rmem_max was set to 12KB. And in this case I was sending MBs of data within a very short span of life. So receiver buffer was soon filled up and UDP was ignoring the rest of the buffer. I have increased the net.core.rmem_max to 10MB using following command
sysctl -w net.core.rmem_max=Value
After that this program is working fine.

TCP client socket, doesn't block on select()

The following code is a test program wriiten to understand the behaviour of select() call in a TCP client program.
What I observe is that the select is not blocking, instead the program is blocking on recv().
The output is as follows:
Wait on select.
Wait on recv.
...
My question is why the select() returns a success? Ideally it should be blocking on the select() instead of recv().
The TCP server is sending a character string of 15 bytes once in 3 seconds.
int clientfd = -1;
int dummyfd = -1;
int maxfd = -1;
struct sockaddr_in server_addr;
char recv_buf[100] = {0};
int msg_len = 0;
int bytes_recv = 0;
fd_set readfd;
int retval = 0;
/* Open the socket and a dummy socket */.
clientfd = socket(AF_INET, SOCK_STREAM, 0);
dummyfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == clientfd || -1 == dummyfd)
{
perror("socket error: ");
exit(1);
}
printf("Socket opened : %d\n", clientfd);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(10000);
//server_addr.sin_addr.s_addr = INADDR_ANY;
inet_aton("127.0.0.1", &(server_addr.sin_addr));
memset(&(server_addr.sin_zero), 0, 8);
/* Connect to server */
if(connect(clientfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)))
{
perror("connect error: ");
exit(1);
}
printf("Connect Success\n");
maxfd = (clientfd > dummyfd) ? (clientfd + 1) : (dummyfd + 1);
while(1)
{
FD_ZERO(&readfd);
FD_SET(clientfd, &readfd);
FD_SET(dummyfd, &readfd);
printf("Wait on select\n");
retval = select(maxfd , &readfd, NULL, NULL, NULL);
if(retval <= 0)
{
printf("select failed\n");
}
else
{
printf("Wait on recv\n");
/* ... The process waits here ... */
bytes_recv = recv(clientfd, recv_buf, 100, 0);
printf("%d: Bytes recv = %d\t%s\n", retval, bytes_recv, recv_buf);
memset(recv_buf, 0 ,100);
}
}
close(clientfd);
return 0;
}
Edit: Without dummyfd, the program works as intended.
A follow up question:
When the server is closed abruptly, how to detect this using select()?
Can the program be modified so that is blocks on select() when the server side, say, crashes?
Use the following to be sure it's the clientfd that's returning from the select:
else if (FD_ISSET(clientfd, &readfd)) {
Don't have time to test, but I suspect the dummyfd is returning as an EOF from the select, not the clientfd.
After select() returns, you will want to conditionally receive from clientfd. My guess is that there may be data on dummyfd that is triggering the select to complete, but the receive is on the clientfd.
retval = select(maxfd , &readfd, NULL, NULL, NULL);
if(retval <= 0)
{
printf("select failed\n");
}
else
{
if (FD_ISSET(clientfd, &readfd))
{
bytes_recv = recv(clientfd, recv_buf, 100, 0);
...
}
if (FD_ISSET(dummyfd, &readfd))
{
/* "dummyfd" processing */
}

C, socket programming: Connecting multiple clients to server using select()

I'm trying to make a server that can be connected to by multiple clients. Here's my code so far:
Client:
int main(int argc, char **argv) {
struct sockaddr_in servaddr;
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) perror("Socket");
bzero((void *) &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6782);
servaddr.sin_addr.s_addr = inet_addr(<server_ip_address>);
if (-1 == connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)))
perror("Connect");
while(1) {
char message[6];
fgets(message, 6, stdin);
message[5] = '\0';
send(sock, message, 6, 0);
}
close(sock);
}
Server:
int main(int argc, char **argv) {
fd_set fds, readfds;
int i, clientaddrlen;
int clientsock[2], rc, numsocks = 0, maxsocks = 2;
int serversock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serversock == -1) perror("Socket");
struct sockaddr_in serveraddr, clientaddr;
bzero(&serveraddr, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(6782);
if (-1 == bind(serversock, (struct sockaddr *)&serveraddr,
sizeof(struct sockaddr_in)))
perror("Bind");
if (-1 == listen(serversock, SOMAXCONN))
perror("Listen");
FD_ZERO(&fds);
FD_SET(serversock, &fds);
while(1) {
readfds = fds;
rc = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
if (rc == -1) {
perror("Select");
break;
}
for (i = 0; i < FD_SETSIZE; i++) {
if (FD_ISSET(i, &readfds)) {
if (i == serversock) {
if (numsocks < maxsocks) {
clientsock[numsocks] = accept(serversock,
(struct sockaddr *) &clientaddr,
(socklen_t *)&clientaddrlen);
if (clientsock[numsocks] == -1) perror("Accept");
FD_SET(clientsock[numsocks], &fds);
numsocks++;
} else {
printf("Ran out of socket space.\n");
}
} else {
int messageLength = 5;
char message[messageLength+1];
int in, index = 0, limit = messageLength+1;
while ((in = recv(clientsock[i], &message[index], limit, 0)) > 0) {
index += in;
limit -= in;
}
printf("%d\n", index);
printf("%s\n", message);
}
}
}
}
close(serversock);
return 0;
}
As soon as a client connects and sends its first message, the server just runs in an infinite loop, and spits out garbage from the message array. recv doesn't seem to receive anything. Can anyone see where i go wrong?
Two issues in your code:
You should do recv(i, ...) instead of recv(clientsock[i], ...)
After that you do not check if recv() failed, and therefore printf() prints out the uninitialised buffer message, hence the garbage in the output
You need to check for limit <= 0 in your read loop, before you call read.
In the while loop for the server, change the code to do recv(i) instead of recv(clientsocks[i]). I have implemented this code and it works with this change.
I replaced the else with the below and it works
} else {
/* int messageLength = 5;
char message[messageLength+1];
int in, index = 0, limit = messageLength+1;
memset ( &message[index] , 0, sizeof ( message [index] ) );
while ((in = recv(i, &message[index], limit, 0)) > 0) {
index += in;
limit -= in;
}
printf("%d\n", index);
printf("%s\n", message);
*/
bzero(buf, sizeof(buf));
if ((rval = read(i, buf, 1024)) < 0)
perror("reading stream message");
else if (rval == 0)
printf("Ending connection\n");
else
printf("-->%s\n", buf);
}
1) It is a good practice to use PF_INET(protocol family) rather than
AF_INET(address family) during the Socket creation .
2) within the while(1) loop
each time it is advisable to make your readfds empty by using FD_ZERO(&readfds).
in the recv() call you should use i rather than clientsocks[i]
you have to check return value of recv is negative(which indicating error in reading) if that is the case you do not have to print the message.
during printing the message make sure the stdout/server is ready for writing anything to it which you can do it by using writefds (3rd argument of select).

Resources