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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaddamreddy
    New Member
    • Feb 2007
    • 4

    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,r es,max_iteratio ns =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_fami ly = 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,&readf d);

    printf("Please enter the message: ");
    bzero(buffer,25 6);
    fgets(buffer,25 5,stdin);

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

    printf("sending the message %d time\n",++count );
    res = sendto(sockfd,b uffer,strlen(bu ffer),0,(struct sockaddr *)&server,lengt h);
    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_SETSI ZE,&testfd,(fd_ set*)0,(fd_set* )0,&timeout);
    //fprintf(stderr, "res= %d\n",res);
    //sleep(3);

    if(FD_ISSET(soc kfd,&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.
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    I have moved this to the C/C++ forum, as I feel the experts there will be much more helpful.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      have a look at this thread it may help
      http://www.thescripts. com/forum/thread598160.ht ml

      Comment

      • gaddamreddy
        New Member
        • Feb 2007
        • 4

        #4
        Originally posted by Motoma
        I have moved this to the C/C++ forum, as I feel the experts there will be much more helpful.
        Thanks buddy

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          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/LinuxTutorialPo sixThreads.html

          Comment

          Working...