Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string.h>
- using namespace std;
- char stack[6][30];
- int top=-1;
- void push()
- { int j;
- if(top==5)
- cout<<"stack is full"; //to check whether stack is full or not
- else
- {
- char x[30];
- cout<<"Enter layer name: \n";
- cin>>x;
- top++;
- for(j=0;x[j]!='\0';j++)
- {
- stack[top][j]=x[j]; //Layer names are pushed into the stack
- }
- }
- }
- void pop(char st[30])
- {int j;
- char x[30];
- if(top==-1)
- cout<<"stack is empty"; //to check whether stack is empty or not
- else
- {
- for(j=0;stack[top][j]!='\0';j++)
- {
- x[j]=stack[top][j]; //popping out the layer names from the stack
- }
- top--;
- cout<<x;
- for(j=0;j<30;j++)
- {
- x[j]='\0';
- }
- cout<<" "<<st<<"\n";
- cout<<"\n\n";
- }
- }
- void display()
- {
- char s[30];
- cout<<"Enter the string: ";
- cin>>s;
- cout<<"-----------\n";
- cout<<"TRANSMITTER\n";
- cout<<"-----------\n\n";
- cout<<stack[0]<<"\n\n";
- for(int i=1;i<=top;i++)
- {
- cout<<stack[i]; //Printing the stack
- cout<<" "<<s<<"\n";
- cout<<"\n\n";
- }
- cout<<"MESSAGE ENTERED INTO PHYSICAL LAYER AND TRANSMITTED.\n\n";
- cout<<"-----------\n";
- cout<<"RECEIVER\n";
- cout<<"-----------\n\n";
- cout<<"MESSAGE ENTERED INTO PHYSICAL LAYER.\n";
- for(int i=top;i>=1;i--)
- pop(s);
- cout<<stack[0]<<"\n\n"; //Printing the first element of the stack
- }
- int main()
- {
- int i;
- for(i=0;i<6;i++)
- push();
- cout<<"--------------------\n\n";
- display();
- return 0;
- }
- 2. a) Bit stuffing
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- void main()
- {
- inti,j,count=0,nl;
- charstr[100];
- //clrscr();
- printf("enter the bit string: ");
- gets(str);
- for (i=0;i<strlen(str);i++) {
- count=0;
- //the following code search the six ones in given string
- for (j=i;j<=(i+5);j++) {
- if(str[j]=='1') {
- count++;
- }
- }
- //if there is six ones then folling code execute to bit stuffing after five ones
- 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();
- }
- b) Byte stuffing
- /*Implement the DataLinklayer framing methods such as Character
- stuffing*/
- #include<stdio.h>
- #include<string.h>
- #include<conio.h>
- void main()
- {
- intj,l,m,c,k;
- char a[50],b[50];
- printf("Enter the string: ");
- scanf("%s",a);
- strcpy(b,"DLESTX"); //Copy DLESTX in b
- m=strlen(a); //Length of a is stored in m
- for(j=0;j<m;)
- {
- if(a[j]=='d')
- {
- if(a[j+1]=='l')
- {
- if(a[j+2]=='e')
- {
- c=j+2;
- for(l=0;l<3;l++)
- {
- for(k=m;k>c;k--)
- {
- a[k]=a[k-1];
- }
- m++;
- a[m]=NULL;
- c+=1;
- }
- a[j+3]='d';
- a[j+4]='l';
- a[j+5]='e';
- a[m]=NULL;
- j+=5;
- }
- }
- }
- j++;
- }
- strcat(b,a);
- strcat(b,"DLEETX");
- printf("\n%s",b);
- printf("\nReceiver side:"); //Reciever side
- m=strlen(a);
- for(j=0;j<m;)
- {
- if(a[j]=='d')
- {
- if(a[j+1]=='l')
- {
- if(a[j+2]=='e')
- {
- c=j;
- for(l=0;l<3;l++)
- {
- for(k=c;k<m;k++)
- a[k]=a[k+1];
- }
- c++;
- }
- j=c;
- }
- }
- j++;
- }
- printf("\n%s",a);
- getch();
- }
- c) To implement Character Count
- Program-
- #include <stdio.h>
- #include <string.h>
- int main(){
- int size;
- int total=0;
- int i=1;
- intj,k=0;
- intfsize;
- printf("Enter the no. of characters: "); //Entering number of characters
- scanf("%d",&size);
- charstr[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]; //Incrementing frame size
- 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);
- }
- return 0;
- }
- 3. CRC
- #include<stdlib.h>
- #include<conio.h>
- #include<stdio.h>
- void main()
- {
- inti,j,n,g,a,arr[20],gen[20],b[20],q[20],s;
- printf("Transmitter side:");
- printf("\nEnter no. of data bits:"); //Number of data bits are entered
- scanf("%d",&n);
- printf("Enter data:");
- for(i=0;i<n;i++)
- scanf("%d",&arr[i]); //Data is stored in the form of an array
- printf("Enter size of generator:");
- scanf("%d",&g);
- do{
- printf("Enter generator:"); //Divisor value is entered by the user
- for(j=0;j<g;j++)
- scanf("%d",&gen[j]);
- }
- while(gen[0]!=1); //Converting into Matrix
- printf("\n\tThe generator matrix:");
- for(j=0;j<g;j++)
- printf("%d",gen[j]);
- a=n+(g-1);
- printf("\n\tThe appended matrix is:"); //Displaying appended matrix
- for(i=0;i< j;++i)
- arr[n+i]=0;
- for(i=0;i< a;++i)
- printf("%d",arr[i]);
- for(i=0;i< n;++i)
- q[i]= arr[i];
- for(i=0;i< n;++i)
- {
- if(arr[i]==0)
- {
- for(j=i;j<g+i;++j)
- arr[j] = arr[j]^0;
- }
- else //Performing Division
- {
- arr[i] = arr[i]^gen[0];
- arr[i+1]=arr[i+1]^gen[1];
- arr[i+2]=arr[i+2]^gen[2];
- arr[i+3]=arr[i+3]^gen[3];
- }
- }
- printf("\n\tThe CRC is :");
- for(i=n;i< a;++i)
- printf("%d",arr[i]);
- s=n+a;
- for(i=n;i<s;i++)
- q[i]=arr[i];
- printf("\n");
- for(i=0;i<a;i++)
- printf("%d",q[i]);
- getch();
- }
- Checksum Calculation
- Code:
- #include<stdio.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]; //Performing sum on each value
- printf("SUM IS: %d",sum);
- checksum=~sum;
- printf("\nSENDER's CHECKSUM IS:%d",checksum);
- return checksum; //returning value of 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;
- }
- int main()
- {
- int a[10],i,m,scheck,rcheck;
- 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");
- return 0;
- }
- 5. Hamming Code
- Code:
- #include<stdio.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 is stored
- 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("\nerror 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]); //Printing the correct message
- }
- }
- }
- Output:
- 6. Simulation of Sliding protocol
- Code:-
- #include<stdio.h>
- int main()
- {
- char sender[50],receiver[50],temp[50];
- int i,winsize;
- printf("\n ENTER THE WINDOWS SIZE : ");
- scanf("%d",&winsize);
- printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
- printf("\n ENTER THE DATA TO BE SENT: ");
- fflush(stdin);
- gets(sender);
- for(i=0;i<winsize;i++)
- temp[i]= receiver[i]; //Swapping
- receiver[i]=sender[i];
- receiver[i]=temp[i];
- printf("\n MESSAGE SEND BY THE SENDER:\n");
- puts(sender);
- printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
- printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
- for(i=0;i<winsize;i++);
- printf("\n ACK:%d",i);
- printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
- puts(receiver);
- printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
- }
- 6.Simulation of Sliding protocol
- PROGRAM:
- #include<stdio.h>
- int main()
- {
- char sender[50],receiver[50],temp[50];
- inti,winsize;
- printf("\n ENTER THE WINDOWS SIZE : ");
- scanf("%d",&winsize);
- printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
- printf("\n ENTER THE DATA TO BE SENT: ");
- fflush(stdin);
- gets(sender);
- for(i=0;i<winsize;i++)
- temp[i]= receiver[i];
- receiver[i]=sender[i];
- receiver[i]=temp[i];
- printf("\n MESSAGE SEND BY THE SENDER:\n");
- puts(sender);
- printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
- printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
- for(i=0;i<winsize;i++);
- printf("\n ACK:%d",i);
- printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
- puts(receiver);
- printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
- }
- 7. Chat application using TCP/IP
- Code:
- // Server Program - Communicating to single client multiple number of times
- #include<stdio.h>
- #include<netinet/in.h>
- #include<netdb.h>
- #include<string.h>
- #define SERV_TCP_PORT 5035
- int main(int argc,char**argv)
- {
- int sockfd,newsockfd,clength;
- struct sockaddr_in serv_addr,cli_addr;
- char buffer[4096];
- //creating a socket
- sockfd=socket(AF_INET,SOCK_STREAM,0);
- serv_addr.sin_family=AF_INET;
- serv_addr.sin_addr.s_addr=INADDR_ANY;
- serv_addr.sin_port=htons(SERV_TCP_PORT);
- printf("\nStart");
- //bind the socket to port address and any local interface
- bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
- printf("\nListening...");
- printf("\n");
- listen(sockfd,5);
- clength=sizeof(cli_addr);
- newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
- //newsockfd=accept(sockfd,(struct sockaddr*)NULL,NULL);
- printf("\nAccepted");
- printf("\n");
- while(1)
- {
- bzero(buffer,4096);
- read(newsockfd,buffer,4096);
- printf("\nClient message:%s",buffer);
- write(newsockfd,buffer,strlen(buffer)+1);
- printf("\n");
- }
- //close(sockfd);
- //return 0;
- }
- Client Side:
- Client Program - Multiple communications to server
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<netdb.h>
- #define SERV_TCP_PORT 5035
- int main(int argc,char*argv[])
- {
- int sockfd;
- struct sockaddr_in serv_addr;
- struct hostent *server;
- char buffer[4096];
- int k=5,cnt=0;
- sockfd=socket(AF_INET,SOCK_STREAM,0);
- serv_addr.sin_family=AF_INET;
- serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
- serv_addr.sin_port=htons(SERV_TCP_PORT);
- printf("\nReady for sending...");
- connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
- while(cnt <= k)
- {
- printf("\nEnter the message to send\n");
- printf("\nClient: ");
- fgets(buffer,4096,stdin);
- write(sockfd,buffer,4096);
- printf("Serverecho:%s",buffer);
- printf("\n");
- cnt++;
- }
- close(sockfd);
- return 0;
- 8. TCP client/server application for transferring a text file from client to server
- PROGRAM:
- Server:
- #include<stdio.h>
- #include<sys/types.h>
- #include<string.h>
- #include<stdlib.h>
- #include<sys/socket.h>
- #include<arpa/inet.h>
- #include<unistd.h>
- #define SA structsockaddr
- #define LISTENQ 5
- int main(intargc,char**argv)
- {
- intfd,sockfd,listenfd,connfd;
- pid_tchildpid;
- socklen_t client;
- structsockaddr_inservaddr,cliaddr;
- listenfd=socket(AF_INET,SOCK_STREAM,0);
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
- servaddr.sin_port=htons(atoi(argv[1]));
- bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
- listen(listenfd,LISTENQ);
- client=sizeof(cliaddr);
- connfd=accept(listenfd,(SA*)&cliaddr,&client);
- char buffer[100];
- FILE *fp;
- read(connfd,buffer,100);
- fp=fopen("add1.txt","w");
- fprintf(fp,"%s",buffer);
- printf("the file was received successfully");
- printf("the new file created is add1.txt");
- }
- Client :
- #include<arpa/inet.h>
- #include<unistd.h>
- #define SA structsockaddr
- int main(intargc,char**argv)
- {
- intsockfd;
- charfname[25];
- intlen;
- structsockaddr_inservaddr,cliaddr;
- sockfd=socket(AF_INET,SOCK_STREAM,0);
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
- servaddr.sin_port=htons(atoi(argv[1]));
- inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
- connect(sockfd,(SA*)&servaddr,sizeof(servaddr));
- char buffer[100];
- FILE *f;
- f=fopen("add.txt","r");
- fscanf(f,"%s",buffer);
- write(sockfd,buffer,100);
- printf("the file was sent successfully");
- }
- 9. TCP based server program to authenticate the client’s Username and Password.
- PROGRAM:
- 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(intargc, char *argv[])
- {
- int sock_des,port,n1,n2,s1,s2;
- char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
- intlength,new_des;
- structsockaddr_inserver_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,(conststructsockaddr *)&server_addr,sizeof(structsockaddr))==-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,(structsockaddr *)&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,”UsernameMatch!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(intargc,char *argv[])
- {
- intsock_des,port,n;
- structhostent *host;
- char buffer1[100],buffer2[100],buffer3[100],buffer4[100];
- structsockaddr_inserver_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 = *((structin_addr *)host->h_addr);
- if(connect(sock_des,(structsockaddr *)&server_addr,sizeof(structsockaddr))==-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