0% found this document useful (0 votes)
6 views8 pages

Goback Merged

Uploaded by

sytlog43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Goback Merged

Uploaded by

sytlog43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SERVER

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<pthread.h>

main()
{
int sockfd,newsockfd,clilen,g=0,n,m,p=0,f=-1,k;
char ca='a',can='c',ch;
struct sockaddr_in serv_addr,cli_addr;
serv_addr.sin_family=PF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(8084);
sockfd=socket(PF_INET,SOCK_STREAM,0);
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(sockfd,5);
printf("Server is waiting.....\n");
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clilen);
while(1)
{
n=read(newsockfd,&ch,1);
n=read(newsockfd,&g,1);
if(ch='d')
{
k=rand();
if(k%3==0)
{
if(g==f+1)
{
printf("Data %d Corrupted\n",g);
write(newsockfd,&can,1);
write(newsockfd,&g,1);
sleep(2);
}
else
{
printf("Discarded data %d\n",g);
}
}
else
{
p=f+1;
if(g==p)
{
printf("Data %d received\n",g);
write(newsockfd,&ca,1);
write(newsockfd,&g,1);
printf("Data %d acknowledged\n",g);
f=g;
}
else
printf("Discarded data %d\n",g);
sleep(2);
}
}
}
}

OUTPUT

CLIENT

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<pthread.h>

int sockfd,m,n,s=-1,g=0;

void* senddata()
{
char ch='d';
while(1)
{
s++;
write(sockfd,&ch,1);
write(sockfd,&s,1);
printf("Send data %d\n",s);
sleep(1);
}
}

void*receivedata()
{
char ca;
while(1)
{
n=read(sockfd,&ca,1);
m=read(sockfd,&g,1);
if(ca=='a')
{
printf("Ack %d Received\n",g);
sleep(1);
}
else if(ca=='c')
{
printf("sending again\n");
s=g-1;
}
}
}

main()
{
pthread_t s,r;
struct sockaddr_in cliaddr;
cliaddr.sin_family=PF_INET;
cliaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
cliaddr.sin_port=htons(8084);
sockfd=socket(PF_INET,SOCK_STREAM,0);
connect(sockfd,(struct sockaddr*)&cliaddr,sizeof(cliaddr));
printf("connecting to server......\n");
pthread_create(&s,0,senddata,0);
sleep(1);
pthread_create(&r,0,receivedata,0);
sleep(1);
while(1);
}

OUTPUT
SERVER

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<pthread.h>
#include<fcntl.h>

int main()
{
int g=0,p,f=-1;
char ch,ca='a',can='c';

struct sockaddr_in server,client;


int len=sizeof(client);
server.sin_family=AF_INET;
server.sin_port=8888;
server.sin_addr.s_addr=INADDR_ANY;

int sockfd=socket(AF_INET,SOCK_STREAM,0);
bind(sockfd,(struct sockaadr*)&server,sizeof(server));

listen(sockfd,5);
printf("Server is waiting......in");
int newfd=accept(sockfd,(struct sockaddr*)&client,&len);

while(1)
{
read(newfd,&ch,1);
read(newfd,&g,1);
if(ch=='d')
{
int k=rand();
if(k%3==0)
{
printf("Data %d corrupted\n",g);
write(newfd,&can,1);
write(newfd,&g,1);
sleep(2);
}
else
{
printf("\nData received: %d\n",g);
write(newfd,&ca,1);
write(newfd,&g,1);
printf("Data acknowledged: %d\n",g);
f=g;
sleep(2);
}
}
}
}
OUTPUT

CLIENT

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<pthread.h>
#include<fcntl.h>

#define WSIZE 2
int sockfd,up=-1,low=0,g;

void* senddata()
{
char ch='d';
while(1)
{
up++;
while((up-low)>WSIZE)
{
printf("Congestion......\n");
sleep(1);
}
printf("Data sent: %d\n",up);
write(sockfd,&ch,1);
write(sockfd,&up,1);
sleep(1);
}
}

void* recvdata()
{
char c;
char ch='d';
while(1)
{
read(sockfd,&c,1);
read(sockfd,&g,1);
if(c=='a')
{
printf("Data acknowledged: %d\n",g);
low++;
}
else if(c=='c')
{
printf("Data %d corrupted, sending again\n",g);
write(sockfd,&ch,1);
write(sockfd,&g,1);
}
sleep(1);
}
}

int main()
{
pthread_t s,r;
sockfd = socket(AF_INET,SOCK_STREAM,0);

struct sockaddr_in server;


server.sin_family = AF_INET;
server.sin_port = 8888;
server.sin_addr.s_addr = INADDR_ANY;

connect(sockfd,(struct sockaddr*)&server,sizeof(server));

pthread_create(&s,0,senddata,0);
sleep(1);
pthread_create(&r,0,recvdata,0);
sleep(1);
while(1);
}

OUTPUT
#include<stdio.h>

int costMatrix[20][20],n;

struct routers
{
int distance[20];
int adjNodes[20];
} node[20];

//function to read the cost matrix


void readCostMatrix()
{
int i,j;
printf("\nEnter cost matrix\n");
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
scanf("%d", &costMatrix[i][j]);
//distance from X to X is 0
costMatrix[i][i]=0;
node[i].distance[j]=costMatrix[i][j];
node[i].adjNodes[j]=j;
}
}
}

void calcRoutingTable()
{
int i,j,k;
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
for(k=0;k<n;++k)
{
//if the cost of the path from X to Y is less than the cost of the path X to Z
if(node[i].distance[j] > costMatrix[i][k]+node[k].distance[j])
{
//susbtitute with minimum distance
node[i].distance[j]=node[i].distance[k]+node[k].distance[j];
//substitute with minimum path
node[i].adjNodes[j]=k;
}
}
}
}
}

void displayRoutes()
{
int i,j;
for(i=0;i<n;++i)
{
printf("\nRouter %d\n",i+1);
for(j=0;j<n;++j)
{
printf("Node %d via %d : Distance %d\n", j+1,node[i].adjNodes[j]+1,node[i].distance[j]);
}
printf("\n");
}
}

int main()
{
int i,j;
printf("Number of nodes:");
scanf("%d",&n);
readCostMatrix();
calcRoutingTable();
displayRoutes();

return 0;
}

OUTPUT

You might also like