Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program-1 : OSI Model Simulation
- Code :
- #include<iostream>
- #include<string.h>
- #include<stdio.h>
- using namespace std;
- void transmitter( char* str1)
- { char a[]="AH",b[]="PH",c[]="SH",d[]="TH",e[]="NH",f[]="DH";
- strcat(a,str1);
- cout<<"Application Layer : "<<a<<"\n";
- strcat(b,a);
- cout<<"Presentation Layer : "<<b<<"\n";
- strcat(c,b);
- cout<<"Session Layer : "<<c<<"\n";
- strcat(d,c);
- cout<<"Transport Layer : "<<d<<"\n";
- strcat(e,d);
- cout<<"Network Layer : "<<e<<"\n";
- strcat(f,e);
- cout<<"Datalink Layer : "<<f<<"\n";
- cout<<"\nMESSAGE ENTERED INTO PHYSICAL LAYER AND TRANSMITTED.\n";
- cout<<"\nRECIEVER\n\n";
- cout<<"MESSAGE ENTERED INTO PHYSICAL LAYER\n\n";
- cout<<"Datalink Layer : "<<f<<"\n";
- cout<<"Network Layer : ";
- for(int i=2;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nTransport Layer : ";
- for(int i=4;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nSession Layer : ";
- for(int i=6;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nPresentation Layer : ";
- for(int i=8;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nApplication Layer : ";
- for(int i=10;i<strlen(f);i++)
- cout<<f[i];
- }
- main()
- { char str[10],add[10];
- cout<<"Input String : ";
- cin>>str;
- cout<<"Input Address : ";
- cin>>add;
- cout<<"\nTRANSMITTER\n\n";
- transmitter(str);
- }
- 1. Character Count :
- Code:
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- void main(){
- int size;
- int total=0;
- int i=1;
- int j,k=0;
- int fsize;
- printf("Enter the no. of characters: ");
- scanf("%d",&size);
- char str[size];
- printf("\nEnter the characters: ");
- scanf("%s",&str);
- while(k<size)
- {
- printf("Enter the frame %d size: ",i);
- scanf("%d",&fsize);
- char frame[fsize+1];
- i++;
- total+=fsize;
- frame[0]=(char)(fsize+48);
- for(j=1;j<fsize;j++)
- {
- if(k<size)
- {
- printf("%c\n",str[k]);
- frame[j]=str[k];
- k++;
- }
- }
- frame[fsize]='\0';
- printf("%s\n",frame);
- }
- getch();
- }
- Output :
- 2. Character Stuffing
- Code:
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- void charc(void);
- void main()
- {i
- nt choice;
- while(1)
- {p
- rintf("\n\n\n1.character stuffing");
- printf("\n\n2.exit");
- printf("\n\n\nenter choice");
- scanf("%d",&choice);
- printf("%d",choice);
- if(choice>2)
- printf("\n\n invalid option....please renter");
- switch(choice)
- {
- case 1:
- charc();
- break;
- case 2:
- exit(0);
- }}}
- void charc(void)
- { char c[50],d[50],t[50];
- int i,m,j;
- clrscr();
- printf("enter the number of characters\n");
- scanf("%d",&m);
- printf("\n enter the characters\n");
- for(i=0;i<m+1;i++)
- {
- scanf("%c",&c[i]);
- }p
- rintf("\n original data\n");
- for(i=0;i<m+1;i++)
- printf("%c",c[i]);
- d[0]='d';
- d[1]='l';
- d[2]='e';
- d[3]='s';
- d[4]='t';
- d[5]='x';
- for(i=0,j=6;i<m+1;i++,j++)
- {
- if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
- {d[j]='d';
- j++;
- d[j]='l';
- j++;
- d[j]='e';
- j++;
- m=m+3;
- }d
- [j]=c[i];
- }
- m=m+6;
- m++;
- d[m]='d';
- m++;
- d[m]='l';
- m++;
- d[m]='e';
- m++;
- d[m]='e';
- m++;
- d[m]='t';
- m++;
- d[m]='x';
- m++;
- printf("\n\n transmitted data: \n");
- for(i=0;i<m;i++)
- {
- printf("%c",d[i]);
- }f
- or(i=6,j=0;i<m-6;i++,j++)
- {
- if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e
- ')
- i=i+3;
- t[j]=d[i];
- }p
- rintf("\n\nreceived data:");
- for(i=0;i<j;i++)
- {printf("%c",t[i]);
- }}
- Output :
- 3.Bit Stuffing
- Code:
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- void main() {
- int i, j,count=0,nl;
- char str[100];
- clrscr();
- printf("enter the bit string: ");
- gets(str);
- for (i=0;i<strlen(str);i++) {
- count=0;
- for (j=i;j<=(i+5);j++) {
- if(str[j]=='1') {
- count++;
- }
- }
- if(count==6) {
- nl=strlen(str)+2;
- for (;nl>=(i+5);nl--) {
- str[nl]=str[nl-1];
- }
- str[i+5]='0';
- i=i+7;
- }}
- puts(str);
- getch();
- }
- Output:
- PROGRAM-3 : To Calculate the Checksum
- Code:
- #include<stdio.h>
- #include<conio.h>
- #include<math.h>
- int sender(int b[10],int k)
- {
- intchecksum,sum=0,i;
- printf("\n****SENDER****\n");
- for(i=0;i<k;i++)
- sum+=b[i];
- printf("SUM IS: %d",sum);
- checksum=~sum;
- printf("\nSENDER's CHECKSUM IS:%d",checksum);
- return checksum;
- }
- int receiver(int c[10],intk,intscheck)
- {
- intchecksum,sum=0,i;
- printf("\n\n****RECEIVER****\n");
- for(i=0;i<k;i++)
- sum+=c[i];
- printf(" RECEIVER SUM IS:%d",sum);
- sum=sum+scheck;
- checksum=~sum;
- printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
- return checksum;
- }
- main()
- {
- int a[10],i,m,scheck,rcheck;
- clrscr();
- printf("\nENTER SIZE OF THE STRING:");
- scanf("%d",&m);
- printf("\nENTER THE ELEMENTS OF THE ARRAY:");
- for(i=0;i<m;i++)
- scanf("%d",&a[i]);
- scheck=sender(a,m);
- rcheck=receiver(a,m,scheck);
- if(rcheck==0)
- printf("\n\nNO ERROR IN TRANSMISSION\n\n");
- else
- printf("\n\nERROR DETECTED");
- getch();
- }
- Output :
- Program-4: Generation and checking of CRC
- Code:
- #include<stdio.h> #include<conio.h> #define N strlen(g) char t[128], cs[128], g[]="100010000"; int a, e, c; void xor() { for(c=1;c} void crc() { for(e=0;e do { if(cs[0]=='1') xor(); for(c=0;c cs[c]=t[e++]; }while(e<=a+N-1); } void main() { clrscr(); printf("\nEnter poly : "); scanf("%s",t); printf("\nGenerating Polynomial is : %s",g); a=strlen(t); for(e=a;e printf("\nModified t[u] is : %s",t); crc(); printf("\nChecksum is : %s",cs); for(e=a;e printf("\nFinalCodeword is : %s",t); printf("\nTest Error detection 0(yes) 1(no) ? : "); scanf("%d",&e); if(e==0) { printf("Enter position where error is to inserted : "); scanf("%d",&e); t[e]=(t[e]=='0')?'1':'0'; printf("Errorneous data : %s\n",t); } crc(); for (e=0;(e<n-1)&&(cs[e]!='1');e++); if(e else printf("No Error Detected."); getch(); }
- Output:
- Program-5: Generation of hamming code
- Code:
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int data[7],rec[7],i,c1,c2,c3,c;
- printf("this works for message of 4bits in size \nenter message bit one by one: ");
- scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
- data[6]=data[0]^data[2]^data[4];
- data[5]=data[0]^data[1]^data[4];
- data[3]=data[0]^data[1]^data[2];
- printf("\nthe encoded bits are given below: \n");
- for (i=0;i<7;i++) {
- printf("%d ",data[i]);
- }
- printf("\nenter the received data bits one by one: ");
- for (i=0;i<7;i++) {
- scanf("%d",&rec[i]);
- }
- c1=rec[6]^rec[4]^rec[2]^rec[0];
- c2=rec[5]^rec[4]^rec[1]^rec[0];
- c3=rec[3]^rec[2]^rec[1]^rec[0];
- c=c3*4+c2*2+c1 ;
- if(c==0) {
- printf("\ncongratulations there is no error: ");
- } else {
- printf("\nerron on the postion: %d\nthe correct message is \n",c);
- if(rec[7-c]==0)
- rec[7-c]=1; else
- rec[7-c]=0;
- for (i=0;i<7;i++) {
- printf("%d ",rec[i]);
- }
- }
- getch();
- }
- //sliding window protocol-go back n arq
- #include<iostream>
- #include<math.h>
- using namespace std;
- int main()
- {
- int m,ws,check=1,count=0;
- cout<<"enter the value of m"<<endl; //what is m???
- cin>>m;
- //calculating window size
- ws=pow(2,m)-1;
- cout<<"ws= "<<ws<<endl;
- int sn=0,sf=0,rn=0,ack,time;
- cout<<"enter time out value "<<endl;
- cin>>time;
- while(check)
- {
- //cout<<sn<<" "<<ws<<endl;
- if(sn == ws)
- {
- cout<<"window full cannot enter anymore data"<<endl;
- break;
- }
- cout<<"press 1 to transmit data else press 0"<<endl;
- cin>>check;
- sn++;
- cout<<"press 1 if acknowledgement is received else press 0"<<endl;
- cin>>ack;
- if(ack)
- {
- rn++;
- sf++;
- time=3;
- }
- else
- {
- time--;
- if(time==0)
- {
- cout<<"time out transmit again from frame "<<sf<<endl;
- sn=sf;
- }
- }
- cout<<"data frames sent= "<<sn<<endl;
- cout<<"data frames received= "<<rn<<endl;
- }
- }
- #client chat app using tcp ip
- chatappserver.c
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<stdio.h>
- #include<arpa/inet.h>
- #include<netinet/in.h>
- #include<string.h>
- #include<unistd.h>
- #define SER_PORT 1200
- int main()
- {
- int a,sersock,newsock,n;
- char str[25],str2[25];
- struct sockaddr_in seraddr;
- struct sockaddr_in cliinfo;
- socklen_t csize=sizeof(cliinfo);
- seraddr.sin_family=AF_INET;
- seraddr.sin_port=htons(SER_PORT);
- seraddr.sin_addr.s_addr=htonl(INADDR_ANY);
- if((sersock=socket(AF_INET,SOCK_STREAM,0))<0)
- {
- error("\n socket");
- exit(0);
- }
- if(bind(sersock,(struct sockaddr *)&seraddr,sizeof(seraddr))<0)
- {
- error("\nBIND");
- exit(0);
- }
- if(listen(sersock,1)<0)
- {
- error("\n LISTEN");
- }
- if((newsock=accept(sersock,(struct sockaddr *)&cliinfo,&csize))<0)
- {
- error("\n ACCEPT");
- exit(0);
- }
- else
- printf("\n now connected to %s\n",inet_ntoa(cliinfo.sin_addr));
- read(newsock,str,sizeof(str));
- do
- {
- printf("\n client msg:%s",str);
- printf("\n server msg:");
- scanf("%s",str2);
- write(newsock,str2,sizeof(str2));
- listen(newsock,1);
- read(newsock,str,sizeof(str));
- n=strcmp(str,"BYE");
- a=strcmp(str2,"BYE");
- }
- while(n!=0||a!=0);
- close(newsock);
- close(sersock);
- return 0;
- }
- chatappclient.c
- #include<stdio.h>
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<arpa/inet.h>
- #include<netinet/in.h>
- #include<unistd.h>
- #define SER_PORT 1200
- int main(int count,char*arg[])
- {
- int a,clisock;
- char str[20],str2[20];
- struct sockaddr_in cliaddr;
- cliaddr.sin_port=htons(SER_PORT);
- cliaddr.sin_family=AF_INET;
- cliaddr.sin_addr.s_addr=inet_addr(arg[1]);
- clisock=socket(AF_INET,SOCK_STREAM,0);
- if(clisock<0)
- {
- perror("\n SOCKET");
- exit(0);
- }
- if(connect(clisock,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
- {
- perror("\n CONNECT");
- exit(0);
- }
- printf("\nclient connected to %s",arg[1]);
- printf("\nCLIENT");
- scanf("%s",&str);
- if(write(clisock,str,sizeof(str))<0)
- {
- printf("\n data could not be sent");
- }
- do
- {
- listen(clisock,1);
- read(clisock,str2,sizeof(str2));
- printf("\nserver msg:%s",str2);
- printf("\nclient msg:");
- scanf("%s",&str);
- a=strcmp(str2,"BYE");
- write(clisock,str2,sizeof(str2));
- }
- while(a!=0);
- close(clisock);
- return 0;
- }
- Activity 8
- Q8. Develop a TCP Client/Server application for transferring a text file from client to server.
- SERVER CODE :#include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<errno.h>
- #include<string.h>
- #include<netdb.h>
- #include<sys/types.h>
- #include<netinet/in.h>
- #include<sys/socket.h>
- int main(int argc, char *argv[])
- {
- int sock_des, port,n,length,new_des,r;
- FILE *fp;
- char buffer[512],s[100],ch;
- struct sockaddr_in server_addr, client_addr;
- if (argc != 2)
- {
- printf("Usage: ./server port\n");
- exit(1);
- port= atoi(argv[1]);
- if ((sock_des = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- {
- perror("socket");
- exit(1);
- }
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(port);
- server_addr.sin_addr.s_addr= htonl(INADDR_ANY);
- if (bind(sock_des,(const struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
- {
- perror("bind");
- exit(1);
- }
- if(listen(sock_des,5)==-1)
- {
- perror("listen");
- exit(1);
- }
- printf("Server is listening at port %d \n", port );
- length=sizeof(struct sockaddr);
- if((new_des= accept(sock_des, (struct sockaddr *)&client_addr,&length)) ==-1 )
- {
- perror("accept");
- exit(1);
- }
- fp=fopen("s1.txt","w");
- while(1)
- {
- r=read(new_des,&ch,1);
- if(r==1)
- fputc(ch,fp);
- else
- break;
- }
- fclose(fp);
- printf("Success");
- close(new_des);
- return 0;
- }
- OUTPUT :-
- CLIENT CODE :#include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<errno.h>
- #include<string.h>
- #include<netdb.h>
- #include<sys/types.h>
- #include<netinet/in.h>
- #include<sys/socket.h>
- int main(int argc, char *argv[])
- {
- int sock_des,port,n;
- char ch;
- FILE *fp;
- struct sockaddr_in server_addr;
- struct hostent *host;
- if (argc != 3)
- {
- printf("Usage: ./client hostname port\n");
- exit(1);
- }
- if ((host=gethostbyname(argv[1])) == NULL)
- {
- printf("Unknown Host\n");
- exit(1);
- }
- port = atoi(argv[2]);
- if ((sock_des = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- {
- perror("socket");
- exit(1);
- }
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(port);
- server_addr.sin_addr = *((struct in_addr *)host->h_addr);
- if(connect(sock_des,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
- {
- perror("connect");
- exit(1);
- }
- fp=fopen("s2.txt","r");
- while((ch=fgetc(fp))!=EOF)
- {
- write(sock_des,&ch,1);
- }
- fclose(fp);
- close(sock_des);
- return 0;
- }
- ACTIVITY 9
- Question :
- Implement a TCP based server program to authenticate the client’s
- Username and
- Password. The authenticity of the client must be sent as
- the reply mess age to the client and display the
- same on the standard output.
- Code :Server :
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- int main(int argc, char *argv[])
- {
- int sock_des,port,n1,n2,s1,s2;
- char null_character;
- char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
- int length,new_des;
- struct sockaddr_in server_addr,client_addr;
- bzero(buffer3,sizeof(buffer3));
- printf("\n Input Authentic Username :\n");
- scanf("%s",buffer3);
- bzero(buffer4,sizeof(buffer4));
- printf("\n Input Authentic Password :\n");
- scanf("%s",buffer4);
- if(argc!=2)
- {
- printf("Usage: ./server port\n");
- exit(1);
- }
- port= atoi(argv[1]);
- if((sock_des=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- perror("socket");
- exit(1);
- }
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(port);
- server_addr.sin_addr.s_addr= htonl(INADDR_ANY);
- if (bind(sock_des,(const struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
- {
- perror("bind");
- exit(1);
- }
- if( listen(sock_des,5)==-1)
- {
- perror("listen");
- exit(1);
- }
- printf("Server is listening at port %d \n", port);
- while(1)
- {
- if((new_des=accept(sock_des,(struct sockaddr *)&client_addr,&length))==-1)
- {
- perror("accept");
- exit(1);
- }
- bzero(buffer1,sizeof(buffer1));
- n1=read(new_des,buffer1,sizeof(buffer1));
- buffer1[n1]=null_character;
- if((s1=strcmp(buffer1,buffer3))==0)
- {
- write(new_des,"Username Match!Enter Password",30);
- printf("Username Match !!!\n");
- bzero(buffer2,sizeof(buffer2));
- n2=read(new_des,buffer2,sizeof(buffer2));
- buffer2[n2]=null_character;
- if((s2=strcmp(buffer2,buffer4))==0)
- {
- write(new_des,"Password Match",15);
- printf("Password Match !!!\n");
- }
- else
- {
- write(new_des,"Password NOT Match",19);
- printf("Password NOT Match !!!\n");
- }
- }
- else
- {
- write(new_des,"Username Not Match",19);
- printf("Username Not Match !!!\n");
- }
- close(new_des);
- }
- }
- Client :-
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- int main(int argc,char *argv[])
- {
- int sock_des,port,n;
- char null_character;
- struct hostent *host;
- char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
- struct sockaddr_in server_addr;
- if(argc!=3)
- {
- printf("Usage: ./client hostname port\n");
- exit(1);
- }
- if((host=gethostbyname(argv[1]))==NULL)
- {
- printf("Unknown Host\n");
- exit(1);
- }
- port = atoi(argv[2]);
- if((sock_des=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- perror("socket");
- exit(1);
- }
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(port);
- server_addr.sin_addr = *((struct in_addr *)host->h_addr);
- if(connect(sock_des,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
- {
- perror("connect");
- exit(1);
- }
- bzero(buffer1,sizeof(buffer1));
- printf("Enter the Username :\n");
- scanf("%s",buffer1);
- write(sock_des,buffer1,sizeof(buffer1));
- bzero(buffer4,sizeof(buffer4));
- n=read(sock_des,buffer4,sizeof(buffer4));
- buffer4[n]= null_character;
- printf("Server Sent : %s\n", buffer4);
- bzero(buffer2,sizeof(buffer2));
- printf("Enter the Password :\n");
- scanf("%s",buffer2);
- write(sock_des,buffer2,sizeof(buffer2));
- bzero(buffer3,sizeof(buffer3));
- n=read(sock_des,buffer3,sizeof(buffer3));
- buffer3[n]=null_character;
- printf("Server Sent : %s\n", buffer3);
- close(sock_des);
- }
Add Comment
Please, Sign In to add comment