0% found this document useful (0 votes)
19 views43 pages

01fe20bec066 218

The document provides details about a laboratory experiment on character stuffing and destuffing. It includes the aim, theoretical background, block diagram, procedure and C program code to implement character stuffing and destuffing. Screenshots of the output are also included.

Uploaded by

rrrlll12345.2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views43 pages

01fe20bec066 218

The document provides details about a laboratory experiment on character stuffing and destuffing. It includes the aim, theoretical background, block diagram, procedure and C program code to implement character stuffing and destuffing. Screenshots of the output are also included.

Uploaded by

rrrlll12345.2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

KLE Technological University, Hubballi-31

School of Electronics Engineering


Program: Electronics and Communication

Computer Communication
and Networking
Laboratory Manual
Course code: 17EECP303

6th Sem

Name: Akhilesh S. Kamble

USN: 01FE20BEC066

Computer Communication and Networking Lab


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual
DEMO EXPERIMENT

Tittle of the experiment: Lan setup

Aim of the experiment: Introduction to hardware components and ethernet LAN.

Theoretical background: A local area network (LAN) is a collection of devices connected


together in one physical location, such as a building, office, or home. A LAN can be
small or large, ranging from a home network with one user to an enterprise network with
thousands of users and devices in an office or school.
Regardless of size, a LAN's single defining characteristic is that it connects devices that
are in a single, limited area. In contrast, a wide area network (WAN) or metropolitan area
network (MAN) covers larger geographic areas. Some WANs and MANs connect many
LANs together.

Simulation:

Computer Communication and Networking Lab 1


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

Conclusion:From this experiment I understood the basic LAN setup.

Computer Communication and Networking Lab 2


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual
Experiment No: 1

1.1 Title of the experiment: Bit Stuffing.

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.

E.g. If data is: 01111011111110 then


After bit stuffing: 01111110 011110111110110 01111110
At receiver de stuffing is done : 01111011111110

1.4 Block Diagram/Flow Chart:

Computer Communication and Networking Lab 3


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

1.5 Step by step procedure to carry out the experiment:


 C program is written to implement bit stuffing and de stuffing.
 Flag bits are inserted at the start and end of the data frame.
 If five consecutive 1s are encountered in data then 0 bit is inserted in bit stuffing.
 In de stuffing the 0 bit after five consecutive 1s is ignored.
 Thus for given input data stuffed and de stuffed frames are obtained.

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++; }

Computer Communication and Networking Lab 4


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

}
out[j]='\0'; 17EECP3
Computer Communication and Networking Laboratory Manual
char res[n+100];
strcat(out,"01111110");
strcpy(res,"01111110");

strcat(res,out)

printf("Original Input : ");


j=0;
while(inp[j]!='\0'){printf("%c",inp[j]);j++;}
j=0;
printf("\nStuffed Data : ");
while(res[j]!='\0'){printf("%c",res[j]);j++;}

// END OF BIT STUFFING *ans stored in res[]

//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;

Computer Communication and Networking Lab 5


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

}
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;
}

Computer Communication and Networking Lab 6


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

1.7 Results: (Screen Shots)

Figure 1 Bit stuffing and de-stuffing

1.7 Conclusion of the experiment:

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.

Computer Communication and Networking Lab 7


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

Experiment No: 2 17EECP3


Computer Communication and Networking Laboratory Manual
2.1 Title of the experiment: Character Stuffing.

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.

Eg: If data is: “This is a very good DLE frame” then


After character stuffing: DLE STX This is a very good DLE DLE frame DLE ETX.
At receiver de stuffing is done: This is a very good DLE frame.

2.4 Block Diagram:

Computer Communication and Networking Lab 8


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

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)

Computer Communication and Networking Lab 9


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

{
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++;

Computer Communication and Networking Lab 10


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

out[j]=arraynew[i];
j++;i++; 17EECP3
Computer Communication and Networking Laboratory Manual
out[j]='\0';
printf("the destuffed array is %s",out);

2.7 Results: (Screen Shots)

Figure 2 Character stuffing output

2.8 Conclusion of the experiment:

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.

Computer Communication and Networking Lab 11


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

Experiment No: 3
17EECP3
Computer Communication and Networking Laboratory Manual
3.1 Title of the experiment: Cyclic Redundancy Code (CRC).

3.2 Aim of the experiment: To write a C program to implement CRC.

3.3 Theoretical background for the experiment

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.

3.4 Block Diagram:

3.5 Step by step procedure to carry out the experiment:

 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.)

Computer Communication and Networking Lab 12


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

 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);

Computer Communication and Networking Lab 13


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

