Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,308 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 831 people online right now. Registration is fast and FREE... Join Now!




how to write go-back-n protocal in c prgramming

 
Reply to this topicStart new topic

how to write go-back-n protocal in c prgramming

wongdong
6 Nov, 2008 - 09:24 AM
Post #1

New D.I.C Head
*

Joined: 5 Nov, 2008
Posts: 3

HI!
I want to kw how to write a go-back-n protocol
plz write a source code in C for me.
thx a lot.

I hv written stop-and-wait protocol but dont kw how to convert it into go-back-n.
code here:
CODE
#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

#define B1200  0x60
#define B2400  0x30
#define B4800  0x18
#define B9600  0x0c
#define B19200 0x06
#define PNONE  0x00
#define PEVEN  0x18        //bit 3,4
#define PODD   0x08        //bit 3
#define DATA7  0x02        //bit_end
#define DATA8  0x03        //bit0,1
#define STOP1  0x00
#define STOP2  0x40        //bit 2

#define PORT 0x3f8
#define TDR  PORT       //Transmit Data Reg
#define RDR  PORT       //Receive Data Reg
#define IER  PORT+1     //Interrupt Enable Reg
#define IIR  PORT+2     //Interrupt Identification Reg
#define LCR  PORT+3     //Line Control Reg
#define MCR  PORT+4
#define LSR  PORT+5     //Line Status Reg
#define PIC8259 0x21    //Mask Interrupt
#define IRQ4 0x0c

#define buffer_size 135
#define buffer_empty (head==tail)
#define ESC 0x1B
#define STX 0x02
#define EOT 0x04
#define ACK 0x06
#define NACK 0x08
#define END 0x10

char data;
char buffer[buffer_size];
int head=0;
int tail=0;

char rec_ch(void);
void sendchar(char ch);
void send_ch(char ch);
void interrupt (*oldhandler)();
void sendfile(time_t *t_end,time_t *t_start,double *fsize,int *numofframe,int *errorframe );
void receivefile(time_t *t_end,time_t *t_start,double *fsize,int *numofframe,int *errorframe );

void interrupt handler(void)
{
  unsigned char reg_IIR;
  unsigned char regLSR;
  disable();
  reg_IIR=inportb(IIR);
  reg_IIR&=0x06;                   //Read LSR, check line error
  if (reg_IIR==0x04)               //Data Received
  {                                //Read data from RDR
    do{
    regLSR=inportb(LSR);
    regLSR&=0x01;
    }    while(regLSR==0x00);
    buffer[head]=inportb(RDR);
    head++;

    if (head==buffer_size)
     head=0;
  }

  outportb(0x20,0x20);          //tell 8259 end of interrupt
  enable();                     //enable interrupt
}

void setcom_mode(unsigned char speed,unsigned char parity,
unsigned char data,unsigned char stop)
{
  unsigned char regLCR;
  regLCR=inportb(LCR);
  regLCR|=0x80;
  outportb(LCR,regLCR);
  outportb(TDR,speed);        //set baudrate
  outportb(IER,0x00);
  regLCR&=0x00;
  regLCR|=parity;
  regLCR|=data;
  regLCR|=stop;
  outportb(LCR,regLCR);
}

void setcom_int(void)
{
  unsigned char IRQmask;
  unsigned char regIER;
  oldhandler=getvect(IRQ4);         //save the old interrupt vector
  setvect(IRQ4,handler);            //install new interrupt handler
  outportb(MCR,0x0f);               //enable 8250 interrupt
  outportb(IER,0x00);
  IRQmask=inportb(PIC8259);
  IRQmask&=0xef;                    //enable COM1
  outportb(PIC8259,IRQmask);
}

void sendchar(char ch)
{
  unsigned char regIER;
  unsigned char regLSR;
  data=ch;
  do{
  regLSR=inportb(LSR);
  regLSR&=0x20;
  }  while(regLSR!=0x20);         //send next byte to THR
  outportb(PORT,ch);
}

