This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Network Programming Laboratory CSE Department
//Program 1:Implementation of CRC-CCITT-16 using shift register for // error detection.
#include<stdio.h>#include<stdlib.h>static int R=0,Gr1=0x8810; //G=0x11021
void UpdateCRC(short x){
short i=0, x_msb;while(i++ < 8) { x_msb = (x >> 7) & 1; /* k=msb of x*/
if (R & 0x8000) /* is msb of R == 1? */R = ((R ^ Gr1) << 1) + (x_msb ^ 1); //XOR G & R
elseR = (R << 1) + x_msb;
x <<= 1;} }
int main(){ short c,opt;
FILE *fp;while(1)
{printf("\n\t\t Menu:\n1: Transmit Message\t2: Check CRC\t3: Exit\nEnter your option:");scanf("%d",&opt);switch(opt)
{ case 1: if( (fp = fopen("tmp","w")) == NULL)
printf("cannot open 'tmp' file."), exit(1);R=0; //ResetCRC Registerprintf("Enter the text to transmit:\n");while ((c=getchar()) != EOF)
}printf("\nSuitable path from router %d to %d is through router %d " "with delay %d units\n",src, dest, via, min);
}
int main(){ int src,dest;
initialize();
6
Network Programming Laboratory CSE Department
getgraph();printf("\nEnter the Source & Destination router\n");scanf("%d%d",&src, &dest);gettable(src);process(src, dest);
}
OUTPUT:
[user@localhost cnlab]$ cc prog3-distance_vector.c[user@localhost cnlab]$ ./a.outEnter no. of routers & edges in the graph:7 10Enter start router --> end router1 2Enter start router --> end router2 3Enter start router --> end router3 4Enter start router --> end router4 5Enter start router --> end router5 6Enter start router --> end router6 1Enter start router --> end router1 3Enter start router --> end router2 7Enter start router --> end router3 6Enter start router --> end router3 7
Enter the Source & Destination router2 5
Enter information for Source router 2Enter distance from source router 2 to 1 :2Enter distance from source router 2 to 3 :20Enter distance from source router 2 to 7 :7
Enter the contents of Echo packet of Adjacent routers of 2Enter the contents of Echo packet of router 1Enter distance from router 1 to 2 :5Enter distance from router 1 to 3 :11Enter distance from router 1 to 4 :24Enter distance from router 1 to 5 :17Enter distance from router 1 to 6 :10Enter distance from router 1 to 7 :6Enter the contents of Echo packet of router 3
7
Network Programming Laboratory CSE Department
Enter distance from router 3 to 1 :3Enter distance from router 3 to 2 :9Enter distance from router 3 to 4 :7Enter distance from router 3 to 5 :15Enter distance from router 3 to 6 :9Enter distance from router 3 to 7 :2Enter the contents of Echo packet of router 7Enter distance from router 7 to 1 :3Enter distance from router 7 to 2 :16Enter distance from router 7 to 3 :11Enter distance from router 7 to 4 :16Enter distance from router 7 to 5 :8Enter distance from router 7 to 6 :9
Suitable path from router 2 to 5 is through router 7 with delay 15 units
8
Network Programming Laboratory CSE Department
// Program 4: Spanning tree algorithm (Kruskal) to find loopless path.
int v1, v2, wt;printf("Enter the no of Vertices & Edges\n");scanf("%d%d",&n, &e);printf("Enter the graph++++++++++++++\n");while(e-- != 0){ printf("\nEnter start vertex-->end vertex:\n");
scanf("%d%d",&v1, &v2); printf("Enter Weight:");
scanf("%d",&wt);a[v1][v2] = a[v2][v1] = wt;
} }
void mini(int *v1, int *v2){ int minedge=999, i, j;
[user@localhost cnlab]$ cc prog4-prims.c[user@localhost cnlab]$ ./a.outEnter number of Vertices & Edges:4 5Enter the graph++++++++++Enter start vertex --> end vertex1 2Enter Weight:1
Enter start vertex --> end vertex2 3Enter Weight:2
Enter start vertex --> end vertex3 4Enter Weight:2
Enter start vertex --> end vertex4 1Enter Weight:4
Enter start vertex --> end vertex1 3Enter Weight:5
Edge 1 from 1 to 2Edge 2 from 2 to 4Edge 3 from 2 to 3
12
Network Programming Laboratory CSE Department
//Program 5: Using TCP/IP sockets, a client-server program to make client //sending the file name & the server to send back the contents of the //requested file if present.
listen(s,5); //listen channel for any request while(1) { printf("\n\nWaiting for request:\n");
sa=accept(s,0,0); //create a socket for communicationif(sa<0) printf("accept failed");
memset(fname,0,sizeof(fname)); read(sa,fname,BUF_SIZE); //read file name from channel
printf("requested filename: %s",fname);fd=open(fname,O_RDONLY); //open file to READ
if(fd<0){ printf("\nError message sent to client\n");
write(sa,"could not open requested file",40);}else {
while(1) { bytes = read(fd,buf,BUF_SIZE); //read file & store in buf
if(bytes<=0) break; write(sa,buf,bytes); //write the contents to channel }
close(fd); //close connection}
close(sa);}
}
14
Network Programming Laboratory CSE Department
OUTPUT:
[user@localhost cnlab]$ cc prog5-sockets_server.c[user@localhost cnlab]$ ./a.out
Waiting for request:requested filename: abc.txt
[user@localhost cnlab]$ cc prog5-sockets_client.c[user@localhost cnlab]$ ./a.outEnter the file name:abc.txthello,welcome to P.A college of engineering.
15
Network Programming Laboratory CSE Department
//Program 6(a): Using FIFO's as IPC channel, a client-server program to make client //sending the file name & the server to send back the contents of the requested //file if present.
}else{ while( (n=read(fd,buff,MAXBUF)) >0 ) //read contents of file
if(write(writefd,buff,n)!=n) //write it to FIFO fileprintf("server:data write error");
}close(readfd);close(writefd);
}
OUTPUT:
[user@localhost cnlab]$ cc prog6-fifo_server.c[user@localhost cnlab]$ ./a.out[user@localhost cnlab]$
[user@localhost cnlab]$ cc prog6-fifo_client.c[user@localhost cnlab]$ ./a.outEnter the file name:abc.txthello,welcome to P.A college of engineering.
17
Network Programming Laboratory CSE Department
//Program 6(b): Using Message Queue as IPC channel, a client-server program to //make client sending the file name & the server to send back the contents //of the requested file if present.
printf("\n\nIncomming Packet size: %d",packet_sz[i]);printf("\nBytes remaining to Transmit: %d",p_sz_rm);p_time = rand(4)*10;printf("\nTime left for transmission: %d units",p_time);for(clk=10; clk<=p_time; clk+=10){ sleep(1);
if(p_sz_rm){ if(p_sz_rm <= o_rate)
printf("\n Packet of size %d Transmitted",p_sz_rm),p_sz_rm=0;
elseprintf("\n Packet of size %d Transmitted",o_rate),p_sz_rm -= o_rate;printf("----Bytes Remaining after Transmission: %d",p_sz_rm);
}else
printf("\n No packets to transmit!!");printf(" Time Left:%d",p_time-clk);
} } }}
25
Network Programming Laboratory CSE Department
OUTPUT:
[user@localhost cnlab]$ cc prog9-leakybucket.c[user@localhost cnlab]$ ./a.outpacket[0]:30 bytes packet[1]:10 bytes packet[2]:10 bytes packet[3]:50 bytes packet[4]:30 bytesEnter the Output rate:20Enter the Bucket Size:50
Incomming Packet size: 30Bytes remaining to Transmit: 30Time left for transmission: 10 unitsPacket of size 20 Transmitted----Bytes Remaining after Transmission: 10 Time Left:0
Incomming Packet size: 10Bytes remaining to Transmit: 20Time left for transmission: 20 unitsPacket of size 20 Transmitted----Bytes Remaining after Transmission: 0 Time Left:10 No packets to transmit!! Time Left:0
Incomming Packet size: 10Bytes remaining to Transmit: 10Time left for transmission: 20 unitsPacket of size 10 Transmitted----Bytes Remaining after Transmission: 0 Time Left:10 No packets to transmit!! Time Left:0
Incomming Packet size: 50Bytes remaining to Transmit: 50Time left for transmission: 10 unitsPacket of size 20 Transmitted----Bytes Remaining after Transmission: 30 Time Left:0