int j = 4;
17EECP3
Computer Communication and Networking Laboratory Manual
while(data[j] != '\0'){

if(rem[0] == '1'){

if(rem[0] == dvsr[0]) rem[0]='0';

else rem[0]='1';

if(rem[1] == dvsr[1]) rem[1]='0';

else rem[1]='1';

if(rem[2] == dvsr[2]) rem[2]='0';

else rem[2]='1';

if(rem[3] == dvsr[3]) rem[3]='0';

else rem[3]='1';

else{

if(data[j] == '\0') break;

rem[0] = rem[1];

rem[1] = rem[2];

rem[2] = rem[3];

rem[3] = data[j];

Computer Communication and Networking Lab 14


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

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'){

if(rem[0] == dvsr[0]) rem[0]='0';

else rem[0]='1';

if(rem[1] == dvsr[1]) rem[1]='0';

else rem[1]='1';

if(rem[2] == dvsr[2]) rem[2]='0';

else rem[2]='1';

if(rem[3] == dvsr[3]) rem[3]='0';

else rem[3]='1';

else{

Computer Communication and Networking Lab 15


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

if(data[j] == '\0') break;


17EECP3
Computer Communication and Networking Laboratory Manual
rem[0] = rem[1];

rem[1] = rem[2];

rem[2] = rem[3];

rem[3] = data[j];

j++;

return 0;

3.7 Results: (Screen Shots)

Figure 3 CRC output

3.8 Conclusion of the experiment:

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.

Computer Communication and Networking Lab 16


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

Experiment No: 4 17EECP3


Computer Communication and Networking Laboratory Manual
4.1 Title of the experiment: Echo Server
4.2 Aim of the experiment: To write a socket program using C to implement Echo server.
4.3 Theoretical background for the experiment: Client server is a computer network
architecture. Server is always on host which has permanent IP address. Client communicate
with the server and have dynamic IP address. Clients cannot communicate directly instead
they can communicate with server as mediator. It is different from P2P architecture where
arbitrary end systems directly communicate. In this experiment a socket program is written in
which the message entered by the client is sent to the server and is echoed back. This is
called echo server.

4.4 Block Diagram:

Computer Communication and Networking Lab 17


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

4.5 Step by step procedure to carry out the experiment:


 Socket program is written to implement the echo-server.
 The server and client programs are run on two different terminals.
 The server program is executed first and later the client program.
 The message is entered at the client end and it is sent to server which gets echoed back at the
receiver.

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>

#define SERVER_TCP_PORT 5750 /*well known port*/


#define BUFLEN 256 /*buffer length*/

int main(int argc, char **argv)


{
int n;
int yes=1;
int sd,new_sd,client_len,port;
struct sockaddr_in server,client;
char buf[BUFLEN];
port=atoi(argv[1]);

//port=5750;

Computer Communication and Networking Lab 18


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

/*create a stream socket*/ 17EECP3


Computer Communication and Networking Laboratory
if((sd=socket(AF_INET,SOCK_STREAM,0))==-1) Manual
{
fprintf(stderr,"can't create a socket\n");
exit(1);
}
/*bind an addresss to the socket*/
//bzero((char *)&server,sizeof(struct sockaddr_in))

server.sin_family=AF_INET;
server.sin_port=port;
server.sin_addr.s_addr=inet_addr("127.0.0.1");

//added for port reuse by us


if(setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes))==-1)
{
perror("setsockopt");
exit(1);
}
if(bind(sd,(struct sockaddr *)&server, sizeof(server))==-1)
{
fprintf(stderr,"can't bind name to socket\n");
exit(1);
}
/*queue upto 5 connect requests*/
listen(sd,5);
while(1)
{
client_len=sizeof(client);
if((new_sd=accept(sd,(struct sockaddr *)&client,&client_len))==-1)
{
fprintf(stderr,"can't accept client\n");
exit(1);
}
n=read(new_sd,buf,sizeof(buf));
printf("The message received by client: %s \n",buf);
write(new_sd,buf,n);
close(new_sd);
}
close(sd);
return(0);
}
#CLIENT PROGRAM
#include<stdio.h>
#include<netdb.h>

Computer Communication and Networking Lab 19


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

#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>

#define SERVER_TCP_PORT 5750 /*well known port*/


#define BUFLEN 256 /*buffer length*/

int main(int argc, char **argv)


{
int n;
int sd,port;
char buf[1024];

struct sockaddr_in server;

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);
}

//bzero((char *)&server,sizeof(struct sockaddr_in));


server.sin_family=AF_INET;
server.sin_port=port;
server.sin_addr.s_addr=inet_addr("127.0.0.1");
/*connecting to the sever*/
if(connect(sd,(struct sockaddr *)&server,sizeof(server))==-1)
{
fprintf(stderr,"can't connect\n");
exit(1);
}
printf("\n\n transmit :(enter the message to be echoed");
scanf("%[^\n]",buf); /*get users text*/
write(sd,buf,BUFLEN); /*send out*/

printf("\n\nEchoed message\n***********************\n");
n=read(sd,buf,sizeof(buf));
printf("%s\n\n\n",buf);
close(sd);
return(0);

Computer Communication and Networking Lab 20


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

4.7 Results: (Screen Shots)

Figure 4 Echo server program output

4.8 Conclusion of the experiment:

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.

Computer Communication and Networking Lab 21


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

Experiment No: 5

5.1 Title of the experiment: File Transfer Protocol.

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.

5.4 Block Diagram:

Computer Communication and Networking Lab 22


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

5.5 Step by step procedure to carry out the experiment:


 Socket program is written to implement FTP.
 A text file is created in the same folder containing the program.
 The program is run. The input text file gets copied to another file.
 Thus FTP is realized.

5.6 C Program/Commands:

FTP SERVER

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

Computer Communication and Networking Lab 23


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

#include <netinet/in.h>
17EECP3
Computer Communication and Networking Laboratory Manual
#include<arpa/inet.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#define CHUNK 1024 /* read 1024 bytes at a time */

void readfile(int new_sd)

char buf[CHUNK];

int n;

FILE *file;

file = fopen("README.txt", "r");

if(file ==NULL)

printf("\n Error in opening a file");

else

while(fgets(buf, sizeof(buf), file) !=NULL)

Computer Communication and Networking Lab 24


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

{
17EECP3
Computer Communication and Networking Laboratory Manual
n=strlen(buf);

write(new_sd, buf,n);

bzero(buf, sizeof(buf));

fclose(file);

int main(int argc, char **argv)

int n;

int yes=1;

int sd, new_sd, client_len, port;

struct sockaddr_in server, client;

char buf[1024];

port = atoi(argv[1]);

// port=5750;

/* create a stream socket */

Computer Communication and Networking Lab 25


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)


17EECP3
{
Computer Communication and Networking Laboratory Manual

fprintf(stderr,"can't create a socket\n");

exit(1);

/* bind an address to the socket */

// bzero((char *)&server, sizeof(struct sockaddr_in));

server.sin_family = AF_INET;

server.sin_port = port;

server.sin_addr.s_addr =inet_addr("10.1.12.211");

if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {

perror("setsockopt");

exit(1);

if(bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1)

fprintf(stderr, "can't bind name to socket\n");

exit(1);

Computer Communication and Networking Lab 26


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

}
17EECP3
Computer Communication and Networking Laboratory Manual
/* queue up to 5 connect requests */

listen(sd,5);

while(1)

client_len = sizeof(client);

if((new_sd = accept(sd, (struct sockaddr *) &client, &client_len)) == -1)

fprintf(stderr, "can't accept client\n");

exit(1);

n = read(new_sd, buf, sizeof(buf));

//printf("The message received by client : %s \n",buf);

//write(new_sd, buf,n);

readfile(new_sd);

close(new_sd);

Computer Communication and Networking Lab 27


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

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>

#define CHUNK 1024

void writefile(int sd)

char buf[CHUNK];

int n,i;

Computer Communication and Networking Lab 28


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

FILE *file;
17EECP3
Computer Communication and Networking Laboratory Manual

file = fopen("temp.txt", "w");

if(file==NULL)

printf("\n Error in opening a file");

while( (read(sd, buf, sizeof(buf))) > 0)

fputs(buf, file);

bzero(buf, sizeof(buf));

int main(int argc, char **argv)

int n;

int sd, port;

char buf[1024];

Computer Communication and Networking Lab 29


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

struct sockaddr_in server;


17EECP3
Computer Communication and Networking Laboratory Manual

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);

// bzero((char *)&server, sizeof(struct sockaddr_in));

server.sin_family = AF_INET;

server.sin_port = port;

server.sin_addr.s_addr = inet_addr("10.1.3.69");

/* connecting to the server */

if(connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1)

fprintf(stderr, "can't connect\n");

exit(1);

Computer Communication and Networking Lab 30


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual
printf("\n Enter the command");

scanf("%s",buf); /* get user's text */

write(sd, buf, sizeof(buf)); /* send it out */

//printf("\n\nEchoed Messege:\n**************\n");

// n = read(sd, buf, sizeof(buf));

// printf("%s\n\n\n",buf);

writefile(sd);

close(sd);

return(0);

5.7 Results: (Screen Shots)

Figure 4 FTP output

Computer Communication and Networking Lab 31


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

5.8 Conclusion of the experiment:

File transfer protocol is implemented. The input file gets copied into the output file. Thus the
FTP is realized.

Experiment No: 6

6.1 Title of the experiment: Chat application.

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.

6.4 Block Diagram:

Computer Communication and Networking Lab 32


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

6.5 Step by step procedure to carry out the experiment:


 Socket program is written to implement the chat application.
 The chat server and client programs are run on two different terminals.
 The server program is executed first and later the client program.
 The message entered at client is sent to server and response to it is sent back to the client.
 This continues until the server and client terminate the chat.

6.6 C Program/Commands:

CHAT SERVER

#include <stdio.h>

Computer Communication and Networking Lab 33


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

#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 SERVER_TCP_PORT 5750 /* well known port */

#define BUFLEN 256 /* buffer length */

#define MAX 80

int flag=0;

int func(int sockfd)

char buff[MAX];

int n;

for(;;)

if(flag==1)

break;

Computer Communication and Networking Lab 34


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

bzero(buff,MAX);
17EECP3
Computer Communication and Networking Laboratory Manual
n=read(sockfd,buff,sizeof(buff));

printf("Message from client is:%s",buff);

bzero(buff,MAX);

n=0;

//while((buff[n++]=getchar())!='\n');

printf("Enter message to be sent to client:\n");

fgets(buff,sizeof(buff),stdin);

n=strlen(buff);

if(strncmp("exit",buff,4)==0)

printf("Server Exit ...\n");

flag=1;

break;

else

write(sockfd,buff,sizeof(buff));

bzero(buff,MAX);

} // for loop

