01fe20bec066 218
01fe20bec066 218
Computer Communication
and Networking
Laboratory Manual
Course code: 17EECP303
6th Sem
USN: 01FE20BEC066
17EECP3
Computer Communication and Networking Laboratory Manual
DEMO EXPERIMENT
Simulation:
17EECP3
Computer Communication and Networking Laboratory Manual
17EECP3
Computer Communication and Networking Laboratory Manual
Experiment No: 1
1.2 Aim of the experiment: Write a C program to implement bit stuffing and de-stuffing.
1.3 Theoretical background for the experiment: In the Data Link layer, the stream of bits
from the physical layer is divided into data frames. The data frames can be of fixed length or
variable length. In variable - length framing, the size of each frame to be transmitted may be
different. So, a pattern of bits is used as a delimiter to mark the end of one frame and the
beginning of the next frame. Bit stuffing is the process of inserting non informative bits into
data to break up bit patterns to affect the synchronous transmission of information. These
non-informative streams of bits are called flag which are inserted to indicate the start and end
of the frame. The location of the stuffing bits is communicated to the receiving end of
the data link, where these extra bits are removed to return the bit streams to their original bit
rates or form. To avoid the misinterpretation of data as the flag bits, if 01111110 is taken as
the flag then in data 0 is inserted if five consecutive 1s are encountered. At receiver the 0 bit
after five consecutive 1s is ignored and the original data is extracted.
17EECP3
Computer Communication and Networking Laboratory Manual
1.6 C Program/Commands:
# BIT STUFFING
#include<stdio.h>
#include<string.h>
int main()
{
int n;
printf("Enter data length : ");
scanf("%d",&n);
// BEGINNING OF BIT STUFFING
int j=0,count=0;
char inp[n],temp,out[n+100];
scanf("%s",inp);
for(int i=0;i<strlen(inp);++i)
{
temp=inp[i];
if(temp=='1')count++;
else count=0;
out[j]=temp;
j++;
if(count==5){count=0; out[j]='0'; j++; }
}
out[j]='\0'; 17EECP3
Computer Communication and Networking Laboratory Manual
char res[n+100];
strcat(out,"01111110");
strcpy(res,"01111110");
strcat(res,out)
//START OF DESTUFFING
j=0;
count=0;
int flag=0,k=0;
char dpk[n+100];
while(res[j]!='\0'){
if(res[j]=='0' && res[j+1]=='1' && res[j+2]=='1' && res[j+3]=='1' &&
res[j+4]=='1' && res[j+5]=='1' && res[j+6]=='1' && res[j+7]=='0'){
flag+=1;
j=j+8;
}
if(flag==2){
j=j-8;
break;
}
17EECP3
Computer Communication and Networking Laboratory Manual
if(flag==1){
temp=res[j];
if(temp=='1')count++;
else count=0;
dpk[k]=temp;
k++;
j++;
if(count==5){count=0; j++;}
}
dpk[k]='\0';
j=0;
printf("\nDeStuffed Data : ");
while(dpk[j]!='\0'){printf("%c",dpk[j]);j++;}
printf("\n");
// END OF DESTUFFING
return 0;
}
17EECP3
Computer Communication and Networking Laboratory Manual
The Bit stuffing and de stuffing is implemented for the input data given. The output is
verified for the various input data. Thus bit stuffing is used to synchronize data at the transmitter
and receiver. The applications of bit stuffing are to synchronize several channels before
multiplexing. It is also used in CAN, HDLC and USB.
2.2 Aim of the experiment: To write a C program to implement character stuffing and de
stuffing.
2.3 Theoretical background for the experiment: Same idea as bit-stuffing, but operates on
bytes instead of bits. Use reserved characters to indicate the start and end of a frame. For
instance, use the two-character sequence DLE STX to signal the beginning of a frame, and
the sequence DLE ETX to flag the frame's end. Other reserved characters are ACK, NAK
and SYN. If these characters are present as a part of data then DLE is inserted before it so
that the data is not misinterpreted as end of frame.
17EECP3
Computer Communication and Networking Laboratory Manual
2.5 Step by step procedure to carry out the experiment:
C program is written to implement character stuffing and de stuffing.
Flag DLE STX is inserted at the start of frame and DLE ETX is inserted at end of the
data frame.
If any of the reserved characters appear in the data then DLE is inserted before it.
In de stuffing the first encountered DLE is ignored and the successive reserved characters
are taken as data.
Thus for given input data stuffed and de stuffed frames are obtained.
2.6 C Program/Commands:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int j=0;
int i=0;
void main()
{
char flag[]={"DLE"};
char input[100];
char arraynew[100];
char temp[4];
printf("enter the string\n");
scanf("%s",input);
int l= strlen(input);
for(i=0;i<=l-3;i++)
{
temp[0]=input[i];
temp[1]=input[i+1];
temp[2]=input[i+2];
temp[3]='\0';
if(strcmp(temp,"ACK")==0 || strcmp(temp,"SYN")==0)
{
arraynew[j]='D'; 17EECP3
Computer Communication and Networking Laboratory Manual
j++;
arraynew[j]='L';
j++;
arraynew[j]='E';
j++;
}
arraynew[j]=input[i];
j++;
}
arraynew[j]=input[i];
i++;
j++;
arraynew[j]=input[i];
i++;
j++;
arraynew[j]='\0';
printf("the input array was %s\n",input);
printf("the stuffed array is %s",arraynew);
/*--------------------------DESTUFFING-------------------------------------------*/
int l1=strlen(arraynew);
char out[100];
i=0;
j=0;
for(i=0;i<=l1-3;i++)
{
temp[0]=arraynew[i];
temp[1]=arraynew[i+1];
temp[2]=arraynew[i+2];
temp[3]='\0';
if(strcmp(temp,flag)==0)
{
i=i+3;
}
out[j]=arraynew[i];
j++;
}
out[j]=arraynew[i];
j++;i++;
out[j]=arraynew[i];
j++;i++; 17EECP3
Computer Communication and Networking Laboratory Manual
out[j]='\0';
printf("the destuffed array is %s",out);
The character stuffing and de stuffing is implemented for the input data given. The output is
verified for the various input data. Thus character stuffing is used to synchronize data at the
transmitter and receiver. As most computer networks cannot reserve characters for use by the
network it is not preferably used.
Experiment No: 3
17EECP3
Computer Communication and Networking Laboratory Manual
3.1 Title of the experiment: Cyclic Redundancy Code (CRC).
The cyclic redundancy check, or CRC, is a technique for detecting errors in digital data, but
not for making corrections when errors are detected. It is used primarily in data transmission.
In the CRC method, a certain number of check bits, often called a checksum, are appended to
the message being transmitted. The receiver can determine whether or not the check bits
agree with the data, to ascertain with a certain degree of probability whether or not an error
occurred in transmission. If an error occurred, the receiver sends a "negative
acknowledgement" (NAK) back to the sender, requesting that the message be retransmitted.
Start
Enter the message to be transmitted
Append the message with 16(since it is 16-bit CRC) 0`s (i.e. if you input a 5 digit message, the appended
message should be 21-bits.)
XOR appended message and transmit it.(Here, you compare with an already existing string such as
10001000000100001 and replace the bits the same way XOR operation works) 17EECP3
Computer Communication and Networking Laboratory Manual
Verify the message that is received is the same as the one sent.
End
3.6 C Program/Commands:
#include<stdio.h>
#include<string.h>
int main(){
int n;
scanf("%d",&n);
char data[n+1],ctc[4]="000";
scanf("%s",data);
strcat(data,ctc);
char dvsr[5]="1101",rem[4];
rem[0] = data[0];
rem[1] = data[1];
rem[2] = data[2];
rem[3] = data[3];
printf("%s\n",data);
int j = 4;
17EECP3
Computer Communication and Networking Laboratory Manual
while(data[j] != '\0'){
if(rem[0] == '1'){
else rem[0]='1';
else rem[1]='1';
else rem[2]='1';
else rem[3]='1';
else{
rem[0] = rem[1];
rem[1] = rem[2];
rem[2] = rem[3];
rem[3] = data[j];
j++;
17EECP3
}
Computer Communication and Networking Laboratory Manual
printf("remainder : %c %c %c %c",rem[0],rem[1],rem[2],rem[3]);
while(data[j] != '\0'){
if(rem[0] == '1'){
else rem[0]='1';
else rem[1]='1';
else rem[2]='1';
else rem[3]='1';
else{
rem[1] = rem[2];
rem[2] = rem[3];
rem[3] = data[j];
j++;
return 0;
The CRC is implemented for the input data given. The output is verified for the various input
data. Thus CRC is used for error detection. It is widely used in communication system and the
data is queried to be retransmitted if the check values do not match at the receiver.
17EECP3
Computer Communication and Networking Laboratory Manual
4.6 C Program/Commands:
# SERVER PROGRAM
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
//port=5750;
server.sin_family=AF_INET;
server.sin_port=port;
server.sin_addr.s_addr=inet_addr("127.0.0.1");
#include<sys/types.h>
#include<sys/socket.h> 17EECP3
Computer Communication
#include<netinet/in.h> and Networking Laboratory Manual
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
port=atoi(argv[1]);
/*create a stream socket*/
if((sd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"can't create a socket\n");
exit(1);
}
printf("\n\nEchoed message\n***********************\n");
n=read(sd,buf,sizeof(buf));
printf("%s\n\n\n",buf);
close(sd);
return(0);
17EECP3
Computer Communication and Networking Laboratory Manual
The echo server program is thus executed and socket programming is used to implement it.
The output is obtained and the program is checked by sending and receiving different messages.
17EECP3
Computer Communication and Networking Laboratory Manual
Experiment No: 5
5.2Aim of the experiment: To write a socket program to implement FTP server and client.
5.3 Theoretical background for the experiment: FTP stands for file transfer protocol which is
protocol used to transfer file from remote host. It uses client-server architecture. FTP host stores
the file. Client logs into host and client program sends command to get a file. It uses two ports.
Port 21 for TCP control connection and Port 20 for TCP data connection. Only the data
connection is terminated after transfer of every file but the control connection exists. Thus file
transfer can be achieved using this application layer protocol.
17EECP3
Computer Communication and Networking Laboratory Manual
5.6 C Program/Commands:
FTP SERVER
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
17EECP3
Computer Communication and Networking Laboratory Manual
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
char buf[CHUNK];
int n;
FILE *file;
if(file ==NULL)
else
{
17EECP3
Computer Communication and Networking Laboratory Manual
n=strlen(buf);
write(new_sd, buf,n);
bzero(buf, sizeof(buf));
fclose(file);
int n;
int yes=1;
char buf[1024];
port = atoi(argv[1]);
// port=5750;
exit(1);
server.sin_family = AF_INET;
server.sin_port = port;
server.sin_addr.s_addr =inet_addr("10.1.12.211");
perror("setsockopt");
exit(1);
exit(1);
}
17EECP3
Computer Communication and Networking Laboratory Manual
/* queue up to 5 connect requests */
listen(sd,5);
while(1)
client_len = sizeof(client);
exit(1);
//write(new_sd, buf,n);
readfile(new_sd);
close(new_sd);
close(sd);
17EECP3
Computer Communication and Networking Laboratory Manual
return(0);
FTP CLIENT
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
char buf[CHUNK];
int n,i;
FILE *file;
17EECP3
Computer Communication and Networking Laboratory Manual
if(file==NULL)
fputs(buf, file);
bzero(buf, sizeof(buf));
int n;
char buf[1024];
port=atoi(argv[1]);
exit(1);
server.sin_family = AF_INET;
server.sin_port = port;
server.sin_addr.s_addr = inet_addr("10.1.3.69");
exit(1);
17EECP3
Computer Communication and Networking Laboratory Manual
printf("\n Enter the command");
//printf("\n\nEchoed Messege:\n**************\n");
// printf("%s\n\n\n",buf);
writefile(sd);
close(sd);
return(0);
17EECP3
Computer Communication and Networking Laboratory Manual
File transfer protocol is implemented. The input file gets copied into the output file. Thus the
FTP is realized.
Experiment No: 6
6.2 Aim of the experiment: To write a socket program using C to implement chat application.
6.3 Theoretical background for the experiment: Client-server architecture is used to build the
chat application. All that is needed is a server application which can supply the location of a
specific user to a client requesting that user and a client application that will connect to a given
location for a user. Thus messages can be sent and response is given for received messages.
17EECP3
Computer Communication and Networking Laboratory Manual
6.6 C Program/Commands:
CHAT SERVER
#include <stdio.h>
#include <sys/types.h>
17EECP3
Computer Communication and Networking Laboratory Manual
#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#define MAX 80
int flag=0;
char buff[MAX];
int n;
for(;;)
if(flag==1)
break;
bzero(buff,MAX);
17EECP3
Computer Communication and Networking Laboratory Manual
n=read(sockfd,buff,sizeof(buff));
bzero(buff,MAX);
n=0;
//while((buff[n++]=getchar())!='\n');
fgets(buff,sizeof(buff),stdin);
n=strlen(buff);
if(strncmp("exit",buff,4)==0)
flag=1;
break;
else
write(sockfd,buff,sizeof(buff));
bzero(buff,MAX);
} // for loop
17EECP3
}
Computer Communication and Networking Laboratory Manual
int n;
int yes=1;
char buff[BUFLEN];
port = atoi(argv[1]);
// port=5750;
exit(1);
server.sin_family = AF_INET;
17EECP3
Computer Communication and Networking Laboratory Manual
server.sin_port = port;
server.sin_addr.s_addr =htonl(INADDR_ANY);
perror("setsockopt");
exit(1);
exit(1);
listen(sd,5);
while(1)
client_len = sizeof(client);
exit(1);
func(new_sd);
close(new_sd);
close(sd);
return(0);
CHAT CLIENT
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
17EECP3
Computer Communication and Networking Laboratory Manual
#include<unistd.h>
#include<string.h>
#define MAX 80
char buff[MAX];
int n;
for(;;)
bzero(buff,sizeof(buff));
n=0;
fgets(buff,sizeof(buff),stdin);
if((strncmp(buff,"exit",4))==0)
printf("Client Exit...\n");
break;
n=strlen(buff);
17EECP3
Computer Communication and Networking Laboratory Manual
write(sockfd,buff,n);
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
int n;
char buff[BUFLEN];
port=atoi(argv[1]);
server.sin_family = AF_INET;
server.sin_port = port;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
exit(1);
func(sd);
close(sd);
return(0);
17EECP3
Computer Communication and Networking Laboratory Manual
The chat application program is thus executed and socket programming is used to implement
this application. The expected output is obtained. The client and server are made to exchange the
messages.