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("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
4 4872
Motoma
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
horace1
1,510 Expert 1GB
have a look at this thread it may help
http://www.thescripts.com/forum/thread598160.html
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
horace1
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
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
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.