/* receive character from terminal buffer */
char rec_ch()
{
  char ch;
  unsigned char regIER;
  int count=0;
  do{
  regIER=inportb(IER);
  regIER|=0x01;
  outportb(IER,regIER);
  count++;
  }  while(buffer_empty && count<15000);
  if (count<15000)
  {
  ch=buffer[tail];
  tail++;
  if (tail==buffer_size)
    tail=0;
  return(ch);
  }
  else
  {
    return(NULL);
  }
}



void sendfile(time_t *t_end,time_t *t_start,double *fsize,int *numofframe,int *errorframe )
{
    char ch,msg,already,frame[124],frame_num='0';
    int n,bcc=0,nbcc,numinarr=0,retransmit=0;
    FILE *in;
    char file_name[50];


    do{
         printf("Enter file name : ");
        scanf("%s", file_name);
        in = fopen(file_name, "r");

      }while(in== NULL);
  

    system("cls");
    setcom_int();
    setcom_mode(B9600,PNONE,DATA8,STOP1);              //intialize the com port

    while(1)
    {
          sendchar(ACK);
          if((ch=rec_ch())==ACK)
          {
            printf("Start sending file..\n");
            *t_start=time(NULL);                                     //the starting time of transmission
            break;
          }
          else
          {
            retransmit=retransmit+1;
            if (retransmit==200)                                        //limit the number of retransmit
             {    
                printf("\nTime out error!\n");
                return;
              }
          }
       }


    while(1)
    {

     if(((ch=rec_ch())==ACK)&&(!feof(in)))
     {
         retransmit =0;
         if(frame_num=='1')                               //change the frame no. for the checking of receiver
               frame_num='0';
         else if(frame_num=='0')
                 frame_num='1';
        
     for(n=0,numinarr=0,bcc=0;n<124;n++)
       {
       if (!feof(in))
         {
         msg=fgetc(in);

         if(msg!=-1)
           {
               frame[numinarr]=msg;          //save data in to the frame
               (*fsize)++;
               bcc+=frame[numinarr];      //sum of all data bits
             numinarr++;
        }
         }
       }
         nbcc=~bcc;         //2 complements
         nbcc++;

       sendchar(STX);
       sendchar(frame_num);
       sendchar(nbcc);
       printf("Frame %d Success\n", *numofframe);
       (*numofframe)++;
       for(n=0;n<numinarr;n++)
       {
        sendchar(frame[n]);           //send data bit of frame
       }
       sendchar(EOT);
       *t_end=time(NULL);
       }
       if((ch==NACK)||((difftime(time(NULL),*t_end))>1.0))
       {
      sendchar(STX);
      (*numofframe)++;   //accumulate no of frame we sent
      (*errorframe)++;     //accumulate no of error frame we sent
      sendchar(frame_num);
      sendchar(nbcc);
       for(n=0;n<numinarr;n++)
       {
        sendchar(frame[n]);                      //retrnasmit the frame which ocurr error before
       }
      sendchar(EOT);
       }
      if(feof(in))
     sendchar(END);
      if(ch==END)
      {
          *t_end=time(NULL);                // end time of transmission
          fclose(in);
          printf("\n\nThe transmission is completed.\n\n");
          return;
      }
      
    printf("retransmit : %d\n", retransmit);
    retransmit=retransmit+1;                               // calculate the no of retransmission
    if (retransmit==200)
      {     printf("\nTransmission time out error!\n");
        return;
      }
    
    }
  
}

