Socket and select


 
Thread Tools Search this Thread
Top Forums Programming Socket and select
# 1  
Old 06-30-2011
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.
Code:
/*=========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;
}


Last edited by Scott; 06-30-2011 at 03:42 PM.. Reason: Code tags PLEASE...
# 2  
Old 06-30-2011
blocking where?
# 3  
Old 07-01-2011
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.....
# 4  
Old 07-01-2011
Hmmm.

First off:
Code:
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:
Code:
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.

Code:
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.

Code:
//                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:

Code:
/*=========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;
}

Code:
/*========= 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;
}


Last edited by Corona688; 07-01-2011 at 04:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

2. Boot Loaders

Reboot and Select Proper Boot device or insert Boot media in select Boot device and press a key

Hello, I have kubuntu on my laptop and now I decided to switch to Windows 7. I made the bios settings properly (first choice is boot from cd\vd) but I see the error " reboot and select proper Boot device or insert Boot media in select Boot device and press a key " I have tried CD and... (0 Replies)
Discussion started by: rpf
0 Replies

3. Programming

I am a little bit confused in a socket example through select()

Hi, I am a newbie about network programming. All codes come from this book: UNIX Network Programming Volume 1, Third Edition. It is a TCP server by use select() int main(int argc, char **argv) { int i, maxi, maxfd, listenfd, connfd, sockfd; int ... (2 Replies)
Discussion started by: sehang
2 Replies

4. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

5. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

6. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

7. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

8. Programming

Multiplexing socket and message queue using Select()

I have a socket and a message queue over which i am trying to multiplex input using select(). When data comes over socket the select works but when it comes over message queue the select is not detecting it . Create_Q gets the identifier of the messege queue. ... (1 Reply)
Discussion started by: shaurya.rastogi
1 Replies

9. Programming

Anyone know how to use socket select() function?

hello socket programming expert, I having difficulties in understanding how select() function in socket programming work.... I'm trying to create my own peer-to-peer chat or file transfer program by using the select() function.... Therefore does anyone had any tutorial or source code that... (4 Replies)
Discussion started by: draggy
4 Replies

10. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies
Login or Register to Ask a Question