Вы находитесь на странице: 1из 26

EX NO 4A ALGORITHM:

CRC

Start the program. Declare and initialize the variable. Enter the input to find the CRC and get the key. Finally it will produce the message with CRC code. Remove the n-1 code with CRC code. Enter the input and key for checking CRC. Print the result. Terminate the program. PROGRAM: #include<string.h> char text[20],key[20],rem[20]; void crc() { int i,j,keylen,textlen; char temp[100]; strcpy(temp,text); keylen=strlen(key); for(i=0;i<keylen-1;i++) strcat(temp,"0"); textlen=strlen(temp); strncpy(rem,temp,keylen); while(i!=textlen) { if(rem[0]=='0') { strcpy(rem,&rem[1]); rem[keylen-1]=temp[++i]; rem[keylen]='\0'; continue; } for(j=0;j<keylen;j++) rem[j]=((rem[j]-'0')^(key[j]-'0'))+'0'; }} main() {

int i,choice; while(1) { printf("\n1.Find CRC\n2.Check CRC\n3.Quit\nEnter the choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the i/p\n"); scanf("%s",text); printf("Enter the key\n"); scanf("%s",key); crc(); printf("Msg %s \n",strcat(text,rem)); break; case 2: printf("Enter the input"); scanf("%s",text); printf("\nEnter key"); scanf("%s",key); crc(); for(i=0;i<strlen(key)-1;i++) if(rem[i]=='1') break; if(i==strlen(key)-1) printf("NO ERROR\n"); else printf("ERROR"); break; case 3: exit(0); } } }

OUTPUT:

EX NO 4B PROGRAM

HAMMING CODE

#include<stdio.h> #include<stdlib.h> char data[5]; int encoded[8],edata[7],syndrome[3]; int hmatrix[3][7] = { 1,0,0,0,1,1,1, 0,1,0,1,0,1,1, 0,0,1,1,1,0,1 }; char gmatrix[4][8]={"0111000","1010100","1100010","1110001"}; int main(){ int i,j; system("clear"); printf("\nHamming code----- Encoding\n"); printf("Enter 4 bit data : "); scanf("%s",data); printf("\nGenerator matrix\n"); for(i=0;i<4;i++) printf("%s\n",gmatrix[i]); printf("\nEncoded data "); for(i=0;i<7;i++) { for(j=0;j<4;j++) encoded[i]+=((data[j]-'0')*(gmatrix[j][i]-'0')); encoded[i]=encoded[i]%2; printf("%d ",encoded[i]); } printf("\nHamming code----- Decoding\n"); printf("Enter encoded bits as recieved : "); for(i=0;i<7;i++) scanf("%d",&edata[i]); for(i=0;i<3;i++) { for(j=0;j<7;j++) syndrome[i]+=(edata[j]*hmatrix[i][j]);

syndrome[i]=syndrome[i]%2; } for(j=0;j<7;j++) if((syndrome[0]==hmatrix[0][j]) && (syndrome[1]==hmatrix[1][j])&& (syndrome[2]==hmatrix[2][j])) break; if(j==7) printf("\nError free\n"); else { printf("\nError recieved at bit number %d of data\n",j+1); edata[j]=!edata[j]; printf("\nCorrect data should be : "); for(i=0;i<7;i++) printf("%d",edata[i]); } return 0; } OUTPUT Hamming code----- Encoding Enter 4 bit data : 1011 Generator matrix 0111000 1010100 1100010 1110001 Encoded data 0 1 0 1 0 1 1 Hamming code----- Decoding Enter encoded bits as received : 0 1 0 1 1 1 1 Error received at bit number 5 of data Correct data should be : 0101011

EX N0 5

SLIDING WINDOW PROTOCOL

PROGRAM Server #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define SIZE 3 int main() { int sfd,lfd,len,i=0,j,status; char str[20],frame[20],temp[20],ack[20]; struct sockaddr_in saddr,caddr; if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1) perror("socket() error"); bzero(&saddr,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(1244); saddr.sin_addr.s_addr=htonl(INADDR_ANY); if(bind(sfd,(struct sockaddr*)&saddr,sizeof(saddr))<0) perror("bind error"); if(listen(sfd,5)>0) len=sizeof(&caddr); lfd=accept(sfd,(struct sockaddr*)&caddr,&len); printf("enter the text:\t"); scanf("%s",str); while(i<strlen(str)) { memset(frame,0,20); strncpy(frame,str+i,SIZE); printf("transmitting frames"); len=strlen(frame); for(j=0;j<len;j++) { printf("%d",i+j); sprintf(temp,"%d",i+j); strcat(frame,temp); }