void receivefile(time_t *t_end,time_t *t_start,double *fsize,int *noofframe,int *errorframe)
{
  char ch,msg,already,frame[124],bcc,nbcc,frame_num,seq_num='0';
  int n,numinarr,retransmit=0;
  FILE  *out;
  char file_name[50];

    do{
        printf("\nEnter file name: ");
        scanf("%s", file_name);
        out = fopen(file_name, "w");
    }while(out == NULL);
    
    system("cls");
    setcom_int();
    setcom_mode(B9600,PNONE,DATA8,STOP1);     //initialize the com port
    
     while(1)
     {

    ch = rec_ch();
    if (ch==ACK)
    {
    sendchar(ACK);
    printf("Receiving...\n");
    sendchar(ACK);
    *t_start=time(NULL);              // start of the receipt
    break;
    }
      else
      {
    retransmit=retransmit+1;
    if (retransmit==200)
      {     printf("\nRecieve time out error!\n");     //limit the number of retransmit
        return;
      }
      }
     }
     while(1)
     {
    if((ch=rec_ch())==STX)
        {
            retransmit=0;
            frame_num = rec_ch();
            nbcc = rec_ch();
            printf("Frame : %d success\n", *noofframe);
            (*noofframe)++;
            for(n=0,numinarr=0,bcc=0;n<124;n++)
            {
            ch=rec_ch();
            if (ch!=EOT)
                {
                    frame[numinarr]=ch;              // receive the data bit of frame
                    bcc+=frame[numinarr];          //sum of the data bit
                    numinarr++;
                }
            else
              break;
            }
            nbcc=~nbcc;               // 2 complement
            nbcc++;
            if (bcc==nbcc)           //error checking (BCC)
            {
                if (frame_num!=seq_num)
                {
                  seq_num=frame_num;
                  for (n=0;n<numinarr;n++, (*fsize)++)
                  {
                  fputc(frame[n], out);            // put the data into the file
                  }

                  for(n=0;n<numinarr;n++)
                  frame[n]=NULL;                  
                }
            sendchar(ACK);
            }
        else
            {
            sendchar(NACK);
            (*errorframe)++;                          // send Negative ACK to transmitor if error is occured
            }
            if (ch==EOT)
                   ch=rec_ch();
        }
  
    if ((ch==END))
    {
        *t_end=time(NULL);      //end of the receipt
        sendchar(END);            
        fclose(out);
        printf("\n\nRecevicing file complete\n\n");

          return;
    }
       else
       {
    retransmit=retransmit+1;
    if (retransmit==200)
      {     printf("\nRecieve time out error!\n");          // calculate the no of retransmission
        return;
      }

       }
      }


}
void main()
{
    char indicator;
    time_t t_end=0,t_start=0;
    double fsize=0;
    int numofframe=0;
    int errorframe=0;

    system("cls");

    printf(   "If you want to send file, Press S             \n");
    printf(   "If you are going to receive file, Press R          \n");

    scanf("%c",&indicator);
    if (indicator=='S'  ||indicator=='s' )
        sendfile(&t_end, &t_start, &fsize, &numofframe, &errorframe);
    else if (indicator=='R' || indicator=='r' )
        receivefile(&t_end, &t_start, &fsize, &numofframe, &errorframe);

/* Show output information*/

    printf("\n==================================\n");
    printf("Total Time consumed: %f s\n",difftime(t_end,t_start));
    printf("Total size of the file: %f bytes\n",fsize);
    printf("Total no. of frame : %d\n",numofframe);
    printf("Total no. of frame without error: %d\n", numofframe-errorframe);
    printf("Total no. of frame with error: %d\n",errorframe);
    printf("\n==================================\n");
      setvect(IRQ4,oldhandler);  //reset the old int. handler


    system("PAUSE");

}


This post has been edited by wongdong: 7 Nov, 2008 - 07:28 AM

User is offlineProfile CardPM
+Quote Post


gabehabe
RE: How To Write Go-back-n Protocal In C Prgramming
6 Nov, 2008 - 11:16 AM
Post #2

i > u
Group Icon

Joined: 6 Feb, 2008
Posts: 7,381



Thanked: 154 times
Dream Kudos: 2850
Expert In: Lots of things.

My Contributions
No.

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:05PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month