尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
1 | P a g e
Submitted To:- Submitted By:-
Sir Rohit Negi Uttam Singh Chaudhary
(17EMBIT055)
2 | P a g e
List of Experiments
S.no Experiment Name Page
no
1 Define different types of LAN.
(LAN, MAN, WAN)
4-6
2 Define and describe all the network
Equipment/Devices.
7-12
3 Define and describe different types of
network topology Star, Ring, Bus and
Mesh.
13-17
4 Write a program in c to implement
various type of framing method.
 Bit stuffing
 Byte stuffing
 Character Stuffing
18-27
5 Write a program in C to implement
Various routing algorithm.
 Simple redundancy check
 Cyclic redundancy check
 Checksum
 Hamming code
28-42
3 | P a g e
6 Write a program in C to implement
routing algorithm.
 Distance vector routing
 Link state routing
43-54
7 Write two program in C Hello_Client
and Hello_Server.
 The server listens for and accepts a
single TCP connection. It reads all
the data it can from that connection
and print it to screen then it close
the connection.
 The client connects to the server
sends the string ”COMPUTER
NETWORK” then closes the
connection.
55-66
4 | P a g e
Experiment:-1
Aim: Define different types of LAN. (LAN, MAN,
WAN)
LAN: Local Area Network
A LAN connects network devices over a relatively short
distance. A networked office building, school, or home
usually contains a single LAN, though sometimes one
building will contain a few small LANs (perhaps one per
room), and occasionally a LAN will span a group of
nearby buildings. In TCP/IP networking, a LAN is often
but not always implemented as a single IP subnet.
LANs are also typically owned, controlled, and managed
by a single person or organization. They also tend to use
certain connectivity technologies, primarily Ethernet and
Token Ring.
5 | P a g e
WAN: Wide Area Network
As the term implies, a WAN spans a large physical
distance. The Internet is the largest WAN, spanning
the Earth.
A WAN is a geographically-dispersed collection of
LANs. A network device called a router connects
LANs to a WAN. In IP networking, the router
maintains both a LAN address and a WAN address.
A WAN differs from a LAN in several important
ways. Most WANs (like the Internet) are not owned
by any one organization but rather exist under
collective or distributed ownership and management.
WANs tend to use technology like ATM, Frame
Relay and X.25 for connectivity over the longer
distances.
6 | P a g e
MAN: Metropolitan Area Network
A large computer network which extends to a city or to a
large university campus is termed as metropolitan area
network or MAN. The purpose of MAN (Metropolitan
Area Network) is to provide the link to the internet in the
long run. A MAN (Metropolitan Area Network) usually
incorporates a number of LANs to form a network. This
large network MANs (Metropolitan Area Network)
backbone comprises of an optical fiber set-up.
7 | P a g e
Experiment:-2
Aim:- Define and also describe different types of
network Equipment /devices.
Different networking devices:
Network Hub:
Network Hub is a networking device which is used to
connect multiple network hosts. A network hub is also
used to do data transfer. The data is transferred in terms of
packets on a computer network. So when a host sends a
data packet to a network hub, the hub copies the data
packet to all of its ports connected to. Like this, all the
ports know about the data and the port for whom the
packet is intended, claims the packet.
8 | P a g e
However, because of its working mechanism, a hub is not
so secure and safe. Moreover, copying the data packets on
all the interfaces or ports makes it slower and more
congested which led to the use of network switch.
Network Switch:
Like a hub, a switch also works at the layer of LAN
(Local Area Network) but you can say that a switch is
more intelligent than a hub. While hub just does the work
of data forwarding, a switch does 'filter and forwarding'
which is a more intelligent way of dealing with the data
packets.
So, when a packet is received at one of the interfaces of
the switch, it filters the packet and sends only to the
interface of the intended receiver.
9 | P a g e
For this purpose, a switch also maintains a CAM (Content
Addressable Memory) table and has its own system
configuration and memory. CAM table is also called as
forwarding table or forwarding information base (FIB).
Modem:
A Modem is somewhat a more interesting network device
in our daily life. So if you have noticed around, you get an
internet connection through a wire (there are different
types of wires) to your house. This wire is used to carry
our internet data outside to the internet world.
However, our computer generates binary data or digital
data in forms of 1s and 0s and on the other hand, a wire
carries an analog signal and that's where a modem comes
in.
10 | P a g e
A modem stands for (Modulator + Demodulator). That
means it modulates and demodulates the signal between
the digital data of a computer and the analog signal of a
telephone line.
Network Router:
A router is a network device which is responsible for
routing traffic from one to another network. These two
networks could be a private company network to a public
network. You can think of a router as a traffic police who
directs different network traffic to different directions.
11 | P a g e
Bridge:
If a router connects two different types of networks, then
a bridge connects two sub networks as a part of the same
network. You can think of two different labs or two
different floors connected by a bridge.
12 | P a g e
Repeater:
A repeateris an electronic device that amplifies the signal
it receives. In other terms, you can think of repeater as a
device which receives a signal and retransmits it at a
higher level or higher power so that the signal can cover
longer distances
.
13 | P a g e
Experiment:-3
Aim: Define and describe different types of
network topology Star, Ring, Bus and Mesh
STAR TOPOLOGY:
A star topology is a topology where every node in the
network is connected to one central node. Every device in
the network is directly connected to the central node and
indirectly connected to every other node.
14 | P a g e
The relationship between these elements is that the central
network device is a server and other devices are treated as
clients. The central node has the responsibility of
managing data transmissions across the network. The
central node or hub also acts as a repeater.
In star topologies, computersare connected with a coaxial
cable, twisted pair, or optical fiber cable.
RING TOPOLOGY:
In networks with a ring topology, computers are
connected to each other in a circular format. Every device
in the network will have two neighbors and no more or no
less. The first node is connected to the last node to link
the loop together.
Ring topologies are unidirectional but can also be made
bidirectional.
15 | P a g e
Ring topologies are unidirectional but can also be made
bidirectional.
To make ring topologies bidirectional you would need to
have two connections between network nodes to form
a Dual Ring Topology. Ring topologies can sustain large
networks much more effectively than bus topologies.
16 | P a g e
BUS TOPOLOGY:-
Bus topology is a type of network where every device is
connected to a single cable which runs from one end of
the network to the other. This type of type of topology is
often referred to as line topology. In a bus topology, data
is transmitted in one direction only. If the bus topology
has two endpoints then it is referred to as a linear bus
topology. Organizations using this type of topology will
generally use an RJ45 cable to link devices together.
17 | P a g e
MESH TOPOLOGY:-
A mesh topology is a point-to-point connectionwhere
nodes are interconnected. In this form of topology, data is
transmitted via two methods: routing and flooding.
Routing is where nodes use routing logic to work out the
shortest distance to the packet’s destination. In contrast
flooding, data is sent to all nodes within the network.
Floodingdoesn’t require any form of routing logic to
work. The interconnectivity of nodesmakes them
extremely resistant to failures.
18 | P a g e
Experiment:-4
1.Bit Stuffing
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
Aim: Write a program in c to implement various type
of framing method.
 Bit stuffing
 Byte stuffing
 Character Stuffing
