Socket and select

I have created two sockets and binded both. My requirement is that 2nd socket must send/ recv data only on expiration of timeval(tv). but the 1st socket must keep on send/recv the data without waiting for the 2nd socket completion......
I have posted my code below......
In this code the 2nd socket is blocking the 1st socket... I don't want this blocking to happen... plz check the code and suggest any correction.

/*=========client PGM=========================*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define MAX   1024
#define ZERO  0
#define ONE   1
#define TWO   2
#define EIGHT 8
#define CLINO 5
#define PORT  5000
#define PORT2 5001

int main(int argc, char **argv)
{
	unsigned int sock, bytes_read, addr_len;
	struct sockaddr_in server_addr, client_addr;
	struct hostent *host;
	char send_data[MAX], recv_data[MAX];

	unsigned int sock2, bytes_read2, addr_len2;
	struct sockaddr_in server_addr2, client_addr2;
	char send_data2[MAX], recv_data2[MAX];

	struct timeval tv, tv1;
	fd_set writefds, readfds;

	tv.tv_sec = 10;
	tv.tv_usec = 500000;
/*	tv1.tv_sec = 2;
	tv1.tv_usec = 500000;
*/
	if (argc != 2) {
		printf ("Usage: ./client <server_ip>\n");
		exit (EXIT_FAILURE);
	}
	host = (struct hostent *) gethostbyname((char *)argv[1]);
	
	if ((sock = socket (AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
		perror ("Socket error");
		exit (EXIT_FAILURE);
	}
	
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(PORT);
	server_addr.sin_addr = *((struct in_addr *)host->h_addr);
	bzero (&(server_addr.sin_zero), EIGHT);
	bzero (recv_data, MAX);
	
	if ((sock2 = socket (AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
		perror ("Socket2 error");
		exit (EXIT_FAILURE);
	}
	
	server_addr2.sin_family = AF_INET;
	server_addr2.sin_port = htons(PORT2);
	server_addr2.sin_addr = *((struct in_addr *)host->h_addr);
	bzero (&(server_addr2.sin_zero), EIGHT);
	bzero (recv_data2, MAX);
	
	FD_ZERO (&writefds);
	FD_SET (sock2, &writefds);
/*	FD_ZERO (&readfds);
	FD_SET (sock2, &readfds);
*/
	while (ONE) {
		printf ("Type Something for socket1 :");
		fgets (send_data, MAX, stdin);
		send_data [strlen (send_data) - ONE] = '\0';
		
		sendto(sock, send_data, strlen(send_data), ZERO ,
			(struct sockaddr *)&server_addr, sizeof(struct sockaddr));

		select (sock2+1, NULL, &writefds, NULL, &tv);
		if (FD_ISSET (sock2, &writefds)) {
			printf("Enter the data from the 2nd socket\n");
			fgets (send_data2, MAX, stdin);
			send_data2 [strlen (send_data2) - ONE] = '\0';
			sendto(sock2, send_data2, strlen(send_data2), ZERO ,
				(struct sockaddr *)&server_addr2, sizeof(struct sockaddr));
			tv.tv_sec = 10;
			tv.tv_usec = 500000;
		} else
			printf ("Timed out RTCP Send at client side\n");
	}
	return 0;
}




/*========= Server PGM =========================*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define MAX   1024
#define ZERO  0
#define ONE   1
#define TWO   2
#define EIGHT 8
#define CLINO 5
#define PORT  5000
#define PORT2 5001

int main(int argc, char **argv)
{
        int sock;
        unsigned int addr_len, bytes_read;
        char recv_data[MAX], send_data[MAX];
        struct sockaddr_in server_addr ,client_addr;
	
        int sock2;
        unsigned int addr_len2, bytes_read2;
        char recv_data2[MAX], send_data2[MAX];
        struct sockaddr_in server_addr2 ,client_addr2;

	struct timeval tv, tv1;
	fd_set readfds, writefds;

	tv.tv_sec = 5;
	tv.tv_usec = 500000;
/*	tv1.tv_sec = 2;
	tv1.tv_usec = 500000;
*/
	if (argc != 2) {
		printf ("Usage: ./server <client_ip>\n");
		exit (EXIT_FAILURE);
	}
	if ((sock = socket(AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
		perror("Socket error");
		exit(EXIT_FAILURE);
        }

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(PORT);
	server_addr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_addr.sin_zero), EIGHT);


	if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -ONE) {
		perror("Bind error");
		exit(EXIT_FAILURE);
	}

	addr_len = sizeof(struct sockaddr);
	
	if ((sock2 = socket(AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
		perror("Socket2 error");
		exit(EXIT_FAILURE);
        }

	server_addr2.sin_family = AF_INET;
	server_addr2.sin_port = htons(PORT2);
	server_addr2.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_addr2.sin_zero), EIGHT);


	if (bind(sock2, (struct sockaddr *)&server_addr2, sizeof(struct sockaddr)) == -ONE) {
		perror("Bind2 error");
		exit(EXIT_FAILURE);
	}

	FD_ZERO (&readfds);
	FD_SET (sock2, &readfds);
/*	FD_ZERO (&writefds);
	FD_SET (sock2, &writefds);
*/
	addr_len2 = sizeof(struct sockaddr);
	printf("\nUDP Server Waiting for client on port 5000");
        fflush(stdout);
	
		select (sock2+1, &readfds, NULL, NULL, &tv);
	while (ONE) {
		
		bytes_read = recvfrom(sock, recv_data, MAX, ZERO, (struct sockaddr *)&client_addr, &addr_len);
		recv_data[bytes_read] = '\0';
		printf("\n%s said : ", inet_ntoa(client_addr.sin_addr));
		printf("%s", recv_data);

		if (FD_ISSET (sock2, &readfds)) {
			bytes_read2 = recvfrom(sock2, recv_data2, MAX, ZERO, (struct sockaddr *)&client_addr2, &addr_len2);
			recv_data2[bytes_read2] = '\0';
			printf("\n%s said : ", inet_ntoa(client_addr2.sin_addr));
			printf("%s\n", recv_data2);
			printf ("Socket 2\n");
/*			tv.tv_sec = 2;
			tv.tv_usec = 500000;
*/		} else
			printf ("Timed out RTCP recv at server side\n");
		
/*		sleep (TWO);
		
		sendto(sock, recv_data, strlen(recv_data), ZERO,(struct sockaddr *)&client_addr, sizeof(struct sockaddr));
		select (sock2+1, NULL, &writefds, NULL, &tv1);
		if (FD_ISSET (sock2, &writefds)) {
			sendto(sock2, recv_data2, strlen(recv_data2), ZERO,(struct sockaddr *)&client_addr2, sizeof(struct sockaddr));
			tv1.tv_sec = 2;
			tv1.tv_usec = 500000;
		} else
			printf ("Timed out RTCP Send at server side\n");
*/
		fflush(stdout);
	}
	return ZERO;
}

blocking where?

socket1 is blocked at the client side until I provide some data to socket2, socket1 data is not accepted... I think select is not working on client side.. I was unable to find the bug.. can you fix this.. Try to run the code you'll come to know the blocking at client side.....

Hmmm.

First off:

send_data [strlen (send_data) - ONE] = '\0';

How could strlen possibly know the length of a string that doesn't already have a NULL?

Anyway, from man fgets:

fgets() reads in at most one less than size characters from stream  and
       stores  them  into  the buffer pointed to by s.  Reading stops after an
       EOF or a newline.  If a newline is read, it is stored into the  buffer.
       A '\0' is stored after the last character in the buffer.

So your data already had a NULL.

recv_data[bytes_read] = '\0';

This is perfectly fine though. You're not transmitting the NULL terminator, since strlen() doesn't count that in the length, so you need to add it.

As for your select problem, I reviewed some of my old threads in which I discovered that you have to FD_SET the file descriptors you want select() to check before you call select() every loop. select() clears things that have no data, but never, ever sets the ones that do.

Here's an example of how to use select().

I'll see if I have time to compile and test your program later

---------- Post updated at 12:24 PM ---------- Previous update was at 11:48 AM ----------

You can put more than one thing in a printf, you know. And it's really helpful to put in newlines so multiple statements don't get glued together. And when you do, you should be adding newlines to the END, not the BEGINNING.

//                printf("\n%s said : ", inet_ntoa(client_addr.sin_addr));
//                printf("%s", recv_data);
        printf("%s said : %s\n", inet_ntoa(client_addr.sin_addr), recv_data);

And when you learned that you that you should be using #define's instead of integers, I think you missed the point. MAX, MAX-1, STDIN_FILENO, and so forth are meaningful, but there's no point #defining ONE or TWO or ZERO. That doesn't tell you why you're using 0, making it unhelpful. And it stops you from changing it later -- MAX can be whatever you want, but FIVE sure can't!

---------- Post updated at 12:50 PM ---------- Previous update was at 12:24 PM ----------

You're not checking the return value of select, and should. I'll be posting a revised version soon.

---------- Post updated at 12:54 PM ---------- Previous update was at 12:50 PM ----------

Here ya go:

/*=========client PGM=========================*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define MAX   1024
#define CLINO 5
#define PORT  5000
#define PORT2 5001

int main(int argc, char **argv)
{
	const char *name="localhost";
	unsigned int sock, bytes_read, addr_len;
	struct sockaddr_in server_addr, client_addr;
	struct hostent *host;
	char send_data[MAX], recv_data[MAX];

	unsigned int sock2, bytes_read2, addr_len2;
	struct sockaddr_in server_addr2, client_addr2;
	char send_data2[MAX], recv_data2[MAX];

	struct timeval tv, tv1;
	fd_set writefds, readfds;

/*	tv1.tv_sec = 2;
	tv1.tv_usec = 500000;
*/

	// 'name' is already localhost, so localhost will be used with no args
	if(argc == 2)		name=argv[1];

	host = (struct hostent *) gethostbyname(name);
	
	if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror ("Socket error");
		exit (EXIT_FAILURE);
	}
	
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(PORT);
	server_addr.sin_addr = *((struct in_addr *)host->h_addr);
	bzero (&(server_addr.sin_zero), 8);
	bzero (recv_data, MAX);
	
	if ((sock2 = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror ("Socket2 error");
		exit (EXIT_FAILURE);
	}
	
	server_addr2.sin_family = AF_INET;
	server_addr2.sin_port = htons(PORT2);
	server_addr2.sin_addr = *((struct in_addr *)host->h_addr);
	bzero (&(server_addr2.sin_zero), 8);
	bzero (recv_data2, MAX);
	
	FD_ZERO (&writefds);
/*	FD_ZERO (&readfds);
	FD_SET (sock2, &readfds);
*/
	while (1) {
		int n;

		printf ("Type Something for socket1 :");
		fgets (send_data, MAX, stdin);
                // strlen doesn't work without a NULL!  send_data already has one!
		// send_data [strlen (send_data) - 1] = '\0';
		
		sendto(sock, send_data, strlen(send_data), 0 ,
			(struct sockaddr *)&server_addr, sizeof(struct sockaddr));

		// Need to set this every loop.  select() clears if
		// the socket's inactive, but never sets it for you.
		FD_SET (sock2, &writefds);

		// select() alters this, so must set this EVERY time.
		// pselect() is more polite.
		tv.tv_sec = 10;
		tv.tv_usec = 500000;
		n=select (sock2+1, NULL, &writefds, NULL, &tv);

		// use a continue instead of 9 levels of nesting
		if(n == 0)
		{
			printf("Timed out RTCP Send at client side\n");
			continue;
		}
		// use an exit() instead of 9 levels of nesting
		if(n < 0)
		{
			perror("select error'");
			exit(1);
		}

		// use a continue, instead of 9 levels of nesting
		if(!FD_ISSET (sock2, &writefds))
		{
			printf ("Timed out RTCP Send at client side\n");
			continue;
		}

		printf("Enter the data from the 2nd socket:  ");
		fgets (send_data2, MAX, stdin);
                // strlen doesn't work without a NULL!  send_data2 already has one!
		// send_data2 [strlen (send_data2) - 1] = '\0';
		sendto(sock2, send_data2, strlen(send_data2), 0 ,
			(struct sockaddr *)&server_addr2, sizeof(struct sockaddr));
	}
	return 0;
}
/*========= Server PGM =========================*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define MAX   1024
#define CLINO 5
#define PORT  5000
#define PORT2 5001

int main(int argc, char **argv)
{
	const char *name=NULL;
        int sock;
        unsigned int addr_len, bytes_read;
        char recv_data[MAX], send_data[MAX];
        struct sockaddr_in server_addr ,client_addr;
	
        int sock2;
        unsigned int addr_len2, bytes_read2;
        char recv_data2[MAX], send_data2[MAX];
        struct sockaddr_in server_addr2 ,client_addr2;

	struct timeval tv, tv1;
	fd_set readfds, writefds;

	tv.tv_sec = 5;
	tv.tv_usec = 500000;
/*	tv1.tv_sec = 2;
	tv1.tv_usec = 500000;
*/
	if (argc != 2) {
		printf ("Usage: ./server <client_ip>\n");
		exit (EXIT_FAILURE);
	}

	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("Socket error");
		exit(EXIT_FAILURE);
        }

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(PORT);
	server_addr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_addr.sin_zero), 8);


	if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
		perror("Bind error");
		exit(EXIT_FAILURE);
	}

	addr_len = sizeof(struct sockaddr);
	
	if ((sock2 = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("Socket2 error");
		exit(EXIT_FAILURE);
        }

	server_addr2.sin_family = AF_INET;
	server_addr2.sin_port = htons(PORT2);
	server_addr2.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_addr2.sin_zero), 8);


	if (bind(sock2, (struct sockaddr *)&server_addr2, sizeof(struct sockaddr)) == -1) {
		perror("Bind2 error");
		exit(EXIT_FAILURE);
	}

	addr_len2 = sizeof(struct sockaddr);
	printf("UDP Server Waiting for client on port 5000\n");
        fflush(stdout);

	FD_ZERO (&readfds);

	while (1) {
		int n;
		// Set this every loop, select() will clear if idle
		FD_SET (sock2, &readfds);

		// select actually ALTERS tv!  You must put these values 
		// in every loop.  pselect() is more polite.
		tv.tv_sec = 5;		tv.tv_usec = 500000;
		n=select(sock2+1, &readfds, NULL, NULL, &tv);

		// use an exit instead of 9 levels of nesting
		if(n< 0)
		{
			perror("select error");
			exit (1);
		}

		// use a continue instead of 9 levels of nesting
		if(n == 0)
		{
			printf("select() timeout\n");
			continue;
		}

		// n must be 1 or more, i.e. 1 or more sockets ready,
		// beyond this point.

		printf("%d fd's ready\n", n);
		
		bytes_read = recvfrom(sock, recv_data, MAX, 0, (struct sockaddr *)&client_addr, &addr_len);
		recv_data[bytes_read] = '\0';
		printf("%s said : %s\n", inet_ntoa(client_addr.sin_addr),
			recv_data);

		// use a continue instead of 9 levels of nesting
		if(!FD_ISSET(sock2, &readfds))
		{
			printf("sock2 not ready on server side\n");
			continue;
		}

		bytes_read2 = recvfrom(sock2, recv_data2, MAX, 0, (struct sockaddr *)&client_addr2, &addr_len2);
		recv_data2[bytes_read2] = '\0';
		printf("%s said : %s\n",
			inet_ntoa(client_addr2.sin_addr), recv_data2);
		printf ("Socket 2\n");
		
		// fflush is not necessary when you print \n on the end!
		//	fflush(stdout);
	}
	return 0;
}