write(lfd,frame,sizeof(frame)); read(lfd,ack,20); sscanf(ack,"%d",&status); if(status==-1) printf("\ntransmission success"); else { printf("\nreceived error in \t%d\n",status); printf("\nretransmitting frames"); for(j=0;;) { frame[i]=str[j+status]; j++; if((j+status)%4==0) break; } printf("\n"); frame[j]='\0'; len=strlen(frame); for(j=0;j<len;j++) { printf("%d",j+status); sprintf(temp,"%d",j+status); strcat(frame,temp); } write(lfd,frame,sizeof(frame)); } i+=SIZE; } write(lfd,"exit",sizeof("exit")); printf("exiting"); sleep(2); close(lfd); close(sfd); return 0; } Client #include<stdio.h> #include<sys/types.h>

#include<sys/socket.h> #include<netinet/in.h> int main() { int lfd,sfd,len,choice; char str[20],err[20]; struct sockaddr_in saddr,caddr; if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1) perror("socket() error"); bzero(&saddr,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(1244); connect(sfd,(struct sockaddr*)&saddr,sizeof(saddr)); for(;;) { read(sfd,str,20); if(!strcmp(str,"exit")) { printf("\nexiting"); break; } printf("\nreceived:\t %swant to report an error?(1-y/0-n):",str); scanf("%d",&choice); if(!choice) write(sfd,"-1",sizeof("-1")); else { printf("enter seg no of frame where error has occured:\t"); scanf("%s",err); write(sfd,err,sizeof(err)); read(sfd,str,20); printf("\nreceived transmitted frame is %s",str); } } close(sfd); return 0; }

OUTPUT: Server [cse314@localhost cse314]$ cc sliser.c [cse314@localhost cse314]$ ./a.out enter the text: asanmemorial transmitting frames012 transmission successtransmitting frames345 received error in 4 retransmitting frames 4567transmitting frames678 transmission successtransmitting frames91011 transmission successexiting[cse314@localhost cse314]$ Client [cse314@localhost cse314]$ cc slicli.c [cse314@localhost cse314]$ ./a.out received: asa012want to report an error?(1-y/0-n):0 received: nme345want to report an error?(1-y/0-n):1 enter seg no of frame where error has occured: 4 received transmitted frame is nmeo4567 received: mor678want to report an error?(1-y/0-n):0 received: ial91011want to report an error?(1-y/0-n):0 exiting[cse314@localhost cse314]$

EX NO:10

CHAT APPLICATION USING TCP

PROGRAM Server: #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<stdlib.h> #include<string.h> #define MAX 80 #define PORT 43454 #define SA struct sockaddr void func(int sockfd) { char buff[MAX]; int n; for(;;) { bzero(buff,MAX); read(sockfd,buff,sizeof(buff)); printf("From client: %s\t To client : ",buff); bzero(buff,MAX); n=0; while((buff[n++]=getchar())!='\n'); write(sockfd,buff,sizeof(buff)); if(strncmp("exit",buff,4)==0) { printf("Server Exit...\n"); break; } } } int main() { int sockfd,connfd,len; struct sockaddr_in servaddr,cli;

sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("Socket successfully created..\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(PORT); if((bind(sockfd,(SA*)&servaddr, sizeof(servaddr)))!=0) { printf("socket bind failed...\n"); exit(0); } else printf("Socket successfully binded..\n"); if((listen(sockfd,5))!=0) { printf("Listen failed...\n"); exit(0); } else printf("Server listening..\n"); len=sizeof(cli); connfd=accept(sockfd,(SA *)&cli,&len); if(connfd<0) { printf("server acccept failed...\n"); exit(0); } else printf("server acccept the client...\n"); func(connfd); close(sockfd); }

Client: #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> #include<stdlib.h> #define MAX 80 #define PORT 43454 #define SA struct sockaddr void func(int sockfd) { char buff[MAX]; int n; for(;;) { bzero(buff,sizeof(buff)); printf("Enter the string : "); n=0; while((buff[n++]=getchar())!='\n'); write(sockfd,buff,sizeof(buff)); bzero(buff,sizeof(buff)); read(sockfd,buff,sizeof(buff)); printf("From Server : %s",buff); if((strncmp(buff,"exit",4))==0) { printf("Client Exit...\n"); break; } } } int main() { int sockfd,connfd; struct sockaddr_in servaddr,cli; sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd==-1) { printf("socket creation failed...\n");

exit(0); } else printf("Socket successfully created..\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); servaddr.sin_port=htons(PORT); if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))!=0) { printf("connection with the server failed...\n"); exit(0); } else printf("connected to the server..\n"); func(sockfd); close(sockfd); }

OUTPUT Server: $ cc tcpchatserver.c $ ./a.out Socket successfully created.. Socket successfully binded.. Server listening.. server acccept the client... From client: hai To client : hello From client: exit To client : exit Server Exit... Client: $ cc tcpchatclient.c $ ./a.out Socket successfully created.. connected to the server.. Enter the string : hai

From Server : hello Enter the string : exit From Server : exit Client Exit...

EX NO:11 PROGRAM

CHAT APPLICATION USING UDP

Server: #include<netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include<stdio.h> #include <arpa/inet.h> #include <string.h> #include<fcntl.h> main() { int sfd,l; char buf[1024]="",buf1[1024]=""; struct sockaddr_in ser; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&ser,sizeof(ser)); ser.sin_family=AF_INET; ser.sin_port=htons(1300); inet_aton("localhost",&ser.sin_addr); printf("Enter the message:"); scanf("%s",buf); sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&ser,sizeof(ser)); recvfrom(sfd,buf1,1024,0,NULL,NULL); close(sfd); } Client: #include<netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include<stdio.h> #include <arpa/inet.h> #include <string.h> #include<fcntl.h> main() { int sfd,l;

char buf[1024]=""; struct sockaddr_in server,client; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(1300); inet_aton("localhost",&server.sin_addr); printf("bind=%d\n" ,bind(sfd,(struct sockaddr *)&server,sizeof(server))); l=sizeof(client); for(;;) { recvfrom(sfd,buf,1024,0,(struct sockaddr *)&client,&l); sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&client,l); printf("MESSAGE FROM CLIENT:%s\n",buf); } } OUTPUT: Server: cc udpechoserver.c -o echo ./echo bind=0 MESSAGE FROM CLIENT:hello Client: cc udpechoclient.c -o udpecho ./udpecho Enter the message: hello

EX NO:12 PROGRAM

FILE TRANSFER USING RS232

#include <stdio.h> #include <dos.h> #include<string.h> #include<conio.h> #define PORT1 0x3f8 void main() { int c,ch,choice,i; char filename[15],filename2[15],buf; FILE *in,*out; clrscr(); outportb(PORT1+0,0x03); outportb(PORT1+1,0); outportb(PORT1+3,0x03); outportb(PORT1+2,0xc7); outportb(PORT1+4,0x0b); printf("\this computer is going to act as a:"); printf("\n 1.sender \n2.receiver\n\nEnter the choice:"); scanf("%d",&choice); if(choice==1) { printf("enter the filename to be created:"); scanf("%s",filename); for(i=0;i<=strlen(filename);i++) outportb(PORT1,filename[i]); in=fopen(filename,"rt"); printf("\n\nsending.....\n\n"); while(!feof(in)) { buf=fgetc(in); printf("%c",buf); outportb(PORT1,buf); delay(10); } } else

{ i=0; buf='a'; while(buf!=NULL) { c=inportb(PORT1+5); if(c&1) { buf=inportb(PORT1); filename2[i]=buf; i++; } } out=fopen(filename2,"wt"); printf("\n Filename received:%s",filename[2]); printf("Reading from the port..."); printf("writing to file %s",filename2); do { c=inportb(PORT1+5); if(c&1) { buf=inportb(PORT1); printf("%c",buf); fputc(buf,out); delay(10); } if(kbhit()) { ch=getch(); } }while(ch!=27); } }

EX NO:13

IMPLEMENTATION OF SHORTEST PATH ROUTING

ALGORITHM 1.Read the no. of nodes n 2.Read the cost matrix for the path from each node to another node. 3.Initialize SOURCE to 1 and include 1 4. Compute D of a node which is the distance from source to that corresponding node. 5.Repeat step 6 to step 8 for n-l nodes. 6.Choose the node that has not been included whose distance is minimum and include that node. 7.For every other node not included compare the distance directly from the source with the distance to reach the node using the newly included node 8.Take the minimum value as the new distance. 9.Print all the nodes with shortest path cost from source node PROGRAM #include<stdio.h> #include<string.h> int main() { int count,src_router,i,j,k,w,v,min; int cost_matrix[100][100],dist[100],last[100]; int flag[100]; printf("\n Enter the no of routers"); scanf("%d",&count); printf("\n Enter the cost matrix values:"); for(i=0;i<count;i++) { for(j=0;j<count;j++) { printf("\n%d->%d:",i,j); scanf("%d",&cost_matrix[i][j]); if(cost_matrix[i][j]<0) cost_matrix[i][j]=1000; } } printf("\n Enter the source router:"); scanf("%d",&src_router); for(v=0;v<count;v++)

