472,119 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 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("");
server.sin_port = htons(9734);
length = sizeof(struct sockaddr_in);


printf("Please enter the message: ");

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");

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);


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);


void error(char *msg)

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

Feb 21 '07 #1
4 4872
3,237 Expert 2GB
I have moved this to the C/C++ forum, as I feel the experts there will be much more helpful.
Feb 21 '07 #2
1,510 Expert 1GB
have a look at this thread it may help
Feb 21 '07 #3
I have moved this to the C/C++ forum, as I feel the experts there will be much more helpful.
Thanks buddy
Feb 22 '07 #4
1,510 Expert 1GB
the simplest approach is to have a seperate thread to talk to each server (with seperate timers etc). have a look at
Feb 25 '07 #5

Post your reply

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

Similar topics

5 posts views Thread by natambu | last post: by
3 posts views Thread by Hukkky | last post: by
reply views Thread by leo001 | last post: by

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.