LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Server/Client Go-Back-N (https://www.linuxquestions.org/questions/programming-9/server-client-go-back-n-4175433812/)

crazystudentc 10-24-2012 04:51 AM

Server/Client Go-Back-N
 
Hi, I have a little side project I have to do, but for the life of me I cant figure out the information.

Ive got the following two source codes

Server1.c
Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>


#define MYPORT "4951"       

// This is the size of our receiving buffer
#define MAXBUFLEN 100


int main(void)
{
        int      sockfd;
        struct addrinfo hints, *servinfo, *p;
        int      rv;
        int      numbytes;
        struct    sockaddr_storage their_addr;
        char      buf[MAXBUFLEN];
        socklen_t addr_len;

       
        memset(&hints, 0, sizeof hints);
        hints.ai_family  = AF_UNSPEC; 
        hints.ai_socktype = SOCK_DGRAM;
        hints.ai_flags    = AI_PASSIVE;

        // COMMENT -- What does the following function do?
        // COMMENT -- Why is the first parameter to getaddrinfo NULL?
        if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
                return 1;
        }

       
        for(p = servinfo; p != NULL; p = p->ai_next)
        {
                if ((sockfd = socket(p->ai_family, p->ai_socktype,
                                p->ai_protocol)) == -1) {
                        perror("listener: socket");
                        continue;
                }

                if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
                        close(sockfd);
                        perror("listener: bind");
                        continue;
                }

                break;
        } // END FOR

        if (p == NULL) {
                fprintf(stderr, "listener: failed to bind socket\n");
                return 2;
        }

        freeaddrinfo(servinfo);

        printf("server: waiting to recvfrom...\n");

 
   
      while(1)
      {
       
        addr_len = sizeof their_addr;
        if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
                (struct sockaddr *)&their_addr, &addr_len)) == -1)
        {
                perror("recvfrom");
                exit(1);
        }

       
        buf[numbytes] = '\0';
        printf("server: received... \"%s\"\n", buf);

     
 

      } // END WHILE -- the main loop

      //  -----------------------------------------------------------------------
      // Close the socket to the channel.
      close(sockfd);

      return 0;
}

and client1.c
Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>

// The channel's port
#define CHANNELPORT "4950"
// Our receiving buffer length
#define MAXBUFLEN  100
// Size of our frame
#define MAXMESGLEN  1

int main(int argc, char *argv[])
{
        int sockfd;                         
        struct addrinfo hints, *servinfo, *p;
        int rv;                             
        int numbytes;                       
        socklen_t addr_len;                 
        struct sockaddr_storage their_addr; 

        char buf[MAXBUFLEN];                 
        char myframe[MAXMESGLEN];           

        char *mesg = "A";                    // Message to be sent

       
        fd_set fds;
        int rc;
        struct timeval timeout;


   
        memset(&hints, 0, sizeof hints);
        hints.ai_family  = AF_UNSPEC;       
        hints.ai_socktype = SOCK_DGRAM;     

       
        if ((rv = getaddrinfo("127.0.0.1", CHANNELPORT, &hints, &servinfo)) != 0) {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
                return 1;
        }

       
        for(p = servinfo; p != NULL; p = p->ai_next)
        {
                if ((sockfd = socket(p->ai_family, p->ai_socktype,
                                p->ai_protocol)) == -1) {
                        perror("client: socket");
                        continue;
                }

                break;
        }

     
        if (p == NULL) {
                fprintf(stderr, "client: failed to bind socket\n");
                return 2;
        }

        myframe[0] = mesg[0];

     
        if ((numbytes = sendto(sockfd, myframe, MAXMESGLEN, 0,
                              p->ai_addr, p->ai_addrlen)) == -1)
        {
                perror("talker: sendto");
                exit(1);
        }

        printf("Done Sending: %d\n", numbytes);

   
        freeaddrinfo(servinfo);
        close(sockfd);
        return 0;
}

Basically I have this code compiled, all works, Im using IdealChannel and EvilChannel, which are two channels im meant to test things on.


In My first step which im stuck on, is I need to modify the above codes to include Go-Back-N. I have to transfer the letters in the char *mesg = "A", and I also have to edit char* mesg so it send a string of ABCD, but sending one letter at a time, and transfer the letters using go-back-n as well.

I was wondering how exactly am I supposed to do that, and how do I then calculate the average round Trip Time of the channel.

I really want to understand this, as then it will help me in the future with my actaul assignments as i know similar things will be coming up soon.

Regards

crazystudentc 10-24-2012 07:55 AM

I have modified client.c to send a large string, but the rest so far i have problems with

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>

// The channel's port -- do not change
#define CHANNELPORT "4950"
// Our receiving buffer length
#define MAXBUFLEN  100
// Size of our frame
#define MAXMESGLEN  1

int main(int argc, char *argv[])
{
        int sockfd;                         
        struct addrinfo hints, *servinfo, *p;
        int rv;                             
        int numbytes;                       
        socklen_t addr_len;                 
        struct sockaddr_storage their_addr; 

        char buf[MAXBUFLEN];                 
        char myframe[MAXMESGLEN];           

        char *mesg = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123";                   
        int i;

        // used for select
        fd_set fds;
        int rc;
        struct timeval timeout;


        memset(&hints, 0, sizeof hints);
        hints.ai_family  = AF_UNSPEC;        // COMMENT
        hints.ai_socktype = SOCK_DGRAM;      // COMMENT

        if ((rv = getaddrinfo("127.0.0.1", CHANNELPORT, &hints, &servinfo)) != 0) {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
                return 1;
        }

        // COMMENT
        for(p = servinfo; p != NULL; p = p->ai_next)
        {
                if ((sockfd = socket(p->ai_family, p->ai_socktype,
                                p->ai_protocol)) == -1) {
                        perror("client: socket");
                        continue;
                }

                break;
        }

        // If we can't get a socket ...
        if (p == NULL) {
                fprintf(stderr, "client: failed to bind socket\n");
                return 2;
        }

   
        for(i=0;i<30;i++)       
        {
                myframe[0] = mesg[i];

                      // COMMENT
                if ((numbytes = sendto(sockfd, myframe, MAXMESGLEN, 0,
                              p->ai_addr, p->ai_addrlen)) == -1)
                {
                        perror("talker: sendto");
                        exit(1);
                }       

                printf("Done Sending: %d\n", numbytes);
        }
       
     
        freeaddrinfo(servinfo);
        close(sockfd);
        return 0;
}



All times are GMT -5. The time now is 05:02 PM.