{ flag[v]=0; last[v]=src_router; dist[v]=cost_matrix[src_router][v]; } flag[src_router]=1; for(i=0;i<count;i++) { min=1000; for(w=0;w<count;w++) { if(!flag[w]) if(dist[w]<min){ v=w; min=dist[w]; } } flag[v]=1; for(w=0;w<count;w++) { if(!flag[w]) if(min+cost_matrix[v][w]<dist[w]) { dist[w]=min+cost_matrix[v][w]; last[w]=v; } } } for(i=0;i<count;i++) { printf("\n%d==>%d:Path taken:%d",src_router,i,i); w=i; while(w!=src_router) { printf("\n<--%d",last[w]); w=last[w]; } printf("\n Shortest path cost:%d",dist[i]); } }

OUTPUT [ME04@TELNET ~]$ cc open.c [ME04@TELNET ~]$ ./a.out Enter the no of routers 4 Enter the cost matrix values 0->0:1 0->1:4 0->2:6 0->3:8 1->0:4 1->1:6 1->2:5 1->3:9 2->0:1 2->1:3 2->2:2 2->3:0 3->0:4 3->1:2 3->2:6 3->3:8 Enter the source router:3 3==>0:Path taken:0 <--3 Shortest path cost:4 3==>1:Path taken:1 <--3 Shortest path cost:2 3==>2:Path taken:2 <--3 Shortest path cost:6 3==>3:Path taken:3 Shortest path cost:8

EX NO:14

BIT STUFFING

ALGORITHM 1. Include the nessary header files and declare the variables. 2. Get the input data from the user. 3. Check for five continuous 1s in the input string. 4. If so, insert 0 after five continuous 1s. 5. Continue the checking for five 1s until \0 is encountered 6. Repeat step5. 7. Stop the program. PROGRAM #include <stdio.h> #include <string.h> void stuff(char str[40]); main() { char str[40]; int i; printf("\n enter the string:"); scanf("%s", &str); printf("\n the given string is:"); printf("\n %s", str); stuff(str); } void stuff(char str[40]) { int i, j, k = 0, l, n, z; printf("\n now we are stuffing the data::\n"); n = strlen(str); for (i = 0; i < n; i++) { if (str[i] == '1') { k = 1; for (l = i + 1; l <= i + 5; l++) { if (str[l] == '1') k++; else

break; } if (k == 6) { i = i + 6; z = n + 1; for (j = z; j >= i; j--) { str[j] = str[j - 1]; } str[j] = '0'; } } } printf("\nThe resultant string after stuffing is..\n"); printf("%s\n", str); }

OUTPUT

EX NO:15 PROGRAM

DNS SERVER

#include<stdio.h> #include<stdlib.h> int main() { int i,flag=0; char buf[80],s1[80],s2[80]; FILE *fp; printf("\nSelect Choice \n\t1.Forward Mode\n\t2.Backword Mode"); printf("\nYour choice is : "); scanf("%d",&i); switch(i) { case 1: printf("\nEnter Name of Server : "); scanf("%s",buf); fp=fopen("dns.txt","r"); while(!feof(fp)) { fscanf(fp,"%s",s1); fscanf(fp,"%s",s2); if((strcmp(s1,buf))==0) { printf("\nThe IP address of Server is : %s\n\n",s2); flag=1; } bzero(s1,80); bzero(s2,80); } if(flag==0) { printf("\nInvalid choice..!\n"); } flag=0; bzero(buf,80); fclose(fp); break;

case 2: printf("\nEnter IP address of server : "); scanf("%s",buf); fp=fopen("dns.txt","r"); while(!feof(fp)) { fscanf(fp,"%s",s1); fscanf(fp,"%s",s2); if((strcmp(s2,buf))==0) { printf("\nThe Name of Server is : %s\n\n",s1); flag=1; } bzero(s1,80); bzero(s2,80); } if(flag==0) { printf("\nInvalid choice..!\n"); } flag=0; bzero(buf,80); fclose(fp); break; default : printf("\nYou have entered invalid choice!"); exit(1); } return 0; } //Create a DNS text file which contains web address & ip. DNS.txt www.google.com www.gmail.com www.youtube.com www.facebook.com www.oxfordec.edu.in www.bing.com

74.125.235.55 173.194.36.54 173.194.36.46 69.171.234.21 69.50.199.222 117.239.189.27

in.yahoo.com www.wikipedia.org

106.10.170.118 208.80.154.225

Вам также может понравиться