Sponsored Content
Full Discussion: Socket and select
Top Forums Programming Socket and select Post 302535740 by Corona688 on Friday 1st of July 2011 02:54:52 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy