/* ********************************************************
*
* sender2.c
*
********************************************************** */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>

#define LOCALPORT 2000 + (100*7) + 6
#define LOCALPORT1 2000 + (100*7) + 1
#define LOCALPORT2 2000 + (100*7) + 2
#define LOCALPORT3 2000 + (100*7) + 3
#define LOCALPORT4 2000 + (100*7) + 4
#define LOCALPORT5 2000 + (100*7) + 5
#define MAXSENDERBUFFER 64
#define LOCALIP "xxx.xx.xxx.xx"
#define THISIP "xxx.xx.xxx.xx"
#define FILESTODO 10
#define MAXRETRY 4
#define TIMEOUT 150

int conversion();
void updatethreads();
void delay();

int numbytes1;
int ackreceived = 0;
int filesdone = 0;
int retrytimes1 = 0;

int run1,run2,run3,run4,run5 = 0;

FILE *ifp1;

char clean[MAXSENDERBUFFER] = "";
char rbuf1[MAXSENDERBUFFER] = "";
char rbuf2[MAXSENDERBUFFER] = "";
char rbuf3[MAXSENDERBUFFER] = "";
char rbuf4[MAXSENDERBUFFER] = "";
char rbuf5[MAXSENDERBUFFER] = "";

char buf1[MAXSENDERBUFFER] = "";
char buf2[MAXSENDERBUFFER] = "";
char buf3[MAXSENDERBUFFER] = "";
char buf4[MAXSENDERBUFFER] = "";
char buf5[MAXSENDERBUFFER] = "";

char file_name[MAXSENDERBUFFER] = "";
char file_name1[MAXSENDERBUFFER] = "";

char gargvv[MAXSENDERBUFFER] = "";

int localtimeout = 0;
int packectcounter = 0;
int totalbytessent = 0;
int x = 0;

char c;

void thread_1(int *);
void thread_2(int *);
void thread_3(int *);
void thread_4(int *);
void thread_5(int *);

void routing_table();
void state_transition_diagram(int x, int y, int z);
void displaypc();

pthread_t thread1, thread2, thread3, thread4, thread5;

int n1 = 0;
int h, k = 0;
int runningthreads = 0;

int rt1[50] = {0};
int rt2[50] = {0};
int rt3[50] = {0};
int rt4[50] = {0};
int rt5[50] = {0};
int xx = 0;

int pc1, pc2, pc3, pc4,pc5, tpc = 0;
int pc1a, pc2a, pc3a, pc4a,pc5a, tpca = 0;

int pc1r, pc2r, pc3r, pc4r, pc5r, tpcr = 0;

/* main communication setup */
/* ------------------------ */
int sockfd, sockfd1, sockfd11, sockfd2, sockfd3, sockfd4, sockfd5;

struct sockaddr_in their_addr;
struct sockaddr_in theirs_addr;

struct sockaddr_in theirs_addr2;
struct sockaddr_in theirs2_addr2;
struct sockaddr_in their_addr2;
struct sockaddr_in their2_addr2;

struct sockaddr_in theirs_addr3;
struct sockaddr_in theirs3_addr3;
struct sockaddr_in their_addr3;
struct sockaddr_in their3_addr3;

struct sockaddr_in theirs_addr4;
struct sockaddr_in theirs4_addr4;
struct sockaddr_in their_addr4;
struct sockaddr_in their4_addr4;

struct sockaddr_in theirs_addr5;
struct sockaddr_in theirs5_addr5;
struct sockaddr_in their_addr5;
struct sockaddr_in their5_addr5;

int addr_len, addr_len1, addr_len2, addr_len3, addr_len4, addr_len5, numbytes;

/* =============================================================== */
main(int argc, char *argv[], char *argvv[])
{
system("clear");
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("Main Socket Setup Error...");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(LOCALPORT);
their_addr.sin_addr.s_addr = inet_addr(LOCALIP);
bzero(&(their_addr.sin_zero),8);

/* three way handshaks algorithm implementation */
/* -------------------------------------------- */
strcpy(buf1,"0");
if ((numbytes = sendto(sockfd,buf1,strlen(buf1),0,
(struct sockaddr *)&their_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending: SYN, SequenceNum = x");
exit(1);
}
printf("\n ---------------------------------------------------------------");
printf("\n ss: Connection Establishment: Three-Way Handshake for Project 2");
printf("\n ss: Sending : SYN, SequenceNum = x");
/* --------------------------------------------- */
addr_len = sizeof(struct sockaddr);
if ((numbytes=recvfrom(sockfd,rbuf1,MAXSENDERBUFFER,0,
(struct sockaddr *)&their_addr, &addr_len))== -1)
{
perror("ss: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
printf("\n ss: Receiving : SYN+ACK, SequenceNum = y");
/* --------------------------------------------- */
strcpy(buf1,"1");
if ((numbytes = sendto(sockfd,buf1,strlen(buf1),0,
(struct sockaddr *)&their_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending: ACK, Acknowlegment = y");
exit(1);
}
printf("\n ss: Sending : ACK, Acknowlegment = y + 1");
printf("\n --------------------------------------------------------------- \n");
/* end three way handshaks algorithm implementation */
/* ------------------------------------------------ */

/* delay */
/* ----- */
delay();

if (strcmp(argv[2],"1")==0)
strcpy(gargvv,"1");
if (strcmp(argv[2],"2")==0)
strcpy(gargvv,"2");
if (strcmp(argv[2],"3")==0)
strcpy(gargvv,"3");
if (strcmp(argv[2],"4")==0)
strcpy(gargvv,"4");
if (strcmp(argv[2],"5")==0)
strcpy(gargvv,"5");

printf("\n -> From Main (gargvv) : %s <-",gargvv);
printf("\n -> From Main argv[1] (Number of Thread to Run) : %s <-",argv[1]);
printf("\n -> From Main argv[2] (File to transfer on each Thread): %s <- \n",argv[2]);

/* activate threads */
/* ---------------- */
if (strcmp(argv[1],"1")==0)
{
if(pthread_create(&thread1,NULL,thread_1,(void *)&n1)!=0)
{
perror("ss: thread1 creation error");
exit(1);
}
run1 = 1;
updatethreads();
printf("\n ss: Activating thread1 with ID: %ld",(long
int)&thread1);
}

if (strcmp(argv[1],"2")==0)
{
if(pthread_create(&thread1,NULL,thread_1,(void *)&n1)!=0)
{
perror("ss: thread1 creation error");
exit(1);
}
printf("\n ss: Activating thread1 with ID: %ld",(long
int)&thread1);
if(pthread_create(&thread2,NULL,thread_2,(void *)&n1)!=0)
{
perror("ss: thread2 creation error");
exit(1);
}
printf("\n ss: Activating thread2 with ID: %ld",(long
int)&thread2);

run1 = 1;
run2 = 1;
updatethreads();
}

if (strcmp(argv[1],"3")==0)
{
if(pthread_create(&thread1,NULL,thread_1,(void *)&n1)!=0)
{
perror("ss: thread1 creation error");
exit(1);
}
printf("\n ss: Activating thread1 with ID: %ld",(long
int)&thread1);
if(pthread_create(&thread2,NULL,thread_2,(void *)&n1)!=0)
{
perror("ss: thread2 creation error");
exit(1);
}
printf("\n ss: Activating thread2 with ID: %ld",(long
int)&thread2);
if(pthread_create(&thread3,NULL,thread_3,(void *)&n1)!=0)
{
perror("ss: thread3 creation error");
exit(1);
}
printf("\n ss: Activating thread3 with ID: %ld",(long
int)&thread3);
run1 = 1;
run2 = 1;
run3 = 1;
updatethreads();
}

if (strcmp(argv[1],"4")==0)
{
if(pthread_create(&thread1,NULL,thread_1,(void *)&n1)!=0)
{
perror("ss: thread1 creation error");
exit(1);
}
printf("\n ss: Activating thread1 with ID: %ld",(long
int)&thread1);
if(pthread_create(&thread2,NULL,thread_2,(void *)&n1)!=0)
{
perror("ss: thread2 creation error");
exit(1);
}
printf("\n ss: Activating thread2 with ID: %ld",(long
int)&thread2);
if(pthread_create(&thread3,NULL,thread_3,(void *)&n1)!=0)
{
perror("ss: thread3 creation error");
exit(1);
}
printf("\n ss: Activating thread3 with ID: %ld",(long
int)&thread3);
if(pthread_create(&thread4,NULL,thread_4,(void *)&n1)!=0)
{
perror("ss: thread4 creation error");
exit(1);
}
printf("\n ss: Activating thread4 with ID: %ld",(long
int)&thread4);
run1 = 1;
run2 = 1;
run3 = 1;
run4 = 1;
updatethreads();
}
if (strcmp(argv[1],"5")==0)
{
if(pthread_create(&thread1,NULL,thread_1,(void *)&n1)!=0)
{
perror("ss: thread1 creation error");
exit(1);
}
printf("\n ss: Activating thread1 with ID: %ld",(long
int)&thread1);
if(pthread_create(&thread2,NULL,thread_2,(void *)&n1)!=0)
{
perror("ss: thread2 creation error");
exit(1);
}
printf("\n ss: Activating thread2 with ID: %ld",(long
int)&thread2);
if(pthread_create(&thread3,NULL,thread_3,(void *)&n1)!=0)
{
perror("ss: thread3 creation error");
exit(1);
}
printf("\n ss: Activating thread3 with ID: %ld",(long
int)&thread3);
if(pthread_create(&thread4,NULL,thread_4,(void *)&n1)!=0)
{
perror("ss: thread4 creation error");
exit(1);
}
printf("\n ss: Activating thread4 with ID: %ld",(long
int)&thread4);
if(pthread_create(&thread5,NULL,thread_5,(void *)&n1)!=0)
{
perror("ss: thread5 creation error");
exit(1);
}
printf("\n ss: Activating thread5 with ID: %ld",(long
int)&thread5);
run1 = 1;
run2 = 1;
run3 = 1;
run4 = 1;
run5 = 1;
updatethreads();
}

while ((run1!=0)||(run2!=0)||(run3!=0)||(run4!=0)||(run5!=0))
{
}
displaypc();
updatethreads();
return(0);
}
/* =============================================================== */

void thread_1(int *n)
{
int x = 0;
FILE *ifp1;

printf("\n -> Inside Thread 1 (gargvv): %s <- \n",gargvv);
if (strcmp(gargvv,"1")==0)
ifp1 = fopen("send_client1_1","r");
if (strcmp(gargvv,"2")==0)
ifp1 = fopen("send_client1_2","r");
if (strcmp(gargvv,"3")==0)
ifp1 = fopen("send_client1_3","r");
if (strcmp(gargvv,"4")==0)
ifp1 = fopen("send_client1_4","r");
if (strcmp(gargvv,"5")==0)
ifp1 = fopen("send_client1_5","r");

if((sockfd1 = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("Main Socket 1 Setup Error...");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(LOCALPORT1);
their_addr.sin_addr.s_addr = inet_addr(THISIP);
bzero(&(their_addr.sin_zero),8);

theirs_addr.sin_family = AF_INET;
theirs_addr.sin_port = htons(LOCALPORT1);
theirs_addr.sin_addr.s_addr = inet_addr(LOCALIP);
bzero(&(theirs_addr.sin_zero),8);

if(bind(sockfd1,(struct sockadd *)&their_addr,sizeof(struct sockaddr)) == -1)
{
perror("\n ss: t1: Binding Error in Thread 1 ");
exit(1);
}

/* state transition diagram */
state_transition_diagram(1,1,0);
/* state transition diagram */

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */

/* state transition diagram */
state_transition_diagram(31,1,0);
/* state transition diagram */

printf("\n -----------------------------------------------------------------------");
printf("\n ss: t1: Connection Establishment: Three-Way Handshake for Thread %d",(long int)&thread1);
printf("\n ss: t1: Sending : SYN, SequenceNum = x");
buf1[x] = '0';
if((numbytes = sendto(sockfd1,buf1,strlen(buf1),0,
(struct sockaddr *)&theirs_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 1: SYN, SequenceNum = X ");
exit(1);
}
printf("\n ss: t1: Receiving : SYN+ACK, SequenceNum = y");
pc1a++;

/* state transition diagram */
state_transition_diagram(2,1,0);
/* state transition diagram */

addr_len1 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd1,rbuf1,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs_addr,&addr_len1))==-1)
{
perror("\n ss: t1: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc1r++;
printf("\n ss: t1: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(31,1,0);
/* state transition diagram */

buf1[x] = '1';
if((numbytes = sendto(sockfd1,buf1,strlen(buf1),0,
(struct sockaddr *)&theirs_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: t1: Error Sending from thread 1: ACK, Ack = y + 1");
exit(1);
}
pc1a++;
/* ------------------------------- */
/* ------------------------------- */

printf("\n ss t1: Thread %d has been activated\n",(long
int)&thread1);

/* state transition diagram */
state_transition_diagram(4,1,0);
/* state transition diagram */

while ((c=getc(ifp1)) != EOF)
{
buf1[x] = c;
if((numbytes = sendto(sockfd1,buf1,strlen(buf1),0,
(struct sockaddr *)&theirs_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 1");
exit(1);
}
pc1++;
pc1a++;
/* printf("%s",buf1); */

addr_len1 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd1,rbuf1,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs_addr,&addr_len1))==-1)
{
perror("\n ss: t1: Timeout - Receive ack from Receiver t1");
exit(1);
}
pc1r++;
} /* end while loop EOF */

/* state transition diagram */
state_transition_diagram(5,1,0);
/* state transition diagram */

strcpy(buf1, "111110End111110Of111110File111110");
if ((numbytes = sendto(sockfd1,buf1,strlen(buf1),0,
(struct sockaddr *)&theirs_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending EOF marker from thread 1");
exit(1);
}
pc1a++;

printf("\n ss t1: Sending from thread: 1");
printf("\n ss t1: Sending from port: %d",LOCALPORT1);
printf("\n ss t1: Sending to: %s ",inet_ntoa(their_addr.sin_addr));
printf("\n ss t1: Finish Sending: %s",buf1);
printf("\n --------------------------------------------- \n");

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t1: Connection Termination : Three-Way Handshake for Thread %d",(long int)&thread1);
printf("\n ss: t1: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(31,1,0);
/* state transition diagram */

buf1[x] = '0';
if((numbytes = sendto(sockfd1,buf1,strlen(buf1),0,
(struct sockaddr *)&theirs_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 1: SYN, SequenceNum = X ");
exit(1);
}
pc1a++;
printf("\n ss: t1: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(3,1,0);
/* state transition diagram */

addr_len1 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd1,rbuf1,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs_addr,&addr_len1))==-1)
{
perror("\n ss: t1: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc1r++;
printf("\n ss: t1: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(34,1,0);
/* state transition diagram */

buf1[x] = '1';
if((numbytes = sendto(sockfd1,buf1,strlen(buf1),0,
(struct sockaddr *)&theirs_addr,sizeof(struct sockaddr))) == -1)
{
perror("ss: t1: Error Sending from thread 1: ACK, Ack = y + 1");
exit(1);
}
pc1a++;
/* ------------------------------- */
/* ------------------------------- */

rt1[xx]=(long int) &thread1;
rt3[xx]=LOCALPORT1;
rt5[xx]=LOCALPORT1;
xx++;
routing_table();

close(sockfd1);
close(ifp1);
updatethreads();
run1 = 0;
}

void thread_2(int *n)
{
int x2 = 0;

FILE *ifp2;

printf("\n -> Inside Thread 2 (gargvv): %s <- \n",gargvv);
if (strcmp(gargvv,"1")==0)
ifp2 = fopen("send_client2_1","r");
if (strcmp(gargvv,"2")==0)
ifp2 = fopen("send_client2_2","r");
if (strcmp(gargvv,"3")==0)
ifp2 = fopen("send_client2_3","r");
if (strcmp(gargvv,"4")==0)
ifp2 = fopen("send_client2_4","r");
if (strcmp(gargvv,"5")==0)
ifp2 = fopen("send_client2_5","r");

printf("\n Trying to Start Three-Way Handshake Algorithm in thread: %d ... \n",(long int)&thread2);
delay();
delay();

if((sockfd2 = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("Main Socket 2 Setup Error...");
exit(1);
}
their2_addr2.sin_family = AF_INET;
their2_addr2.sin_port = htons(LOCALPORT2);
their2_addr2.sin_addr.s_addr = inet_addr(THISIP);
bzero(&(their2_addr2.sin_zero),8);

theirs2_addr2.sin_family = AF_INET;
theirs2_addr2.sin_port = htons(LOCALPORT2);
theirs2_addr2.sin_addr.s_addr = inet_addr(LOCALIP);
bzero(&(theirs2_addr2.sin_zero),8);

if(bind(sockfd2,(struct sockadd *)&their2_addr2,sizeof(struct sockaddr)) == -1)
{
perror("\n ss: t2: Binding Error in Thread 2 ");
exit(1);
}

/* state transition diagram */
state_transition_diagram(1,2,0);
/* state transition diagram */

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t2: Connection Establishment: Three-Way Handshake for Thread %d",(long int)&thread2);
printf("\n ss: t2: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(31,2,0);
/* state transition diagram */

buf2[x] = '0';
if((numbytes = sendto(sockfd2,buf2,strlen(buf2),0,
(struct sockaddr *)&theirs2_addr2,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 2: SYN, SequenceNum = X ");
exit(1);
}
pc2a++;
printf("\n ss: t2: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(3,2,0);
/* state transition diagram */

addr_len2 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd2,rbuf2,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs2_addr2,&addr_len2))==-1)
{
perror("\n ss: t2: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc2r++;
printf("\n ss: t2: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(31,2,0);
/* state transition diagram */

buf2[x] = '1';
if((numbytes = sendto(sockfd2,buf2,strlen(buf2),0,
(struct sockaddr *)&theirs2_addr2,sizeof(struct sockaddr))) == -1)
{
perror("ss: t2: Error Sending from thread 2: ACK, Ack = y + 1");
exit(1);
}
pc2a++;
/* ------------------------------- */
/* ------------------------------- */

/* state transition diagram */
state_transition_diagram(4,2,0);
/* state transition diagram */

printf("\n ss t2: Thread %d has been activated.\n",(long
int)&thread2);
while ((c=getc(ifp2)) != EOF)
{
buf2[x2] = c;
if((numbytes = sendto(sockfd2,buf2,strlen(buf2),0,
(struct sockaddr *)&theirs2_addr2,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 2");
exit(1);
}
pc2++;
pc2a++;
/* printf("%s",buf2); */

addr_len2 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd2,rbuf2,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs2_addr2,&addr_len2))==-1)
{
perror("\n ss: t2: Timeout - Receive ack from Receiver t2");
exit(1);
}
pc2r++;

} /* end while loop EOF */

/* state transition diagram */
state_transition_diagram(5,2,0);
/* state transition diagram */

strcpy(buf2, "111110End111110Of111110File111110");
if ((numbytes = sendto(sockfd2,buf2,strlen(buf2),0,
(struct sockaddr *)&theirs2_addr2,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending EOF marker from thread 2");
exit(1);
}
pc2a++;

printf("\n ss t2: Sending from thread: 2");
printf("\n ss t2: Sending from port: %d",LOCALPORT2);
printf("\n ss t2: Sending to: %s ",inet_ntoa(their2_addr2.sin_addr));
printf("\n ss t2: Finish Sending: %s",buf2);
printf("\n --------------------------------------------- \n");

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t2: Connection Termination : Three-Way Handshake for Thread %d",(long int)&thread2);
printf("\n ss: t2: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(7,2,0);
/* state transition diagram */

buf2[x] = '0';
if((numbytes = sendto(sockfd2,buf2,strlen(buf2),0,
(struct sockaddr *)&theirs2_addr2,sizeof(struct sockaddr))) == -1)
{
perror("ss: ts: Error Sending from thread 2: SYN, SequenceNum = X ");
exit(1);
}
pc2a++;
printf("\n ss: t2: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(8,2,0);
/* state transition diagram */

addr_len2 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd2,rbuf2,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs2_addr2,&addr_len2))==-1)
{
perror("\n ss: t2: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc2r++;
printf("\n ss: t2: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(34,2,0);
/* state transition diagram */

buf2[x] = '1';
if((numbytes = sendto(sockfd2,buf2,strlen(buf2),0,
(struct sockaddr *)&theirs2_addr2,sizeof(struct sockaddr))) == -1)
{
perror("ss: t2: Error Sending from thread 2: ACK, Ack = y + 1");
exit(1);
}
pc2a++;
/* ------------------------------- */
/* ------------------------------- */

rt1[xx]=(long int) &thread2;
rt3[xx]=LOCALPORT2;
rt5[xx]=LOCALPORT2;
xx++;
routing_table();

close(sockfd2);
close(ifp2);
updatethreads();
run2 = 0;
}

void thread_3(int *n)
{
int x3 = 0;

FILE *ifp3;

printf("\n -> Inside Thread 3 (gargvv): %s <- \n",gargvv);
if (strcmp(gargvv,"1")==0)
ifp3 = fopen("send_client3_1","r");
if (strcmp(gargvv,"2")==0)
ifp3 = fopen("send_client3_2","r");
if (strcmp(gargvv,"3")==0)
ifp3 = fopen("send_client3_3","r");
if (strcmp(gargvv,"4")==0)
ifp3 = fopen("send_client3_4","r");
if (strcmp(gargvv,"5")==0)
ifp3 = fopen("send_client3_5","r");

printf("\n Trying to Start Three-Way Handshake Algorithm in thread: %d ... \n",(long int)&thread3);
delay();
delay();
delay();

if((sockfd3 = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("Main Socket 3 Setup Error...");
exit(1);
}
their3_addr3.sin_family = AF_INET;
their3_addr3.sin_port = htons(LOCALPORT3);
their3_addr3.sin_addr.s_addr = inet_addr(THISIP);
bzero(&(their3_addr3.sin_zero),8);

theirs3_addr3.sin_family = AF_INET;
theirs3_addr3.sin_port = htons(LOCALPORT3);
theirs3_addr3.sin_addr.s_addr = inet_addr(LOCALIP);
bzero(&(theirs3_addr3.sin_zero),8);

if(bind(sockfd3,(struct sockadd *)&their3_addr3,sizeof(struct sockaddr)) == -1)
{
perror("\n ss: t3: Binding Error in Thread 3 ");
exit(1);
}

/* state transition diagram */
state_transition_diagram(1,3,0);
/* state transition diagram */

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t3: Connection Establishment: Three-Way Handshake for Thread %d",(long int)&thread3);
printf("\n ss: t3: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(31,3,0);
/* state transition diagram */

buf3[x] = '0';
if((numbytes = sendto(sockfd3,buf3,strlen(buf3),0,
(struct sockaddr *)&theirs3_addr3,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 3: SYN, SequenceNum = X ");
exit(1);
}
pc3a++;
printf("\n ss: t3: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(31,3,0);
/* state transition diagram */

addr_len3 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd3,rbuf3,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs3_addr3,&addr_len3))==-1)
{
perror("\n ss: t3: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc3r++;
printf("\n ss: t3: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(3,3,0);
/* state transition diagram */

buf3[x] = '1';
if((numbytes = sendto(sockfd3,buf3,strlen(buf3),0,
(struct sockaddr *)&theirs3_addr3,sizeof(struct sockaddr))) == -1)
{
perror("ss: t3: Error Sending from thread 3: ACK, Ack = y + 1");
exit(1);
}
pc3a++;
/* ------------------------------- */
/* ------------------------------- */

printf("\n ss t3: Thread %d has been activated.\n",(long
int)&thread3);

/* state transition diagram */
state_transition_diagram(4,3,0);
/* state transition diagram */

while ((c=getc(ifp3)) != EOF)
{
buf3[x3] = c;
if((numbytes = sendto(sockfd3,buf3,strlen(buf3),0,
(struct sockaddr *)&theirs3_addr3,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 3");
exit(1);
}
pc3++;
pc3a++;
/* printf("%s",buf3); */

addr_len3 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd3,rbuf3,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs3_addr3,&addr_len3))==-1)
{
perror("\n ss: t3: Timeout - Receive ack from Receiver t3");
exit(1);
}
pc3r++;

} /* end while loop EOF */

/* state transition diagram */
state_transition_diagram(5,3,0);
/* state transition diagram */

strcpy(buf3, "111110End111110Of111110File111110");
if ((numbytes = sendto(sockfd3,buf3,strlen(buf3),0,
(struct sockaddr *)&theirs3_addr3,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending EOF marker from thread 3");
exit(1);
}
pc3a++;

printf("\n ss t3: Sending from thread: 3");
printf("\n ss t3: Sending from port: %d",LOCALPORT3);
printf("\n ss t3: Sending to: %s ",inet_ntoa(their3_addr3.sin_addr));
printf("\n ss t3: Finish Sending: %s",buf3);
printf("\n --------------------------------------------- \n");

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t3: Connection Termination : Three-Way Handshake for Thread %d",(long int)&thread3);
printf("\n ss: t3: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(7,3,0);
/* state transition diagram */

buf3[x3] = '0';
if((numbytes = sendto(sockfd3,buf3,strlen(buf3),0,
(struct sockaddr *)&theirs3_addr3,sizeof(struct sockaddr))) == -1)
{
perror("ss: ts: Error Sending from thread 3: SYN, SequenceNum = X ");
exit(1);
}
pc3a++;
printf("\n ss: t3: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(8,3,0);
/* state transition diagram */

addr_len3 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd3,rbuf3,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs3_addr3,&addr_len3))==-1)
{
perror("\n ss: t3: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc3r++;
printf("\n ss: t3: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(34,3,0);
/* state transition diagram */

buf3[x3] = '1';
if((numbytes = sendto(sockfd3,buf3,strlen(buf3),0,
(struct sockaddr *)&theirs3_addr3,sizeof(struct sockaddr))) == -1)
{
perror("ss: t3: Error Sending from thread 3: ACK, Ack = y + 1");
exit(1);
}
pc3a++;
/* ------------------------------- */
/* ------------------------------- */

rt1[xx]=(long int) &thread3;
rt3[xx]=LOCALPORT3;
rt5[xx]=LOCALPORT3;
xx++;
routing_table();

close(sockfd3);
close(ifp3);
updatethreads();
run3 = 0;
}

void thread_4(int *n)
{
int x4 = 0;

FILE *ifp4;

printf("\n -> Inside Thread 4 (gargvv): %s <- \n",gargvv);
if (strcmp(gargvv,"1")==0)
ifp4 = fopen("send_client4_1","r");
if (strcmp(gargvv,"2")==0)
ifp4 = fopen("send_client4_2","r");
if (strcmp(gargvv,"3")==0)
ifp4 = fopen("send_client4_3","r");
if (strcmp(gargvv,"4")==0)
ifp4 = fopen("send_client4_4","r");
if (strcmp(gargvv,"5")==0)
ifp4 = fopen("send_client4_5","r");

printf("\n Trying to Start Three-Way Handshake Algorithm in thread: %d ... \n",(long int)&thread4);
delay();
delay();
delay();
delay();

if((sockfd4 = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("Main Socket 4 Setup Error...");
exit(1);
}
their4_addr4.sin_family = AF_INET;
their4_addr4.sin_port = htons(LOCALPORT4);
their4_addr4.sin_addr.s_addr = inet_addr(THISIP);
bzero(&(their4_addr4.sin_zero),8);

theirs4_addr4.sin_family = AF_INET;
theirs4_addr4.sin_port = htons(LOCALPORT4);
theirs4_addr4.sin_addr.s_addr = inet_addr(LOCALIP);
bzero(&(theirs4_addr4.sin_zero),8);

if(bind(sockfd4,(struct sockadd *)&their4_addr4,sizeof(struct sockaddr)) == -1)
{
perror("\n ss: t4: Binding Error in Thread 4 ");
exit(1);
}

/* state transition diagram */
state_transition_diagram(1,4,0);
/* state transition diagram */

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t4: Connection Establishment: Three-Way Handshake for Thread %d",(long int)&thread4);
printf("\n ss: t4: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(31,4,0);
/* state transition diagram */

buf4[x] = '0';
if((numbytes = sendto(sockfd4,buf4,strlen(buf4),0,
(struct sockaddr *)&theirs4_addr4,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 4: SYN, SequenceNum = X ");
exit(1);
}
pc4a++;
printf("\n ss: t4: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(3,4,0);
/* state transition diagram */

addr_len4 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd4,rbuf4,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs4_addr4,&addr_len4))==-1)
{
perror("\n ss: t4: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc4r++;
printf("\n ss: t4: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(3,4,0);
/* state transition diagram */

buf4[x] = '1';
if((numbytes = sendto(sockfd4,buf4,strlen(buf4),0,
(struct sockaddr *)&theirs4_addr4,sizeof(struct sockaddr))) == -1)
{
perror("ss: t4: Error Sending from thread 4: ACK, Ack = y + 1");
exit(1);
}
pc4a++;
/* ------------------------------- */
/* ------------------------------- */

printf("\n ss t4: Thread %d has been activated.\n",(long
int)&thread4);

/* state transition diagram */
state_transition_diagram(4,4,0);
/* state transition diagram */

while ((c=getc(ifp4)) != EOF)
{
buf4[x4] = c;
if((numbytes = sendto(sockfd4,buf4,strlen(buf4),0,
(struct sockaddr *)&theirs4_addr4,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 4");
exit(1);
}
pc4++;
pc4a++;
/* printf("%s",buf4); */

addr_len4 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd4,rbuf4,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs4_addr4,&addr_len4))==-1)
{
perror("\n ss: t4: Timeout - Receive ack from Receiver t4");
exit(1);
}
pc4r++;

} /* end while loop EOF */

/* state transition diagram */
state_transition_diagram(5,4,0);
/* state transition diagram */

strcpy(buf4, "111110End111110Of111110File111110");
if ((numbytes = sendto(sockfd4,buf4,strlen(buf4),0,
(struct sockaddr *)&theirs4_addr4,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending EOF marker from thread 4");
exit(1);
}
pc4a++;

printf("\n ss t4: Sending from thread: 4");
printf("\n ss t4: Sending from port: %d",LOCALPORT4);
printf("\n ss t4: Sending to: %s ",inet_ntoa(their4_addr4.sin_addr));
printf("\n ss t4: Finish Sending: %s",buf4);
printf("\n --------------------------------------------- \n");

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t4: Connection Termination : Three-Way Handshake for Thread %d",(long int)&thread4);
printf("\n ss: t4: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(6,4,0);
/* state transition diagram */

buf4[x4] = '0';
if((numbytes = sendto(sockfd4,buf4,strlen(buf4),0,
(struct sockaddr *)&theirs4_addr4,sizeof(struct sockaddr))) == -1)
{
perror("ss: ts: Error Sending from thread 4: SYN, SequenceNum = X ");
exit(1);
}
pc4a++;
printf("\n ss: t4: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(8,4,0);
/* state transition diagram */

addr_len4 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd4,rbuf4,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs4_addr4,&addr_len4))==-1)
{
perror("\n ss: t4: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc4r++;
printf("\n ss: t4: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(8,4,0);
/* state transition diagram */

buf4[x4] = '1';
if((numbytes = sendto(sockfd4,buf4,strlen(buf4),0,
(struct sockaddr *)&theirs4_addr4,sizeof(struct sockaddr))) == -1)
{
perror("ss: t4: Error Sending from thread 4: ACK, Ack = y + 1");
exit(1);
}
pc4a++;

/* state transition diagram */
state_transition_diagram(34,4,0);
/* state transition diagram */

/* ------------------------------- */
/* ------------------------------- */

rt1[xx]=(long int) &thread4;
rt3[xx]=LOCALPORT4;
rt5[xx]=LOCALPORT4;
xx++;
routing_table();

close(sockfd4);
close(ifp4);
updatethreads();
run4 = 0;
}

void thread_5(int *n)
{
int x5 = 0;

FILE *ifp5;

printf("\n -> Inside Thread 5 (gargvv): %s <- \n",gargvv);
if (strcmp(gargvv,"1")==0)
ifp5 = fopen("send_client5_1","r");
if (strcmp(gargvv,"2")==0)
ifp5 = fopen("send_client5_2","r");
if (strcmp(gargvv,"3")==0)
ifp5 = fopen("send_client5_3","r");
if (strcmp(gargvv,"4")==0)
ifp5 = fopen("send_client5_4","r");
if (strcmp(gargvv,"5")==0)
ifp5 = fopen("send_client5_5","r");

printf("\n Trying to Start Three-Way Handshake Algorithm in thread: %d ... \n",(long int)&thread5);
delay();
delay();
delay();
delay();
delay();
delay();

if((sockfd5 = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("Main Socket 5 Setup Error...");
exit(1);
}
their5_addr5.sin_family = AF_INET;
their5_addr5.sin_port = htons(LOCALPORT5);
their5_addr5.sin_addr.s_addr = inet_addr(THISIP);
bzero(&(their5_addr5.sin_zero),8);

theirs5_addr5.sin_family = AF_INET;
theirs5_addr5.sin_port = htons(LOCALPORT5);
theirs5_addr5.sin_addr.s_addr = inet_addr(LOCALIP);
bzero(&(theirs5_addr5.sin_zero),8);

if(bind(sockfd5,(struct sockadd *)&their5_addr5,sizeof(struct sockaddr)) == -1)
{
perror("\n ss: t5: Binding Error in Thread 5 ");
exit(1);
}

/* state transition diagram */
state_transition_diagram(1,5,0);
/* state transition diagram */

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t5: Connection Establishment: Three-Way Handshake for Thread %d",(long int)&thread5);
printf("\n ss: t5: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(31,5,0);
/* state transition diagram */

buf5[x] = '0';
if((numbytes = sendto(sockfd5,buf5,strlen(buf5),0,
(struct sockaddr *)&theirs5_addr5,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 5: SYN, SequenceNum = X ");
exit(1);
}
pc5a++;
printf("\n ss: t5: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(3,5,0);
/* state transition diagram */

addr_len5 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd5,rbuf5,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs5_addr5,&addr_len5))==-1)
{
perror("\n ss: t5: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc5r++;
printf("\n ss: t5: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(3,5,0);
/* state transition diagram */

buf5[x] = '1';
if((numbytes = sendto(sockfd5,buf5,strlen(buf5),0,
(struct sockaddr *)&theirs5_addr5,sizeof(struct sockaddr))) == -1)
{
perror("ss: t5: Error Sending from thread 5: ACK, Ack = y + 1");
exit(1);
}
pc5a++;
/* ------------------------------- */
/* ------------------------------- */

printf("\n ss t5: Thread %d has been activated.\n",(long
int)&thread5);

/* state transition diagram */
state_transition_diagram(4,5,0);
/* state transition diagram */

while ((c=getc(ifp5)) != EOF)
{
buf5[x5] = c;
if((numbytes = sendto(sockfd5,buf5,strlen(buf5),0,
(struct sockaddr *)&theirs5_addr5,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending from thread 5");
exit(1);
}
pc5++;
pc5a++;
/* printf("%s",buf5); */

addr_len5 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd5,rbuf5,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs5_addr5,&addr_len5))==-1)
{
perror("\n ss: t5: Timeout - Receive ack from Receiver t5");
exit(1);
}
pc5r++;

} /* end while loop EOF */

/* state transition diagram */
state_transition_diagram(5,5,0);
/* state transition diagram */

strcpy(buf5, "111110End111110Of111110File111110");
if ((numbytes = sendto(sockfd5,buf5,strlen(buf5),0,
(struct sockaddr *)&theirs5_addr5,sizeof(struct sockaddr))) == -1)
{
perror("ss: Error Sending EOF marker from thread 5");
exit(1);
}
pc5a++;

printf("\n ss t5: Sending from thread: 5");
printf("\n ss t5: Sending from port: %d",LOCALPORT5);
printf("\n ss t5: Sending to: %s ",inet_ntoa(their5_addr5.sin_addr));
printf("\n ss t5: Finish Sending: %s",buf5);
printf("\n --------------------------------------------- \n");

/* ------------------------------- */
/* three way handshake
/* ------------------------------- */
printf("\n -----------------------------------------------------------------------");
printf("\n ss: t5: Connection Termination : Three-Way Handshake for Thread %d",(long int)&thread5);
printf("\n ss: t5: Sending : SYN, SequenceNum = x");

/* state transition diagram */
state_transition_diagram(7,5,0);
/* state transition diagram */

buf5[x5] = '0';
if((numbytes = sendto(sockfd5,buf5,strlen(buf5),0,
(struct sockaddr *)&theirs5_addr5,sizeof(struct sockaddr))) == -1)
{
perror("ss: ts: Error Sending from thread 5: SYN, SequenceNum = X ");
exit(1);
}
pc5a++;
printf("\n ss: t5: Receiving : SYN+ACK, SequenceNum = y");

/* state transition diagram */
state_transition_diagram(8,5,0);
/* state transition diagram */

addr_len5 = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd5,rbuf5,MAXSENDERBUFFER,0,
(struct sockaddr *)&theirs5_addr5,&addr_len5))==-1)
{
perror("\n ss: t5: Error Receiving SYN+ACK, SequenceNum = y");
exit(1);
}
pc5r++;
printf("\n ss: t5: Sending : ACK, Acknowledgment = y + 1");
printf("\n -----------------------------------------------------------------------\n");

/* state transition diagram */
state_transition_diagram(34,5,0);
/* state transition diagram */

buf5[x5] = '1';
if((numbytes = sendto(sockfd5,buf5,strlen(buf5),0,
(struct sockaddr *)&theirs5_addr5,sizeof(struct sockaddr))) == -1)
{
perror("ss: t5: Error Sending from thread 5: ACK, Ack = y + 1");
exit(1);
}
pc5a++;
/* ------------------------------- */
/* ------------------------------- */

rt1[xx]=(long int) &thread5;
rt3[xx]=LOCALPORT5;
rt5[xx]=LOCALPORT5;
xx++;
routing_table();

close(sockfd5);
close(ifp5);
updatethreads();
run5 = 0;
}

void displaypc()
{
tpc = pc1+pc2+pc3+pc4+pc5;
tpca = pc1a+pc2a+pc3a+pc4a+pc5a;
tpcr = pc1r+pc2r+pc3r+pc4r+pc5r;
printf("\n --------------------------------------------------------");
printf("\n Packets Counter (Sent Data):");
printf("\n");
printf("\n Thread1:%d Thread2:%d Thread3:%d",pc1,pc2,pc3);
printf("\n Thread4:%d Thread5:%d Total Count:%d",pc4,pc5,tpc);
printf("\n");
printf("\n Packets Counter (Sent Data and Ack):");
printf("\n");
printf("\n Thread1:%d Thread2:%d Thread3:%d",pc1a,pc2a,pc3a);
printf("\n Thread4:%d Thread5:%d Total Count:%d",pc4a,pc5a,tpca);
printf("\n");
printf("\n Packets Counter (Received Data and Ack):");
printf("\n");
printf("\n Thread1:%d Thread2:%d Thread3:%d",pc1r,pc2r,pc3r);
printf("\n Thread4:%d Thread5:%d Total Count:%d",pc4r,pc5r,tpcr);
printf("\n --------------------------------------------------------");
}

void routing_table()
{
int x = 0;
printf("\n -----------------------------------------------------------------------");
printf("\n Routing Table:");
printf("\n THREAD HOST-FROM PORT-FROM HOST-TO PORT-TO COST");

for (x=0; x<50; x++)
{
if (rt1[x]>0)
{
printf("\n %d %s %d %s %d %d",rt1[x],THISIP,rt3[x],LOCALIP,rt5[x],1);
}
}

for (x=0; x<50; x++)
{
if (rt1[x]>0)
{
printf("\n %d %s %d %s %d %d",rt1[x],THISIP,rt3[x],THISIP,rt5[x],0);
}
}
printf("\n -----------------------------------------------------------------------");
}

void updatethreads()
{
int x = 0;
x = run1 + run2 + run3 + run4 + run5;
printf("\n +++++++++++++++++++++++++++++++++++++++++++");
printf("\n ss: Total Threads Actually Running: %d", x);
printf("\n %d %d %d %d %d",run1,run2,run3,run4,run5);
printf("\n +++++++++++++++++++++++++++++++++++++++++++");
}

void state_transition_diagram(int x, int y, int z)
{
int x1 = x;
int y1 = y;
int z1 = z;
char my_message[MAXSENDERBUFFER]="";
char my_section[MAXSENDERBUFFER]="";

if (x1 == 1)
strcpy(my_message,"CLOSED");
if (x1 == 2)
strcpy(my_message,"LISTEN");
if (x1 == 3)
strcpy(my_message,"SYN_RCVD");
if (x1 == 4)
strcpy(my_message,"ESTABLISHED");
if (x1 == 5)
strcpy(my_message,"FIN_WAIT_1");
if (x1 == 6)
strcpy(my_message,"FIN_WAIT_2");
if (x1 == 7)
strcpy(my_message,"CLOSING");
if (x1 == 8)
strcpy(my_message,"TIME_WAIT");
if (x1 == 31)
strcpy(my_message,"SYN_SENT");
if (x1 == 32)
strcpy(my_message,"CLOSE_WAIT");
if (x1 == 33)
strcpy(my_message,"LAST_ACK");
if (x1 == 34)
strcpy(my_message,"CLOSED");

if (y1 == 0)
strcpy(my_section,"PROJECT 2, MAIN SECTION");
if (y1 == 1)
strcpy(my_section,"PROJECT 2, THREAD 1");
if (y1 == 2)
strcpy(my_section,"PROJECT 2, THREAD 2");
if (y1 == 3)
strcpy(my_section,"PROJECT 2, THREAD 3");
if (y1 == 4)
strcpy(my_section,"PROJECT 2, THREAD 4");
if (y1 == 5)
strcpy(my_section,"PROJECT 2, THREAD 5");

printf("\n______________________________________________________________");
printf("\nss: Entering the Following State of State Transition Diagram:");
printf("\nss: -> %s <-",my_message);
printf(" in %s",my_section);
printf("\n______________________________________________________________\n");
}

void delay()
{
sleep(4);
}

int conversion()
{
int i, ii, j = 0;
for (j=0; j<strlen(rbuf1); j++)
{
if ((rbuf1[j])=='0')
i = 0;
if ((rbuf1[j])=='1')
i = 1;
if ((rbuf1[j])=='2')
i = 2;
if ((rbuf1[j])=='3')
i = 3;
if ((rbuf1[j])=='4')
i = 4;
if ((rbuf1[j])=='5')
i = 5;
if ((rbuf1[j])=='6')
i = 6;
if ((rbuf1[j])=='7')
i = 7;
if ((rbuf1[j])=='8')
i = 8;
if ((rbuf1[j])=='9')
i = 9;
if (j==0)
ii = i;
if (j!=0)
ii = (ii*10) + i;
}
return ii;
}

www.cpccci.com
www.cpcwebsolutions.com
www.cpcwebdevelopment.com