19 | P a g e
printf("Enterthe frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5;k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
20 | P a g e
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
getch();
}
21 | P a g e
OUTPUT:
Enterframe size (Example: 8):12
Enterthe frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0
0 1
After Bit Stuffing :0101111101001
2.Byte Stuffing
#include<stdio.h>
#include<string.h>
void main(){
char frame[50][50],str[50][50];
char flag[10];
strcpy(flag,"flag");
char esc[10];
strcpy(esc,"esc");
int i,j,k=0,n;
strcpy(frame[k++],"flag");
22 | P a g e
printf("Enterno.ofString :t");
scanf("%d",&n);
printf("EnterString n");
for(i=0;i<=n;i++)
{
gets(str[i]);
}
printf("You entered :n");
for(i=0;i<=n;i++)
{
puts(str[i]);
}
printf("n");
for(i=1;i<=n;i++)
{
if(strcmp(str[i],flag)!=0 && strcmp(str[i],esc)!=0)
{
strcpy(frame[k++],str[i]);
}
else
{
strcpy(frame[k++],"esc");
strcpy(frame[k++],str[i]);
}
23 | P a g e
}
strcpy(frame[k++],"flag");
//frame[k++]='0';
printf("------------------------------n");
printf("Byte stuffing at sender side:nn");
printf("------------------------------n");
for(i=0;i<k;i++)
{
printf("%st",frame[i]); }
}
Output:-
Enter no.of String : 2
Enter String
Apple
cake
You entered :
Apple
cake -
--------------------------
Byte stuffing at sender side:
------------------------------
flag Apple cake flag
24 | P a g e
3.Character Stuffing
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
clrscr();
printf("enter stringn");
scanf("%s",&a);
n=strlen(a);
printf("enter positionn");
scanf("%d",&pos);
if(pos>n)
{
25 | P a g e
printf("invalid position, Enter again :");
scanf("%d",&pos);
}
printf("enter the charactern");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
26 | P a g e
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++; }
27 | P a g e
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='0';
printf("nframe after stuffing:n");
printf("%s",b);
getch();
}
INPUT:-
enter string:-
asdlefgh
enter position: 8
invalid position, enter again: 3
enter the character: k
OUTPUT:-
frame after stuffing:-
dlestx as dle k dle dle dlefgh dleetx
28 | P a g e
Experiment:-5
Aim:- Write a program in C to implement Various
routing algorithm.
 Simple redundancy check
 Cyclic redundancy check
 Checksum
 Hamming code
1.Simple Parity Check:
# include<bits/stdc++.h>
# define bool int
using namespace std;
// Function to get parity of number n. It returns1
// if n has odd parity, and returns 0 if n has even
// parity
bool getParity(unsigned int n)
{
bool parity = 0;
while (n)
29 | P a g e
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
/* Driver program to test getParity() */
int main()
{ unsigned int n = 7;
cout<<"Parity of no "<<n<<" = "<<(getParity(n)?
"odd": "even");
getchar();
return 0; }
OUTPUT
Even Parity
Odd Parity
30 | P a g e
2.Cyclic RedundancyCheck
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,keylen,msglen;
char input[100],
key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("EnterData: ");
gets(input);
printf("EnterKey: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
31 | P a g e
for(i=0;i<keylen-1;i++)
{
input[msglen+i]='0';
}
for(i=0;i<keylen;i++)
temp[i]=input[i];
for(i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for(j=0;j<keylen;j++)
key[j]='0';
else
for(j=0;j<keylen;j++)
key[j]=key1[j];
for(j=keylen-1;j>0;j--)
{
if(temp[j]==key[j])
32 | P a g e
rem[j-1]='0';
else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("nQuotient is ");
for(i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("nRemainder is ");
for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("nFinal data is: ");
for(i=0;i<msglen;i++)
printf("%c",input[i]);
for(i=0;i<keylen-1;i++)
33 | P a g e
printf("%c",rem[i]);
getch();
}
Output:
EnterData: 11110110101
EnterKey: 111010
Quotient is 10010010000
Remainder is 00000
Final data is: 1111011010100000
34 | P a g e
3. CheckSum
#include<stdio.h>
#include<string.h>
int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i,length;
printf("Enter first binary stringn");
scanf("%s",&a);
printf("Enter second binary stringn");
scanf("%s",&b);
if(strlen(a)==strlen(b)){
length = strlen(a);
35 | P a g e
char carry='0';
for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
36 | P a g e
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
37 | P a g e
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';
}
else
break;
}
38 | P a g e
printf("nSum=%c%s",carry,sum);
for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}
if(carry=='1')
carry='0';
else
carry='1';
printf("nChecksum=%c%s",carry,complement);
}
39 | P a g e
else {
printf("nWrong input strings");
}
}
OUTPUT
Enterfirst binary string
101101
Entersecond binary string
110010
Sum=1011111
Checksum=0100000
--------------------------------------
40 | P a g e
4.Hamming 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: ");
41 | P a g e
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("ncongratulationsthere is no error: ");
} else {
printf("nerron on the postion: %dnthe 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]); } }
42 | P a g e
getch();
}
Output:-
this works for message of 4bits in size
enter message bit one by one:0
1
0
1
the encoded bits are given below:-
0101101
enter the received data bits one by one:-0
1
0
1
1
0
1
congratulationsthere is no error:
43 | P a g e
Experiment:-6
Aim:- Write a program in C to implement routing
algorithm.
 Distance vector routing
 Link state routing
1. Distancevector routing
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
44 | P a g e
printf("nEnterthe number of nodes : ");
scanf("%d",&nodes);//Enterthe nodes
printf("nEnterthe cost matrix :n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialisethe distance
equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)
45 | P a g e
//We choosearbitary vertex k and we calculatethe direct
distance from the nodei to k using the cost matrix
//and add the distance from k to nodej
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculatethe minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("nn For router%dn",i+1);
for(j=0;j<nodes;j++)
{
46 | P a g e
printf("tnnode%d via %d Distance %d
",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("nn");
getch();
}
OUTPUT
A sample run of the program works as:-
Enter the number of nodes:
3
Enterthe cost matrix:
0 2 7
2 0 1
7 1 0
47 | P a g e
For router1
node 1 via 1 Distance 0
node 2 via 2 Distance 2
node 3 via 3 Distance 3
For router2
node 1 via 1 Distance 2
node 2 via 2 Distance 0
node 3 via 3 Distance 1
For router3
node 1 via 1 Distance 3
node 2 via 2 Distance 1
node 3 via 3 Distance 0
48 | P a g e
2. Link State Routing
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void lsr(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enterno. of vertices:");
scanf("%d",&n);
printf("nEnterthe adjacency matrix:n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
49 | P a g e
scanf("%d",&G[i][j]);
printf("nEnterthe starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void lsr(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[]stores the predecessorof each node
//countgives the number of nodes seen so far
//createthe cost matrix
50 | P a g e
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[]and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
51 | P a g e
while(count<n-1)
{
mindistance=INFINITY;
//nextnodegives the nodeat minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through
nextnode
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
52 | P a g e
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
//print the path and distance of each node
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("nDistance of
node%d=%d",i,distance[i]);
printf("nPath=%d",i);
53 | P a g e
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
OUTPUT:
Enterno. of vertices: 5
Enterthe adjency matrix:
10 0 30 100
10 0 50 0 0
0 50 0 20 10
38 0 23 0 63
54 | P a g e
Enterthe starting node: 0
Distance of node1 = 10
Path = 1<-0
Distance of node2 = 50
Path= 2<-3<-0
Distance of node3 = 30
Path = 3<-0
Distance of node4 =60
Path = 4<-2<-3<-0
Process returned 5(0x5) execution time : 47.471s
Press any key to continue.
55 | P a g e
Experiment:-7
Aim: Write two programs in C Hello_Clientand
Hello_Server.
 The server listens for and accepts a single TCP
connection. It reads all the data it can form that
connectionand prints it to screen then it close
the connection
 The client connectsto the server sends the
string “COMPUTER NETWORK” then closes
the connection
1.TCP SERVER:-
#include<netdb.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
56 | P a g e
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Function designed for chat between client and
server.
void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it
in buffer
read(sockfd, buff, sizeof(buff));
// print buffer which containsthe client
57 | P a g e
contents
printf("From client: %st To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != 'n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and
chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...n");
break;
}
}
58 | P a g e
}
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
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));
59 | P a g e
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=
htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP
and verification
if ((bind(sockfd, (SA*)&servaddr,
sizeof(servaddr))) != 0) {
printf("socket bind failed...n");
exit(0);
}
else
printf("Socket successfully binded..n");
// Now server is ready to listen and verification
60 | P a g e
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...n");
exit(0);
}
else
printf("Server listening..n");
len = sizeof(cli);
// Accept the data packet from client and
verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...n");
exit(0);
}
else
printf("server acccept the client...n");
// Function for chatting between client and server
func(connfd);
61 | P a g e
// After chattingclose the socket
close(sockfd);
}
Output
Server side:
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...
62 | P a g e
3. TCP CLIENT:-
#include<netdb.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
63 | P a g e
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;
64 | P a g e
// socket create and varification
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));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr))
!= 0) {
65 | P a g e
printf("connectionwith the server failed...n");
exit(0);
}
else
printf("connected to the server..n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}
66 | P a g e
Output
Client side:
Socket successfully created..
connectedto the server..
Enterthe string : hi
From Server : hello
Enterthe string : exit
From Server : exit
Client Exit...

More Related Content

What's hot

Semi Structured Data
Semi Structured DataSemi Structured Data
Semi Structured Data
MariaDB plc
 
KIT-601 Lecture Notes-UNIT-2.pdf
KIT-601 Lecture Notes-UNIT-2.pdfKIT-601 Lecture Notes-UNIT-2.pdf
KIT-601 Lecture Notes-UNIT-2.pdf
Dr. Radhey Shyam
 
IM02: Database Language
IM02: Database LanguageIM02: Database Language
IM02: Database Language
Kelvin Chan
 
Blackboard architecture pattern
Blackboard architecture patternBlackboard architecture pattern
Blackboard architecture pattern
aish006
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
LamineKaba6
 
Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)
Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)
Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)
AYESHA JAVED
 
