Sponsored Content
Full Discussion: Socket and select
Top Forums Programming Socket and select Post 302535262 by naresh046 on Thursday 30th of June 2011 08:55:47 AM
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...
 

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 06:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy