473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How a client will connect to multiple servers through UDP Socket (Linux 2.4) + timer

Hai to all frns,

Reqirement 1:

1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that corresponding ACK from that server within 2 sec.i need to send the Request again.

for this i did like this.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <time.h>
#include <sys/ioctl.h>
#include <unistd.h>
void error(char *);

int main()
{

int sockfd,length,res,max_iterations =3;
struct sockaddr_in server, from;
char buffer[256];
struct timeval timeout;
unsigned char flag=1,i;
int count =0 ;

fd_set readfd,testfd;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) error("socket");

server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(9734);
length = sizeof(struct sockaddr_in);

FD_ZERO(&readfd);
FD_SET(sockfd,&readfd);
FD_SET(0,&readfd);

printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);

while(flag && (max_iterations >0))
{

printf("sending the message %d time\n",++count);
res = sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&server,length);
if (res < 0) error("error in Sendto");

fprintf(stderr,"Waiting......\n");
testfd = readfd;
timeout.tv_sec =2;
timeout.tv_usec =500000;

/*select function call uses timeout value to prevent indefinite blocking of recvfrom ..timeout value is given using a struct timeval*/
res = select(FD_SETSIZE,&testfd,(fd_set*)0,(fd_set*)0,&t imeout);
//fprintf(stderr,"res= %d\n",res);
//sleep(3);

if(FD_ISSET(sockfd,&testfd))
{

//fprintf(stderr,"hi2");
res = recvfrom(sockfd,buffer,256,0,(struct sockaddr *)&from,&length);

if (res < 0)
{
error("error in recvfrom");
exit (1);
}
flag =0;
write(1,"Got an ack: ",12);
write(1,buffer,res);
}

max_iterations--;
}
close(sockfd);
}

void error(char *msg)
{
perror(msg);
exit(0);
}


Now my Requirement is ...

There are multiple servers(let us assume there are 4 servers)

for contacting each server Requirement 1 conditions should apply..In my packet i have one identifier also .to distinguish each server..

Here problem is if i have send a request to SERVER1...start the timer.....and send the request to SERVER2...start the timer with an interval of 2 sec if server1 is not responding with in 2sec time ..meanwhile server2 has replied now how i have to Recognise from which server it has come ....and also how to maintain timer functionality for all these different requests....

Some one suggested Thread Concept i have to use .But i dont knowhow to do it..

please help me..And thanks allot for showing patience to read my Post..

Reagards,
Keshav.
Feb 21 '07 #1
2 7597
Banfa
9,065 Expert Mod 8TB
Knowing who has repied is not a problem because you are using receive from so you know who (which server) have received the message from.

What is a problem is that recvfrom blocks for 2 seconds, more than enough time for another server to timeout.

Threads would be a way round it, you use a separate thread to monitor each server (and they are not that hard to use normally a function call to start them with a function pointer as an entry point).

Alternitively using a single thread you could set a much smaller timeout for recvfrom so that you can check every sever for a reply several times a second by using a loop.

Expand|Select|Wrap|Line Numbers
  1. REPEAT WHILE >0 CONNECTIONS
  2.    FOR EACH SERVER CONNECTION
  3.         WAIT FOR REPLY FOR 0.05 sec
  4.  
  5.         IF REPLY RECEIVED
  6.             CLOSE SERVER CONNECTION
  7.         ELSE IF MORE THAN 2 secs SINCE TRANSMISSION
  8.             RAISE ERROR
  9.             CLOSE SERVER CONNECTION
  10.         ENDIF
  11.     ENDFOR
  12. ENDREPEAT
  13.  
Feb 22 '07 #2
Knowing who has repied is not a problem because you are using receive from so you know who (which server) have received the message from.

What is a problem is that recvfrom blocks for 2 seconds, more than enough time for another server to timeout.

Threads would be a way round it, you use a separate thread to monitor each server (and they are not that hard to use normally a function call to start them with a function pointer as an entry point).

Alternitively using a single thread you could set a much smaller timeout for recvfrom so that you can check every sever for a reply several times a second by using a loop.

Expand|Select|Wrap|Line Numbers
  1. REPEAT WHILE >0 CONNECTIONS
  2.    FOR EACH SERVER CONNECTION
  3.         WAIT FOR REPLY FOR 0.05 sec
  4.  
  5.         IF REPLY RECEIVED
  6.             CLOSE SERVER CONNECTION
  7.         ELSE IF MORE THAN 2 secs SINCE TRANSMISSION
  8.             RAISE ERROR
  9.             CLOSE SERVER CONNECTION
  10.         ENDIF
  11.     ENDFOR
  12. ENDREPEAT
  13.  

Thanks buddy...i am trying to do it in the manner what u have suggested me.
thanks alott.
Feb 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: rdh | last post by:
Hi all, I am in process of developing a Server in C++ supporting multiple protocols. The server will be exposing various functionalities, and the clients can communicate over any of the...
2
by: Michel Esber | last post by:
Hello, I have an environment that contains V8 (FP 11) and V7 (FP13) Linux servers. One of our applications reads data from V8 servers and writes summarized data into V7 servers that can´t be...
11
by: Tor Erik | last post by:
Hi, The reason is that my application does about 16 connects and data transfers per second, to the same 16 remote hosts. After approx 200 secs there are 4000 sockets waiting to be garbage...
4
by: gaddamreddy | last post by:
Hai to all frns, Reqirement 1: 1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that...
5
by: ganeshp | last post by:
Hi , In Java using socket programming is it possible to have a server program on windows that services a client program on linux? I tried the code in the below given link:...
5
by: natambu | last post by:
I have a linux box with multiple ip addresses. I want to make my python client connect from one of the ip addresses. Here is my code, no matter what valid information I put in the bind it always...
3
by: dynamo08 | last post by:
Hi, I am new to socket programming, I wrote an ftpclient which connects to an ftp server and downloads a file to the client computer. Now in one client connection I want to download multiple files...
3
by: TsanChung | last post by:
I want to make a java TCP socket client to communicate with a TCP server socket on linux. Are there some sample C unix server and java client socket programs available? The Richard Stevens'...
3
by: Hukkky | last post by:
I'm testing simple server/client codes on linux. just server can wait for client's connect sign and accept, and client can't connect to server, this is all. There's no problems just for this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.