Ch11-Data Storage Design
Ch11-Data Storage DesignCh11-Data Storage Design
Ch11-Data Storage Design
Fajar Baskoro
 
Relational database
Relational database Relational database
Relational database
Megha Sharma
 
Erp implementation cost analysis
Erp implementation cost analysisErp implementation cost analysis
Erp implementation cost analysis
Celine George
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
Dbms slides
Dbms slidesDbms slides
Dbms slides
rahulrathore725
 
Files Vs DataBase
Files Vs DataBaseFiles Vs DataBase
Files Vs DataBase
Dr. C.V. Suresh Babu
 
Web design - Applications and web application definition
Web design - Applications and web application definitionWeb design - Applications and web application definition
Web design - Applications and web application definition
Mustafa Kamel Mohammadi
 
Database and types of database
Database and types of databaseDatabase and types of database
Database and types of database
baabtra.com - No. 1 supplier of quality freshers
 
Difference between File system And DBMS.pptx
Difference between File system And DBMS.pptxDifference between File system And DBMS.pptx
Difference between File system And DBMS.pptx
ShayanMujahid2
 
Access 2010 Unit A PPT
Access 2010 Unit A PPTAccess 2010 Unit A PPT
Access 2010 Unit A PPT
okmomwalking
 
A Data Curation Framework: Data Curation and Research Support Services
A Data Curation Framework: Data Curation and Research Support ServicesA Data Curation Framework: Data Curation and Research Support Services
A Data Curation Framework: Data Curation and Research Support Services
SusanMRob
 
Enterprise application
Enterprise applicationEnterprise application
Enterprise application
ancientsoul90
 
Enterprise and Enterprise Application
Enterprise and Enterprise ApplicationEnterprise and Enterprise Application
Enterprise and Enterprise Application
Rajandeep Gill
 
Enterprise application integration
Enterprise application integrationEnterprise application integration
Enterprise application integration
Goa App
 

What's hot (20)

Semi Structured Data
Semi Structured DataSemi Structured Data
Semi Structured Data
 
KIT-601 Lecture Notes-UNIT-2.pdf
KIT-601 Lecture Notes-UNIT-2.pdfKIT-601 Lecture Notes-UNIT-2.pdf
KIT-601 Lecture Notes-UNIT-2.pdf
 
IM02: Database Language
IM02: Database LanguageIM02: Database Language
IM02: Database Language
 
Blackboard architecture pattern
Blackboard architecture patternBlackboard architecture pattern
Blackboard architecture pattern
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)
Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)
Exercise solution of chapter3 of datawarehouse cs614(solution of exercise)
 
Ch11-Data Storage Design
Ch11-Data Storage DesignCh11-Data Storage Design
Ch11-Data Storage Design
 
Relational database
Relational database Relational database
Relational database
 
Erp implementation cost analysis
Erp implementation cost analysisErp implementation cost analysis
Erp implementation cost analysis
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Dbms slides
Dbms slidesDbms slides
Dbms slides
 
Files Vs DataBase
Files Vs DataBaseFiles Vs DataBase
Files Vs DataBase
 
Web design - Applications and web application definition
Web design - Applications and web application definitionWeb design - Applications and web application definition
Web design - Applications and web application definition
 
Database and types of database
Database and types of databaseDatabase and types of database
Database and types of database
 
Difference between File system And DBMS.pptx
Difference between File system And DBMS.pptxDifference between File system And DBMS.pptx
Difference between File system And DBMS.pptx
 
Access 2010 Unit A PPT
Access 2010 Unit A PPTAccess 2010 Unit A PPT
Access 2010 Unit A PPT
 
A Data Curation Framework: Data Curation and Research Support Services
A Data Curation Framework: Data Curation and Research Support ServicesA Data Curation Framework: Data Curation and Research Support Services
A Data Curation Framework: Data Curation and Research Support Services
 
Enterprise application
Enterprise applicationEnterprise application
Enterprise application
 
