Computer Networks Lab File
Computer Networks Lab File
Practical File
of
Computer Network Lab
(KCS 653)
for
B. Tech. in Computer Science & Engineering
(3rd Year, 6th Semester)
(2021-22)
S. no. Experiments
1 To study the Cross-Wired and Straight through network cables.
2
Implementation of Stop and Wait Protocol and Sliding Window
Protocol.
3
Study of Socket Programming and Client – Server model.
4
Write a code simulating ARP /RARP protocols.
5
Write a code simulating PING and TRACEROUTE commands.
6
Create a socket for HTTP for web page upload and download.
7 Write a program to implement RPC (Remote Procedure Call).
8
Implementation of Subnetting.
9 Applications using TCP Sockets like a. Echo client and echo server b.
Chat c. File Transfer.
10 Applications using TCP and UDP Sockets like
d. DNS e. SNMP f. File Transfer.
The Ethernet cables are categorized as Cat 5, Cat 5e, Cat 6, and UTP cable. Cat 5 cable can support
a 10/100 Mbps Ethernet network while Cat 5e and Cat 6 cable to support Ethernet.
Straight-through cable is a type of CAT5 with RJ-45 connectors at each end, and each has the same
pin out. It is in accordance with either the T568A or T568B standards. It uses the same color code
throughout the LAN for consistency. This type of twisted-pair cable is used in LAN to connect a
computer or a network hub such as a router. It is one of the most common types of network cable.
Crossover Cable
A Crossover cable is a type of CAT 5 where one end isT568A configuration and the other end as
T568BConfiguration. In this type of cable connection, Pin 1 is crossed with Pin 3, and Pin 2 is
crossed with Pin 6.
HEMANT KUMAR | 1901530100014 | B.TECH CSE 3rd YEAR
Crossover cable is used to connect two or more computing devices. The internal wiring of crossover
cables reverses the transmission and receive signals. It is widely used to connect two devices of the
same type: e.g., two computers or two switches to each other.
In regard to physical appearance, Crossover Ethernet cables are very much similar to regular
Ethernet cables. Still, they are different with regard to the order with which the wires are arranged.
This type of Ethernet cable is made to connect to network devices of the same kind over Ethernet
directly. Crossover cables are mostly used to connect two hosts directly.
Router to Router
Here is an application where you should use Crossover Cable:
Aim: Implementation of Stop and Wait Protocol and Sliding Window Protocol.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int k,time,win=2,i2=0,frame=0,a[20],b[20],i,j,s,r,ack,c,d;
int send(int,int);
int receive();
int checsum(int *);
main()
{
int i1=0,j1=0,c1;
printf("Enter the frame size\n");
scanf("%d",&frame);
printf("Enter the window size\n");
scanf("%d",&win);
j1=win;
for(i=0;i<frame;i++)
a[i]=rand(); } k=1;
while(i1<frame)
{
if((frame-i1)<win)
j1=frame-i1;
ack=receive(i1,i1+j1,c1);
if (ack!=0)
{
printf("\n\n1.Selective window\n");
printf("2.Go back N\n");
scanf("%d",&ack);
switch(ack)
{
case 1:
printf("\n\n\t Selective window \t\nEnter the faulty frame no\n");
scanf("%d",&i2);
printf("\n\n Retransmit the frame %d \n",i2);
send(i2,i2+1);
break; case 2:
HEMANT KUMAR | 1901530100014 | B.TECH CSE 3rd YEAR
printf("\n\n\t Go back n\t\n\n");
printf("\nRetransmit the frames from %d to %d\n",i1,i1+j1);
send(i1,i1+j1);
break;
}
}
i1=i1+win;
k++;
}
}
int send(c,d)
{
int t1;
for(i=c;i<d;i++)
{
b[i]=a[i];
printf("frame %d is sent\n",i);
}
s=checsum(&a[c]);
return(s);
}
int receive(c,d,c2)
int c2;
{ r=checsum(&b[c]);
if(c2==r)
{
return(0);
}
else return(1);
}
int checsum(int *c)
{
int sum=0;
for(i=0;i<win;i++)
sum=sum^(*c);
return sum;
}
1.selective window
2.Go back N
1 selective window
enter the faculty frame no
15
retransmit the frames from 15
frame 15 is sent
transmit the window no 2
frame 5 is sent
frame 6 is sent
frame 7 is sent
frame 8 is sent
frame 9 is sent
1.selective window
2.Go back N
#include<iostream.h>
#include<stdio.j>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#includ<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#includ<time.h>
int main()
{
int listenfd,connfd,n;
struct sockaddr_in serv;
char buff[100];
int port=1058;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(port);
bind(listenfd,(struct sockaddr*)&serv,sizeof(serv));
listen(listenfd,15);
while(1) { connfd=accept(listenfd,(struct sockaddr*)NULL,NULL);
cout<<"\nConx.Open!]n";
int child;
if((child=fork())<<"\nChild disaster!";
exit(1);
}
else if(child==0)
{
close(listenfd);
strcpy(buff,"abc");
while(strcmp(buff,"bye")!=0)
{
if(strcmp(buff,"bye")==0)
break; n=read(connfd,buff,100);
buff[n]='\0';
cout<<"\nClient:"<<<"Server:";
gets(buff);
write(connfd,buff,strlen(buff));
}
cout<<"\n\n Conx closed!";
close(connfd);
exit(0);
HEMANT KUMAR | 1901530100014 | B.TECH CSE 3rd YEAR
}
close(connfd);
}
return 0;
}
Client Program:-
#include<iostream.h>
#include<stdio.j>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#includ<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#includ<errno.h>
#define MAX 100
main(int argc.char*argv)
{
int sockfd,n;
char buff[100];
struct sockaddr_in serv;
if(argc!=3)
{
printf("\n Error ! usage:./a.out\n");
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
//ARP CLIENT
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<string.h>
Main()
{
int shmid,a;
char *ptr, *shmptr;
char ptr2[51], ip[12], mac[26]; shmid=shmget(3000,10,0666);
shmptr=shmat(shmid,NULL,0);
puts("the arp table is");
printf("%s",shmptr);
printf("\n1.ARP\n 2.RARP\n 3.EXIT\n");
scanf("%d",&a);
HEMANT KUMAR | 1901530100014 | B.TECH CSE 3rd YEAR
switch(a)
{
case 1:
puts("enter ip address");
scanf("%s",ip);
ptr=strstr(shmptr, ip);
ptr-=8;
sscanf(ptr,"%s%*s",ptr2);
printf("mac addr is %s",ptr2);
break;
case 2:
puts("enter mac addr");
scanf("%s",mac); ptr=strstr(shmptr, mac);
sscanf(ptr,"%*s%s",ptr2);
printf("%s",ptr2);
break;
case 3:
exit(1);
}
}
Variation 2)
#include<sys/socket.h>
#include< netinet/in.h>
#include<arpa/in.h>
main()
{
char host[17],*ptr;
long int addr;
struct in_addr ad; /* netinet/in.h*/ puts("\n\nEnter the host ID");
gets(host);
addr= inet_addr(host); /*convert dotted decimal notation to 32-bit ip addres*/ puts("\n\n32 Bit IP
address is:");
printf("\n\n%d",addr);
puts("\n\nEnter the 32 bit ip address :\n");
scanf("%d",&ad.s_addr); ptr=inet_ntoa(ad); /*convert ip to dotted decimal*/ puts("\n\nHost ip
address is");
printf("\n%s",ptr);
}
PING:
Variation 1)
int main()
Else
NSLog(@"Host is unreachable");
Variation 2)
SCNetworkReachabilityFlags flags;
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags &
kSCNetworkFlagsConnectionRequired);
if (isAvailable)
Else
NSLog(@"Host is unreachable");
Aim: Create a socket for HTTP for web page upload and download
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#includ<netdb.h>
#include<arpa/inet.h>
#define size 100
FILE *local;
char buff[size];
int con,i,l,nrv,sd;
if(argc!=3)
printf(“\nUsage: %sfilename”,argv[0]);
exit(1);
if(cp=strchr(argv[1],’/’))
*cp=’\0’; l=strlen(argv[1]);
hostname=malloc(l+1);
strcpy(hostname,argv[1]);
*cp=’/’;
l=strlen(cp); req=malloc(l+1);
strcpy(req,cp);
else
hostname=argv[1];
req=”/”;
printf(“\nHost=%s\nReq= %s”,hostname,req);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
exit(1);
bzero(&sock,sizeof(sock));
sock.sin_family=AF_INET;
con=inet_pton(AF_INET, argv[1], &sock);
sock.sin_port=htons(80);
perror(“\nConnection failed”);
exit(1);
nrv=read(sd,buff,size);
if(nrv>0)
}
else break;
}
while(1);
close(sd);
fclose(local);
return 0;
}
RESULT:
Thus the program was executed and output is verified successfully.
AIM: To Write a Network Socket program for implementing the echo operation.
ALGORITHM:
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for server and bind socket with addresses.
4. Specify number of allowed connections.
5. Wait for connection.
6. Accept connection if any.
7. Retrieve information from Connected Socket.
8. Echo retrieved information to Connected Socket.
9. Close Connected Socket.
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for client.
4. Connect client socket to the server socket addresses.
5. Give message to be echoed.
6. Retrieve echoed information from server and display it.
SERVER PROGRAM:
import java.io.*;
import java.net.*;
import java.lang.String.*;
public class Secho
{
while(true)
{
str=in.readLine();
out.writeBytes(str+"\n");
System.out.println("Msg from Client");
System.out.println(str+"\n");
}
}
}
CLIENT PROGRAM:
import java.io.*;
import java.net.*;
import java.lang.String.*;
public class Cecho
{
public static void main(String args[]) throws Exception
{
DataInputStream in=new DataInputStream (System.in);
Socket s=new Socket("LocalHost",123);
DataInputStream inecho=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
String str;
System.out.println("\nCLIENT SIDE!...\nType EXIT TO QUIT\nEnter Client Msg");
while((str=in.readLine())!=null)
{
out.writeBytes(str+"\n");
if(str.equals("exit"))
{
out.writeBytes("\nClient Terminated");
break;
}
Else{
System.out.println("\nEcho From Server");
System.out.print(str+"\n");
System.out.println("\nCLIENT SIDE!...\nEnter Client Msg");
}
}
}
}
SERVER:
CLIENT:
RESULT:
Thus the program was executed and output is verified successfully.
b. Chat
CHAT APPLICATION
AIM: To write a network program for implementing chat using TCP socket.
SERVER PROGRAM:
import java.io.*;
import java.net.*;
public class chatserver
{
public static void main(String args[])throws Exception
{
DataInputStream din=null;
DataOutputStream dout=null;
Socket c=null;
ServerSocket m=null;
DataInputStream stdin=new DataInputStream(System.in);
try
{
m=new ServerSocket(68);
c=m.accept();
din=new DataInputStream(c.getInputStream());
dout=new DataOutputStream(c.getOutputStream());
}
catch(Exception e)
{
}
while(c!=null)
{
String m2;
System.out.println("Server");
while(true)
{
String m1=din.readLine();
System.out.println("Message from client.."+m1);
System.out.println("\n\n Enter the message...");
m2=stdin.readLine();
dout.writeBytes(""+m2);
dout.writeBytes("\n");
}
}
din.close();
dout.close();
c.close();
m.close();
HEMANT KUMAR | 1901530100014 | B.TECH CSE 3rd YEAR
}
}
CLIENT PROGRAM:
import java.io.*;
import java.net.*;
public class chatclient
{
public static void main(String args[])throws Exception
{
Socket c=null;
DataInputStream uin=null;
DataInputStream din=null;
DataOutputStream dout=null;
try
{
c=new Socket("localhost",68);
uin=new DataInputStream(System.in);
din=new DataInputStream(c.getInputStream());
dout=new DataOutputStream(c.getOutputStream());
}
catch(Exception e)
{
}
if(c!=null)
{
String inp;
System.out.println("Enter the message:");
while((inp=uin.readLine())!=null)
{
dout.writeBytes(""+inp);
dout.writeBytes("\n");
System.out.println("Echoed message from server.."+din.readLine());
System.out.println("Enter ur message:");
}
}
din.close();
dout.close();
c.close();
}
}
OUTPUT:
SERVER:
RESULT:
Thus the program was executed and output is verified successfully.
c. File Transfer
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
HEMANT KUMAR | 1901530100014 | B.TECH CSE 3rd YEAR
#include <errno.h>
#include <arpa/inet.h>
int main(void)
{
int sockfd = 0;
int bytesReceived = 0;
char recvBuff[256];
memset(recvBuff, '0', sizeof(recvBuff));
struct sockaddr_in serv_addr;
/* Create a socket first */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
/* Initialize sockaddr_in data structure */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000); // port
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Attempt a connection */
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
/* Create file where data will be stored */
FILE *fp;
fp = fopen("sample_file.txt", "ab");
if(NULL == fp)
{
printf("Error opening file");
return 1;
}
/* Receive data in chunks of 256 bytes */
while((bytesReceived = read(sockfd, recvBuff, 256)) > 0)
{
printf("Bytes received %d\n",bytesReceived);
// recvBuff[n] = 0;
fwrite(recvBuff, 1,bytesReceived,fp);
}
return 0;
}
AIM: To write a UDP program for implementing the simple DNS service.
ALGORITHM:
SERVER PROGRAM
import java.net.*;
import java.io.*;
class p1sender {
public static void main(String args[]) throws Exception {
byte senderBuffer[] = new byte[128]; // Packets 128 bytes long
String toName, userIn;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket senderSocket = new DatagramSocket(999);
// Create socket for sending
if (args.length > 0) toName = args[0]; // IP or DNS from command line
else toName = "localhost";
while ((userIn=in.readLine())!=null ){ // Read standard input
senderBuffer=userIn.getBytes(); // Convert from string to byte array
// Send datagram packet out socket
InetAddress.getByName(toName), 666));
}
}
}
CLIENT PROGRAM
import java.net.*;
class p1receiver {
public static void main(String args[]) throws Exception {
byte receiveData[] = new byte[128];
DatagramSocket receiveSocket = new DatagramSocket(666); // Create socket for receiving
DatagramPacket receivePacket = new DatagramPacket(receiveData, 128);
while (true) {
receiveSocket.receive(receivePacket); // Wait for datagram packet to arrive
// Print packet contents as String
System.out.println(new String(receivePacket.getData(), 0, receivePacket.getLength()));
}
}}
OUTPUT:
SERVER
CLIENT