Computer Communication and Networking Lab 35


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
}
Computer Communication and Networking Laboratory Manual

int main(int argc, char **argv)

int n;

int yes=1;

int sd, new_sd, client_len, port;

struct sockaddr_in server, client;

char buff[BUFLEN];

port = atoi(argv[1]);

// port=5750;

/* create a stream socket */

if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

fprintf(stderr,"can't create a socket\n");

exit(1);

/* bind an address to the socket */

// bzero((char *)&server, sizeof(struct sockaddr_in));

Computer Communication and Networking Lab 36


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

server.sin_family = AF_INET;
17EECP3
Computer Communication and Networking Laboratory Manual
server.sin_port = port;

server.sin_addr.s_addr =htonl(INADDR_ANY);

if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {

perror("setsockopt");

exit(1);

if(bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1)

fprintf(stderr, "can't bind name to socket\n");

exit(1);

/* queue up to 5 connect requests */

listen(sd,5);

while(1)

client_len = sizeof(client);

Computer Communication and Networking Lab 37


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

if((new_sd = accept(sd, (struct sockaddr *) &client, &client_len)) == -1)


17EECP3
{
Computer Communication and Networking Laboratory Manual

fprintf(stderr, "can't accept client\n");

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>

Computer Communication and Networking Lab 38


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

#include<stdlib.h>
17EECP3
Computer Communication and Networking Laboratory Manual
#include<unistd.h>

#include<string.h>

#define BUFLEN 256 /* buffer length */

#define MAX 80

void func(int sockfd)

char buff[MAX];

int n;

for(;;)

bzero(buff,sizeof(buff));

printf("Enter the message to be sent: ");

n=0;

fgets(buff,sizeof(buff),stdin);

if((strncmp(buff,"exit",4))==0)

printf("Client Exit...\n");

break;

Computer Communication and Networking Lab 39


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

n=strlen(buff);
17EECP3
Computer Communication and Networking Laboratory Manual
write(sockfd,buff,n);

bzero(buff,sizeof(buff));

read(sockfd,buff,sizeof(buff));

printf("Message from Server : %s",buff);

int main(int argc, char **argv)

int n;

int sd, port;

char buff[BUFLEN];

struct sockaddr_in server;

//command line argument

port=atoi(argv[1]);

/* create a stream socket */

if(( sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

Computer Communication and Networking Lab 40


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

fprintf(stderr, "can't create a socket\n");


17EECP3
exit(1);
Computer Communication and Networking Laboratory Manual

// bzero((char *)&server, sizeof(struct sockaddr_in));

server.sin_family = AF_INET;

server.sin_port = port;

server.sin_addr.s_addr = inet_addr("127.0.0.1");

/* connecting to the server */

if(connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1)

fprintf(stderr, "can't connect\n");

exit(1);

func(sd);

close(sd);

return(0);

6.7 Results: (Screen Shots)

Computer Communication and Networking Lab 41


SCHOOL OF ELECTRONICS ENGINEERING
Program: Electronics and Communication

17EECP3
Computer Communication and Networking Laboratory Manual

Figure 5 Chat application output

6.8 Conclusion of the experiment:

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.

Computer Communication and Networking Lab 42

You might also like