Enterprise and Enterprise Application
Enterprise and Enterprise ApplicationEnterprise and Enterprise Application
Enterprise and Enterprise Application
 
Enterprise application integration
Enterprise application integrationEnterprise application integration
Enterprise application integration
 

Similar to Data Communication and Networking(DCACN)

Networking Fundamentals
Networking FundamentalsNetworking Fundamentals
Networking Fundamentals
Rajet Veshin
 
05 - Networking Components and Devices.ppt
05 - Networking Components and Devices.ppt05 - Networking Components and Devices.ppt
05 - Networking Components and Devices.ppt
ssuserf7cd2b
 
Ccna ccnp interview question 1
Ccna ccnp interview question 1Ccna ccnp interview question 1
Ccna ccnp interview question 1
Salim Khan
 
Siemens Industrial Training
Siemens Industrial TrainingSiemens Industrial Training
Siemens Industrial Training
Srinidhi Bheesette
 
Understanding Networking Devices – A Brief Introduction | TechLinu.com
Understanding Networking Devices – A Brief Introduction | TechLinu.comUnderstanding Networking Devices – A Brief Introduction | TechLinu.com
Understanding Networking Devices – A Brief Introduction | TechLinu.com
TechLinu.com
 
Networking devices(siddique)
Networking devices(siddique)Networking devices(siddique)
Networking devices(siddique)
Siddique Ibrahim
 
Data communication notes
Data communication notesData communication notes
Data communication notes
Md. Faruk Hossain
 
Computer networking
Computer networkingComputer networking
Computer networking
poonamchopra7975
 
Cna
CnaCna
Computer networks
Computer networksComputer networks
Computer networks
Sayma Sultana
 
Introduction To Computer Networks
Introduction To Computer Networks Introduction To Computer Networks
Introduction To Computer Networks
AfsanaAkterRubi
 
Network layer u3
Network  layer u3Network  layer u3
Network layer u3
IndrajaMeghavathula
 
Networking devices
Networking devicesNetworking devices
Networking devices
rehnuma rusha
 
Advanced computer network lab manual (practicals in Cisco Packet tracer)
Advanced computer network lab manual (practicals in Cisco Packet tracer)Advanced computer network lab manual (practicals in Cisco Packet tracer)
Advanced computer network lab manual (practicals in Cisco Packet tracer)
VrundaBhavsar
 
Introduction to Computer Networks Lecture slides ppt
Introduction to Computer Networks Lecture slides pptIntroduction to Computer Networks Lecture slides ppt
Introduction to Computer Networks Lecture slides ppt
Osama Yousaf
 
What is networking
What is networkingWhat is networking
What is networking
babyparul
 
Class Note
Class NoteClass Note
Class Note
AK IJ
 
Network the 4th layer
Network the 4th layerNetwork the 4th layer
Network the 4th layer
kachbourimed
 
networking1.ppt
networking1.pptnetworking1.ppt
networking1.ppt
ChinmayWaingankar3
 
Computer Networks basics Unit 1 ppt.pptx
Computer Networks basics Unit 1 ppt.pptxComputer Networks basics Unit 1 ppt.pptx
Computer Networks basics Unit 1 ppt.pptx
sakshamsharma7257
 

Similar to Data Communication and Networking(DCACN) (20)

Networking Fundamentals
Networking FundamentalsNetworking Fundamentals
Networking Fundamentals
 
05 - Networking Components and Devices.ppt
05 - Networking Components and Devices.ppt05 - Networking Components and Devices.ppt
05 - Networking Components and Devices.ppt
 
Ccna ccnp interview question 1
Ccna ccnp interview question 1Ccna ccnp interview question 1
Ccna ccnp interview question 1
 
Siemens Industrial Training
Siemens Industrial TrainingSiemens Industrial Training
Siemens Industrial Training
 
Understanding Networking Devices – A Brief Introduction | TechLinu.com
Understanding Networking Devices – A Brief Introduction | TechLinu.comUnderstanding Networking Devices – A Brief Introduction | TechLinu.com
Understanding Networking Devices – A Brief Introduction | TechLinu.com
 
Networking devices(siddique)
Networking devices(siddique)Networking devices(siddique)
Networking devices(siddique)
 
Data communication notes
Data communication notesData communication notes
Data communication notes
 
Computer networking
Computer networkingComputer networking
Computer networking
 
Cna
CnaCna
Cna
 
Computer networks
Computer networksComputer networks
Computer networks
 
Introduction To Computer Networks
Introduction To Computer Networks Introduction To Computer Networks
Introduction To Computer Networks
 
Network layer u3
Network  layer u3Network  layer u3
Network layer u3
 
Networking devices
Networking devicesNetworking devices
Networking devices
 
Advanced computer network lab manual (practicals in Cisco Packet tracer)
Advanced computer network lab manual (practicals in Cisco Packet tracer)Advanced computer network lab manual (practicals in Cisco Packet tracer)
Advanced computer network lab manual (practicals in Cisco Packet tracer)
 
Introduction to Computer Networks Lecture slides ppt
Introduction to Computer Networks Lecture slides pptIntroduction to Computer Networks Lecture slides ppt
Introduction to Computer Networks Lecture slides ppt
 
What is networking
What is networkingWhat is networking
What is networking
 
Class Note
Class NoteClass Note
Class Note
 
Network the 4th layer
Network the 4th layerNetwork the 4th layer
Network the 4th layer
 
networking1.ppt
networking1.pptnetworking1.ppt
networking1.ppt
 
Computer Networks basics Unit 1 ppt.pptx
Computer Networks basics Unit 1 ppt.pptxComputer Networks basics Unit 1 ppt.pptx
Computer Networks basics Unit 1 ppt.pptx
 

More from Uttam Singh Chaudhary

Big Data Analytics Lab File
Big Data Analytics Lab FileBig Data Analytics Lab File
Big Data Analytics Lab File
Uttam Singh Chaudhary
 
5 Pen PC Technology
5 Pen PC Technology5 Pen PC Technology
5 Pen PC Technology
Uttam Singh Chaudhary
 
DataBase Management System Lab File
DataBase Management System Lab FileDataBase Management System Lab File
DataBase Management System Lab File
Uttam Singh Chaudhary
 
Traffic woes in India
Traffic woes in IndiaTraffic woes in India
Traffic woes in India
Uttam Singh Chaudhary
 
Software Requirement Specification Of Hotel Management System
Software Requirement Specification Of Hotel Management SystemSoftware Requirement Specification Of Hotel Management System
Software Requirement Specification Of Hotel Management System
Uttam Singh Chaudhary
 
Network Topologies
Network TopologiesNetwork Topologies
Network Topologies
Uttam Singh Chaudhary
 

More from Uttam Singh Chaudhary (6)

Big Data Analytics Lab File
Big Data Analytics Lab FileBig Data Analytics Lab File
Big Data Analytics Lab File
 
5 Pen PC Technology
5 Pen PC Technology5 Pen PC Technology
5 Pen PC Technology
 
DataBase Management System Lab File
DataBase Management System Lab FileDataBase Management System Lab File
DataBase Management System Lab File
 
