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.