Traffic woes in India
Traffic woes in IndiaTraffic woes in India
Traffic woes in India
 
Software Requirement Specification Of Hotel Management System
Software Requirement Specification Of Hotel Management SystemSoftware Requirement Specification Of Hotel Management System
Software Requirement Specification Of Hotel Management System
 
Network Topologies
Network TopologiesNetwork Topologies
Network Topologies
 

Recently uploaded

🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
dulbh kashyap
 
Cuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort Service
Cuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort ServiceCuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort Service
Cuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort Service
yakranividhrini
 
Cricket management system ptoject report.pdf
Cricket management system ptoject report.pdfCricket management system ptoject report.pdf
Cricket management system ptoject report.pdf
Kamal Acharya
 
My Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdfMy Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdf
Geoffrey Wardle. MSc. MSc. Snr.MAIAA
 
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Banerescorts
 
Call Girls Goa (india) ☎️ +91-7426014248 Goa Call Girl
Call Girls Goa (india) ☎️ +91-7426014248 Goa Call GirlCall Girls Goa (india) ☎️ +91-7426014248 Goa Call Girl
Call Girls Goa (india) ☎️ +91-7426014248 Goa Call Girl
sapna sharmap11
 
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptxMODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
NaveenNaveen726446
 
SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )
Tsuyoshi Horigome
 
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
IJCNCJournal
 
Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine
 
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Tsuyoshi Horigome
 
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
nainakaoornoida
 
Intuit CRAFT demonstration presentation for sde
Intuit CRAFT demonstration presentation for sdeIntuit CRAFT demonstration presentation for sde
Intuit CRAFT demonstration presentation for sde
ShivangMishra54
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
gapboxn
 
🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...
🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...
🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...
sonamrawat5631
 
Call Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl Lucknow
Call Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl LucknowCall Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl Lucknow
Call Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl Lucknow
yogita singh$A17
 
CSP_Study - Notes (Paul McNeill) 2017.pdf
CSP_Study - Notes (Paul McNeill) 2017.pdfCSP_Study - Notes (Paul McNeill) 2017.pdf
CSP_Study - Notes (Paul McNeill) 2017.pdf
Ismail Sultan
 
paper relate Chozhavendhan et al. 2020.pdf
paper relate Chozhavendhan et al. 2020.pdfpaper relate Chozhavendhan et al. 2020.pdf
paper relate Chozhavendhan et al. 2020.pdf
ShurooqTaib
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
Lubi Valves
 
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
DharmaBanothu
 

Recently uploaded (20)

🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
🚺ANJALI MEHTA High Profile Call Girls Ahmedabad 💯Call Us 🔝 9352988975 🔝💃Top C...
 
Cuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort Service
Cuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort ServiceCuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort Service
Cuttack Call Girls 💯Call Us 🔝 7374876321 🔝 💃 Independent Female Escort Service
 
Cricket management system ptoject report.pdf
Cricket management system ptoject report.pdfCricket management system ptoject report.pdf
Cricket management system ptoject report.pdf
 
My Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdfMy Airframe Metallic Design Capability Studies..pdf
My Airframe Metallic Design Capability Studies..pdf
 
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
Hot Call Girls In Bangalore ✔ 9079923931 ✔ Hi I Am Divya Vip Call Girl Servic...
 
Call Girls Goa (india) ☎️ +91-7426014248 Goa Call Girl
Call Girls Goa (india) ☎️ +91-7426014248 Goa Call GirlCall Girls Goa (india) ☎️ +91-7426014248 Goa Call Girl
Call Girls Goa (india) ☎️ +91-7426014248 Goa Call Girl
 
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptxMODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
MODULE 5 BIOLOGY FOR ENGINEERS TRENDS IN BIO ENGINEERING.pptx
 
SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )SPICE PARK JUL2024 ( 6,866 SPICE Models )
SPICE PARK JUL2024 ( 6,866 SPICE Models )
 
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
 
Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024Better Builder Magazine, Issue 49 / Spring 2024
Better Builder Magazine, Issue 49 / Spring 2024
 
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
Update 40 models( Solar Cell ) in SPICE PARK(JUL2024)
 
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
❣Independent Call Girls Chennai 💯Call Us 🔝 7737669865 🔝💃Independent Chennai E...
 
Intuit CRAFT demonstration presentation for sde
Intuit CRAFT demonstration presentation for sdeIntuit CRAFT demonstration presentation for sde
Intuit CRAFT demonstration presentation for sde
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...
🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...
🔥Young College Call Girls Chandigarh 💯Call Us 🔝 7737669865 🔝💃Independent Chan...
 
Call Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl Lucknow
Call Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl LucknowCall Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl Lucknow
Call Girls In Lucknow 🔥 +91-7014168258🔥High Profile Call Girl Lucknow
 
CSP_Study - Notes (Paul McNeill) 2017.pdf
CSP_Study - Notes (Paul McNeill) 2017.pdfCSP_Study - Notes (Paul McNeill) 2017.pdf
CSP_Study - Notes (Paul McNeill) 2017.pdf
 
paper relate Chozhavendhan et al. 2020.pdf
paper relate Chozhavendhan et al. 2020.pdfpaper relate Chozhavendhan et al. 2020.pdf
paper relate Chozhavendhan et al. 2020.pdf
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
 
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
An In-Depth Exploration of Natural Language Processing: Evolution, Applicatio...
 

Data Communication and Networking(DCACN)

  • 1. 1 | P a g e Submitted To:- Submitted By:- Sir Rohit Negi Uttam Singh Chaudhary (17EMBIT055)
  • 2. 2 | P a g e List of Experiments S.no Experiment Name Page no 1 Define different types of LAN. (LAN, MAN, WAN) 4-6 2 Define and describe all the network Equipment/Devices. 7-12 3 Define and describe different types of network topology Star, Ring, Bus and Mesh. 13-17 4 Write a program in c to implement various type of framing method.  Bit stuffing  Byte stuffing  Character Stuffing 18-27 5 Write a program in C to implement Various routing algorithm.  Simple redundancy check  Cyclic redundancy check  Checksum  Hamming code 28-42
  • 3. 3 | P a g e 6 Write a program in C to implement routing algorithm.  Distance vector routing  Link state routing 43-54 7 Write two program in C Hello_Client and Hello_Server.  The server listens for and accepts a single TCP connection. It reads all the data it can from that connection and print it to screen then it close the connection.  The client connects to the server sends the string ”COMPUTER NETWORK” then closes the connection. 55-66
  • 4. 4 | P a g e Experiment:-1 Aim: Define different types of LAN. (LAN, MAN, WAN) LAN: Local Area Network A LAN connects network devices over a relatively short distance. A networked office building, school, or home usually contains a single LAN, though sometimes one building will contain a few small LANs (perhaps one per room), and occasionally a LAN will span a group of nearby buildings. In TCP/IP networking, a LAN is often but not always implemented as a single IP subnet. LANs are also typically owned, controlled, and managed by a single person or organization. They also tend to use certain connectivity technologies, primarily Ethernet and Token Ring.
  • 5. 5 | P a g e WAN: Wide Area Network As the term implies, a WAN spans a large physical distance. The Internet is the largest WAN, spanning the Earth. A WAN is a geographically-dispersed collection of LANs. A network device called a router connects LANs to a WAN. In IP networking, the router maintains both a LAN address and a WAN address. A WAN differs from a LAN in several important ways. Most WANs (like the Internet) are not owned by any one organization but rather exist under collective or distributed ownership and management. WANs tend to use technology like ATM, Frame Relay and X.25 for connectivity over the longer distances.
  • 6. 6 | P a g e MAN: Metropolitan Area Network A large computer network which extends to a city or to a large university campus is termed as metropolitan area network or MAN. The purpose of MAN (Metropolitan Area Network) is to provide the link to the internet in the long run. A MAN (Metropolitan Area Network) usually incorporates a number of LANs to form a network. This large network MANs (Metropolitan Area Network) backbone comprises of an optical fiber set-up.
  • 7. 7 | P a g e Experiment:-2 Aim:- Define and also describe different types of network Equipment /devices. Different networking devices: Network Hub: Network Hub is a networking device which is used to connect multiple network hosts. A network hub is also used to do data transfer. The data is transferred in terms of packets on a computer network. So when a host sends a data packet to a network hub, the hub copies the data packet to all of its ports connected to. Like this, all the ports know about the data and the port for whom the packet is intended, claims the packet.
  • 8. 8 | P a g e However, because of its working mechanism, a hub is not so secure and safe. Moreover, copying the data packets on all the interfaces or ports makes it slower and more congested which led to the use of network switch. Network Switch: Like a hub, a switch also works at the layer of LAN (Local Area Network) but you can say that a switch is more intelligent than a hub. While hub just does the work of data forwarding, a switch does 'filter and forwarding' which is a more intelligent way of dealing with the data packets. So, when a packet is received at one of the interfaces of the switch, it filters the packet and sends only to the interface of the intended receiver.
  • 9. 9 | P a g e For this purpose, a switch also maintains a CAM (Content Addressable Memory) table and has its own system configuration and memory. CAM table is also called as forwarding table or forwarding information base (FIB). Modem: A Modem is somewhat a more interesting network device in our daily life. So if you have noticed around, you get an internet connection through a wire (there are different types of wires) to your house. This wire is used to carry our internet data outside to the internet world. However, our computer generates binary data or digital data in forms of 1s and 0s and on the other hand, a wire carries an analog signal and that's where a modem comes in.
  • 10. 10 | P a g e A modem stands for (Modulator + Demodulator). That means it modulates and demodulates the signal between the digital data of a computer and the analog signal of a telephone line. Network Router: A router is a network device which is responsible for routing traffic from one to another network. These two networks could be a private company network to a public network. You can think of a router as a traffic police who directs different network traffic to different directions.
  • 11. 11 | P a g e Bridge: If a router connects two different types of networks, then a bridge connects two sub networks as a part of the same network. You can think of two different labs or two different floors connected by a bridge.
  • 12. 12 | P a g e Repeater: A repeateris an electronic device that amplifies the signal it receives. In other terms, you can think of repeater as a device which receives a signal and retransmits it at a higher level or higher power so that the signal can cover longer distances .
  • 13. 13 | P a g e Experiment:-3 Aim: Define and describe different types of network topology Star, Ring, Bus and Mesh STAR TOPOLOGY: A star topology is a topology where every node in the network is connected to one central node. Every device in the network is directly connected to the central node and indirectly connected to every other node.
  • 14. 14 | P a g e The relationship between these elements is that the central network device is a server and other devices are treated as clients. The central node has the responsibility of managing data transmissions across the network. The central node or hub also acts as a repeater. In star topologies, computersare connected with a coaxial cable, twisted pair, or optical fiber cable. RING TOPOLOGY: In networks with a ring topology, computers are connected to each other in a circular format. Every device in the network will have two neighbors and no more or no less. The first node is connected to the last node to link the loop together. Ring topologies are unidirectional but can also be made bidirectional.
  • 15. 15 | P a g e Ring topologies are unidirectional but can also be made bidirectional. To make ring topologies bidirectional you would need to have two connections between network nodes to form a Dual Ring Topology. Ring topologies can sustain large networks much more effectively than bus topologies.
  • 16. 16 | P a g e BUS TOPOLOGY:- Bus topology is a type of network where every device is connected to a single cable which runs from one end of the network to the other. This type of type of topology is often referred to as line topology. In a bus topology, data is transmitted in one direction only. If the bus topology has two endpoints then it is referred to as a linear bus topology. Organizations using this type of topology will generally use an RJ45 cable to link devices together.
  • 17. 17 | P a g e MESH TOPOLOGY:- A mesh topology is a point-to-point connectionwhere nodes are interconnected. In this form of topology, data is transmitted via two methods: routing and flooding. Routing is where nodes use routing logic to work out the shortest distance to the packet’s destination. In contrast flooding, data is sent to all nodes within the network. Floodingdoesn’t require any form of routing logic to work. The interconnectivity of nodesmakes them extremely resistant to failures.
  • 18. 18 | P a g e Experiment:-4 1.Bit Stuffing #include<stdio.h> #include<conio.h> #include<string.h> void main() { int a[20],b[30],i,j,k,count,n; printf("Enter frame size (Example: 8):"); scanf("%d",&n); Aim: Write a program in c to implement various type of framing method.  Bit stuffing  Byte stuffing  Character Stuffing
  • 19. 19 | P a g e printf("Enterthe frame in the form of 0 and 1 :"); for(i=0; i<n; i++) scanf("%d",&a[i]); i=0; count=1; j=0; while(i<n) { if(a[i]==1) { b[j]=a[i]; for(k=i+1; a[k]==1 && k<n && count<5;k++) { j++; b[j]=a[k]; count++; if(count==5) {
  • 20. 20 | P a g e j++; b[j]=0; } i=k; } } else { b[j]=a[i]; } i++; j++; } printf("After Bit Stuffing :"); for(i=0; i<j; i++) printf("%d",b[i]); getch(); }
  • 21. 21 | P a g e OUTPUT: Enterframe size (Example: 8):12 Enterthe frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0 0 1 After Bit Stuffing :0101111101001 2.Byte Stuffing #include<stdio.h> #include<string.h> void main(){ char frame[50][50],str[50][50]; char flag[10]; strcpy(flag,"flag"); char esc[10]; strcpy(esc,"esc"); int i,j,k=0,n; strcpy(frame[k++],"flag");
  • 22. 22 | P a g e printf("Enterno.ofString :t"); scanf("%d",&n); printf("EnterString n"); for(i=0;i<=n;i++) { gets(str[i]); } printf("You entered :n"); for(i=0;i<=n;i++) { puts(str[i]); } printf("n"); for(i=1;i<=n;i++) { if(strcmp(str[i],flag)!=0 && strcmp(str[i],esc)!=0) { strcpy(frame[k++],str[i]); } else { strcpy(frame[k++],"esc"); strcpy(frame[k++],str[i]); }
  • 23. 23 | P a g e } strcpy(frame[k++],"flag"); //frame[k++]='0'; printf("------------------------------n"); printf("Byte stuffing at sender side:nn"); printf("------------------------------n"); for(i=0;i<k;i++) { printf("%st",frame[i]); } } Output:- Enter no.of String : 2 Enter String Apple cake You entered : Apple cake - -------------------------- Byte stuffing at sender side: ------------------------------ flag Apple cake flag
  • 24. 24 | P a g e 3.Character Stuffing #include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> void main() { int i=0,j=0,n,pos; char a[20],b[50],ch; clrscr(); printf("enter stringn"); scanf("%s",&a); n=strlen(a); printf("enter positionn"); scanf("%d",&pos); if(pos>n) {
  • 25. 25 | P a g e printf("invalid position, Enter again :"); scanf("%d",&pos); } printf("enter the charactern"); ch=getche(); b[0]='d'; b[1]='l'; b[2]='e'; b[3]='s'; b[4]='t'; b[5]='x'; j=6; while(i<n) { if(i==pos-1) { b[j]='d';
  • 26. 26 | P a g e b[j+1]='l'; b[j+2]='e'; b[j+3]=ch; b[j+4]='d'; b[j+5]='l'; b[j+6]='e'; j=j+7; } if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e') { b[j]='d'; b[j+1]='l'; b[j+2]='e'; j=j+3; } b[j]=a[i]; i++; j++; }
  • 27. 27 | P a g e b[j]='d'; b[j+1]='l'; b[j+2]='e'; b[j+3]='e'; b[j+4]='t'; b[j+5]='x'; b[j+6]='0'; printf("nframe after stuffing:n"); printf("%s",b); getch(); } INPUT:- enter string:- asdlefgh enter position: 8 invalid position, enter again: 3 enter the character: k OUTPUT:- frame after stuffing:- dlestx as dle k dle dle dlefgh dleetx
  • 28. 28 | P a g e Experiment:-5 Aim:- Write a program in C to implement Various routing algorithm.  Simple redundancy check  Cyclic redundancy check  Checksum  Hamming code 1.Simple Parity Check: # include<bits/stdc++.h> # define bool int using namespace std; // Function to get parity of number n. It returns1 // if n has odd parity, and returns 0 if n has even // parity bool getParity(unsigned int n) { bool parity = 0; while (n)
  • 29. 29 | P a g e { parity = !parity; n = n & (n - 1); } return parity; } /* Driver program to test getParity() */ int main() { unsigned int n = 7; cout<<"Parity of no "<<n<<" = "<<(getParity(n)? "odd": "even"); getchar(); return 0; } OUTPUT Even Parity Odd Parity
  • 30. 30 | P a g e 2.Cyclic RedundancyCheck #include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,keylen,msglen; char input[100], key[30],temp[30],quot[100],rem[30],key1[30]; clrscr(); printf("EnterData: "); gets(input); printf("EnterKey: "); gets(key); keylen=strlen(key); msglen=strlen(input); strcpy(key1,key);
  • 31. 31 | P a g e for(i=0;i<keylen-1;i++) { input[msglen+i]='0'; } for(i=0;i<keylen;i++) temp[i]=input[i]; for(i=0;i<msglen;i++) { quot[i]=temp[0]; if(quot[i]=='0') for(j=0;j<keylen;j++) key[j]='0'; else for(j=0;j<keylen;j++) key[j]=key1[j]; for(j=keylen-1;j>0;j--) { if(temp[j]==key[j])
  • 32. 32 | P a g e rem[j-1]='0'; else rem[j-1]='1'; } rem[keylen-1]=input[i+keylen]; strcpy(temp,rem); } strcpy(rem,temp); printf("nQuotient is "); for(i=0;i<msglen;i++) printf("%c",quot[i]); printf("nRemainder is "); for(i=0;i<keylen-1;i++) printf("%c",rem[i]); printf("nFinal data is: "); for(i=0;i<msglen;i++) printf("%c",input[i]); for(i=0;i<keylen-1;i++)
  • 33. 33 | P a g e printf("%c",rem[i]); getch(); } Output: EnterData: 11110110101 EnterKey: 111010 Quotient is 10010010000 Remainder is 00000 Final data is: 1111011010100000
  • 34. 34 | P a g e 3. CheckSum #include<stdio.h> #include<string.h> int main() { char a[20],b[20]; char sum[20],complement[20]; int i,length; printf("Enter first binary stringn"); scanf("%s",&a); printf("Enter second binary stringn"); scanf("%s",&b); if(strlen(a)==strlen(b)){ length = strlen(a);
  • 35. 35 | P a g e char carry='0'; for(i=length-1;i>=0;i--) { if(a[i]=='0' && b[i]=='0' && carry=='0') { sum[i]='0'; carry='0'; } else if(a[i]=='0' && b[i]=='0' && carry=='1') { sum[i]='1'; carry='0'; } else if(a[i]=='0' && b[i]=='1' && carry=='0') { sum[i]='1';
  • 36. 36 | P a g e carry='0'; } else if(a[i]=='0' && b[i]=='1' && carry=='1') { sum[i]='0'; carry='1'; } else if(a[i]=='1' && b[i]=='0' && carry=='0') { sum[i]='1'; carry='0'; } else if(a[i]=='1' && b[i]=='0' && carry=='1') { sum[i]='0';
  • 37. 37 | P a g e carry='1'; } else if(a[i]=='1' && b[i]=='1' && carry=='0') { sum[i]='0'; carry='1'; } else if(a[i]=='1' && b[i]=='1' && carry=='1') { sum[i]='1'; carry='1'; } else break; }
  • 38. 38 | P a g e printf("nSum=%c%s",carry,sum); for(i=0;i<length;i++) { if(sum[i]=='0') complement[i]='1'; else complement[i]='0'; } if(carry=='1') carry='0'; else carry='1'; printf("nChecksum=%c%s",carry,complement); }
  • 39. 39 | P a g e else { printf("nWrong input strings"); } } OUTPUT Enterfirst binary string 101101 Entersecond binary string 110010 Sum=1011111 Checksum=0100000 --------------------------------------
  • 40. 40 | P a g e 4.Hamming 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: ");
  • 41. 41 | P a g e 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("ncongratulationsthere is no error: "); } else { printf("nerron on the postion: %dnthe 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]); } }
  • 42. 42 | P a g e getch(); } Output:- this works for message of 4bits in size enter message bit one by one:0 1 0 1 the encoded bits are given below:- 0101101 enter the received data bits one by one:-0 1 0 1 1 0 1 congratulationsthere is no error:
  • 43. 43 | P a g e Experiment:-6 Aim:- Write a program in C to implement routing algorithm.  Distance vector routing  Link state routing 1. Distancevector routing #include<stdio.h> struct node { unsigned dist[20]; unsigned from[20]; }rt[10]; int main() { int costmat[20][20]; int nodes,i,j,k,count=0;
  • 44. 44 | P a g e printf("nEnterthe number of nodes : "); scanf("%d",&nodes);//Enterthe nodes printf("nEnterthe cost matrix :n"); for(i=0;i<nodes;i++) { for(j=0;j<nodes;j++) { scanf("%d",&costmat[i][j]); costmat[i][i]=0; rt[i].dist[j]=costmat[i][j];//initialisethe distance equal to cost matrix rt[i].from[j]=j; } } do { count=0; for(i=0;i<nodes;i++)
  • 45. 45 | P a g e //We choosearbitary vertex k and we calculatethe direct distance from the nodei to k using the cost matrix //and add the distance from k to nodej for(j=0;j<nodes;j++) for(k=0;k<nodes;k++) if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j]) {//We calculatethe minimum distance rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]; rt[i].from[j]=k; count++; } }while(count!=0); for(i=0;i<nodes;i++) { printf("nn For router%dn",i+1); for(j=0;j<nodes;j++) {
  • 46. 46 | P a g e printf("tnnode%d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]); } } printf("nn"); getch(); } OUTPUT A sample run of the program works as:- Enter the number of nodes: 3 Enterthe cost matrix: 0 2 7 2 0 1 7 1 0
  • 47. 47 | P a g e For router1 node 1 via 1 Distance 0 node 2 via 2 Distance 2 node 3 via 3 Distance 3 For router2 node 1 via 1 Distance 2 node 2 via 2 Distance 0 node 3 via 3 Distance 1 For router3 node 1 via 1 Distance 3 node 2 via 2 Distance 1 node 3 via 3 Distance 0
  • 48. 48 | P a g e 2. Link State Routing #include<stdio.h> #include<conio.h> #define INFINITY 9999 #define MAX 10 void lsr(int G[MAX][MAX],int n,int startnode); int main() { int G[MAX][MAX],i,j,n,u; printf("Enterno. of vertices:"); scanf("%d",&n); printf("nEnterthe adjacency matrix:n"); for(i=0;i<n;i++) for(j=0;j<n;j++)
  • 49. 49 | P a g e scanf("%d",&G[i][j]); printf("nEnterthe starting node:"); scanf("%d",&u); dijkstra(G,n,u); return 0; } void lsr(int G[MAX][MAX],int n,int startnode) { int cost[MAX][MAX],distance[MAX],pred[MAX]; int visited[MAX],count,mindistance,nextnode,i,j; //pred[]stores the predecessorof each node //countgives the number of nodes seen so far //createthe cost matrix
  • 50. 50 | P a g e for(i=0;i<n;i++) for(j=0;j<n;j++) if(G[i][j]==0) cost[i][j]=INFINITY; else cost[i][j]=G[i][j]; //initialize pred[],distance[]and visited[] for(i=0;i<n;i++) { distance[i]=cost[startnode][i]; pred[i]=startnode; visited[i]=0; } distance[startnode]=0; visited[startnode]=1; count=1;
  • 51. 51 | P a g e while(count<n-1) { mindistance=INFINITY; //nextnodegives the nodeat minimum distance for(i=0;i<n;i++) if(distance[i]<mindistance&&!visited[i]) { mindistance=distance[i]; nextnode=i; } //check if a better path exists through nextnode visited[nextnode]=1; for(i=0;i<n;i++) if(!visited[i])
  • 52. 52 | P a g e if(mindistance+cost[nextnode][i]<distance[i]) { distance[i]=mindistance+cost[nextnode][i]; pred[i]=nextnode; } count++; } //print the path and distance of each node for(i=0;i<n;i++) if(i!=startnode) { printf("nDistance of node%d=%d",i,distance[i]); printf("nPath=%d",i);
  • 53. 53 | P a g e j=i; do { j=pred[j]; printf("<-%d",j); }while(j!=startnode); } } OUTPUT: Enterno. of vertices: 5 Enterthe adjency matrix: 10 0 30 100 10 0 50 0 0 0 50 0 20 10 38 0 23 0 63
  • 54. 54 | P a g e Enterthe starting node: 0 Distance of node1 = 10 Path = 1<-0 Distance of node2 = 50 Path= 2<-3<-0 Distance of node3 = 30 Path = 3<-0 Distance of node4 =60 Path = 4<-2<-3<-0 Process returned 5(0x5) execution time : 47.471s Press any key to continue.
  • 55. 55 | P a g e Experiment:-7 Aim: Write two programs in C Hello_Clientand Hello_Server.  The server listens for and accepts a single TCP connection. It reads all the data it can form that connectionand prints it to screen then it close the connection  The client connectsto the server sends the string “COMPUTER NETWORK” then closes the connection 1.TCP SERVER:- #include<netdb.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<sys/types.h>
  • 56. 56 | P a g e #define MAX 80 #define PORT 8080 #define SA struct sockaddr // Function designed for chat between client and server. void func(int sockfd) { char buff[MAX]; int n; // infinite loop for chat for (;;) { bzero(buff, MAX); // read the message from client and copy it in buffer read(sockfd, buff, sizeof(buff)); // print buffer which containsthe client
  • 57. 57 | P a g e contents printf("From client: %st To client : ", buff); bzero(buff, MAX); n = 0; // copy server message in the buffer while ((buff[n++] = getchar()) != 'n') ; // and send that buffer to client write(sockfd, buff, sizeof(buff)); // if msg contains "Exit" then server exit and chat ended. if (strncmp("exit", buff, 4) == 0) { printf("Server Exit...n"); break; } }
  • 58. 58 | P a g e } // Driver function int main() { int sockfd, connfd, len; struct sockaddr_in servaddr, cli; // socket create and verification 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));
  • 59. 59 | P a g e // assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr= htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); // Binding newly created socket to given IP and verification if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { printf("socket bind failed...n"); exit(0); } else printf("Socket successfully binded..n"); // Now server is ready to listen and verification
  • 60. 60 | P a g e if ((listen(sockfd, 5)) != 0) { printf("Listen failed...n"); exit(0); } else printf("Server listening..n"); len = sizeof(cli); // Accept the data packet from client and verification connfd = accept(sockfd, (SA*)&cli, &len); if (connfd < 0) { printf("server acccept failed...n"); exit(0); } else printf("server acccept the client...n"); // Function for chatting between client and server func(connfd);
  • 61. 61 | P a g e // After chattingclose the socket close(sockfd); } Output Server side: Socket successfully created.. Socket successfully binded.. Server listening.. server acccept the client... From client: hi To client : hello From client: exit To client : exit Server Exit...
  • 62. 62 | P a g e 3. TCP CLIENT:- #include<netdb.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #define MAX 80 #define PORT 8080 #define SA struct sockaddr void func(int sockfd) { char buff[MAX]; int n; for (;;) { bzero(buff, sizeof(buff)); printf("Enter the string : "); n = 0;
  • 63. 63 | P a g e 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;
  • 64. 64 | P a g e // socket create and varification 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)); // assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr= inet_addr("127.0.0.1"); servaddr.sin_port = htons(PORT); // connect the client socket to server socket if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
  • 65. 65 | P a g e printf("connectionwith the server failed...n"); exit(0); } else printf("connected to the server..n"); // function for chat func(sockfd); // close the socket close(sockfd); }
  • 66. 66 | P a g e Output Client side: Socket successfully created.. connectedto the server.. Enterthe string : hi From Server : hello Enterthe string : exit From Server : exit Client Exit...
  翻译: