Вы находитесь на странице: 1из 11

CS P52 COMPUTER NETWORKS LABORATORY

MANUAL

(JUNE 2018- NOVEMBER 2018)

V SEMESTER

P a g e |1 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


Subject Lectures Tutorial Practical
Subject Name
Code (Periods) (Periods) (Periods)
COMPUTER NETWORKS
CS P52 - - 3
LABORATORY

LIST OF EXPERIMENTS

1. Implementation of a socket program for Echo/Ping/Talk commands.


2. Creation of a socket between two computers and enable file transfer between them. Using (a.)
TCP (b.) UDP
3. Implementation of a program for Remote Command Execution (Two M/Cs may be used).
4. Implementation of a program for CRC and Hamming code for error handling.
5. Writing a code for simulating Sliding Window Protocols.
6. Create a socket for HTTP for web page upload & Download.
7. Write a program to implement RCP (Remote Capture Screen).
8. Implementation (using NS2/Glomosim) and Performance evaluation of the following routing
protocols:
a. Shortest path routing
b. Flooding routing
9. Broadcast/Multicast routing.
10. Implementation of ARP.
11. Throughput comparison between 802.3 and 802.11.
12. Study of Key distribution and Certification schemes.
13. Design of an E-Mail system

P a g e |2 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


INDEX
Ex. Page
Date LIST OF EXPERIMENT Mark Signature
No. No.
STUDY OF COMMANDS FOR SOCKET
1
PROGRAMMING
2 BASIC PROGRAMS
2.1 GENERATE RANDOM PORT NUMBER
2.2 CLIENT-IP ADDRESS
3 TCP COMMUNICATION
3.1 ONE WAY COMMUNICATION
3.2 TWO WAY COMMUNICATION
3.3 ECHO COMMAND
3.4 PING COMMAND
3.5 TALK COMMAND
4 FILE TRANSFER
4.1 TCP
4.2 UDP
5 REMOTE COMMAND EXECUTION
6 ERROR CHECKING
6.1 CYCLIC REDUNDANCY CHECK
6.2 HAMMING CODE
SIMULATION OF SLIDING WINDOW
7
PROTOCOL
HTTP WEBPAGE DOWNLOAD(Response
8
Code)
IMPLEMENTATION OF RCP (Remote
9
Screen Capture)
10 ROUTING
10.1 SHORTEST PATH ROUTING
10.2 FLOODING ROUTING
10.3 MULTICAST ROUTING
10.4 BROADCAST ROUTING
11 ADDRESS RESOLUTION PROTOCOL
THROUGHPUT COMPARISON BETWEEN
12
802.3 & 802.11
KEY DISTRIBUTION AND
13
CERTIFICATION SCHEMES
14 DESIGN OF E-MAIL SYSTEM

P a g e |3 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


Ex.No: 1 STUDY OF COMMANDS FOR SOCKET PROGRAMMING

Aim:
To study the commands of the socket programming

Sockets:
A socket is one end-point of a two-way communication link between two programs running on
the network. Socket classes are used to represent the connection between a client program and a server
program.
The java.net package provides two classes--Socket and ServerSocket--that implement the client
side of the connection and the server side of the connection, respectively.
They connect to them on published ports when the ServerSocket created it will register itself
with the system as having an internet in client connection. The constructor for the ServerSocket having
clients connection it can leave pending before it should be refers connections

Port:
The TCP and UDP protocols use ports to map incoming data to a particular process running on a
computer. Port numbers range from 0 to 65,535 because ports are represented by 16-bit numbers. The
port numbers ranging from 0 - 1023 are restricted; they are reserved for use by well-known services
such as HTTP and FTP and other system service( called well-known ports.)

TCP/IP Protocol:
TCP provides a point-to-point channel for applications that require reliable communications. The
Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Telnet are all examples of
applications that require a reliable communication channel. The order in which the data is sent and
received over the network is critical to the success of these applications. When HTTP is used to read
from a URL, the data must be received in the order in which it was sent. Otherwise, you end up with a
jumbled HTML file, a corrupt zip file, or some other invalid information

Stream communication
The stream communication protocol, transfer control protocol, TCP is a connection-oriented
protocol. In order to do communication over the TCP protocol, a connection must first be established
between the pair of sockets. While one of the sockets listens for a connection request (server), the other
asks for a connection. Once two sockets have been connected, they can be used to transmit data in both
directions

Creating TCP Servers:

1. Create a ServerSocket attached to a port number.


ServerSocket server = new ServerSocket(port);
2. Wait for connections from clients requesting connections to that port.
Socket channel = server.accept();
3. Get input and output streams associated with the socket.
out = new PrintWriter (channel.getOutputStream());
reader = new InputStreamReader (channel.getInputStream());
in = new BufferedReader (reader);
String data = in.readLine();
out.println("Hey! ");
P a g e |4 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE
Creating TCP Clients:
1. Create a Socket object attached to a remote host, port.
Socket client = new Socket(host, port);
2. Get input and output streams associated with the socket.
out = new PrintWriter (client.getOutputStream());
reader = new InputStreamReader (client.getInputStream());
in = new BufferedReader (reader);
out.println("Hi!”);
String data = in.readLine();

UDP:

The UDP protocol provides for communication that is not guaranteed between two applications
on the network. UDP is not connection-based like TCP. Rather, it sends independent packets of data,
called datagrams, from one application to another. Sending datagrams is much like sending a letter
through the postal service: The order of delivery is not important and is not guaranteed, and each
message is independent of any other.

Datagram communication:
The datagram communication protocol, user datagram protocol, is a connectionless protocol,
meaning that each time you send datagrams, you also need to send the local socket descriptor and the
receiving socket‟s address.

Creating UDP Servers:

1. Create a DatagramSocket attached to a port.


int port = 1234;
DatagramSocket socket = new DatagramSocket(port);
2. Allocate space to hold the incoming packet, and create an instance ofDatagramPacket to hold the
incoming data.
byte[] buffer = new byte[1024];
DatagramPacket packet =
newDatagramPacket(buffer, buffer.length);
3. Block until a packet is received, then extract the information you need from thepacket.
socket.receive(packet);
InetAddressremoteHost = packet.getAddress();
intremotePort = packet.getPort();
byte[] data = packet.getData();

Creating UDP Clients:

1. First allocate space to hold the data we are sending and create an instance of
byte[] buffer = new byte[1024];
int port = 1234;
InetAddress host =InetAddress.getByName("magelang.com");
DatagramPacket p =new DatagramPacket(buffer, buffer.length,host, port);
2. Create a DatagramSocket and send the packet using this socket.
DatagramSocket socket = new DatagramSocket();
socket.send(p);
InetAddresslocalHostname = socket.getLocalAddress();
intlocalPort = socket.getLocalPort();

P a g e |5 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


Serversocket Constructor and Methods:
The java.net.ServerSocket class is used by server applications to obtain a port and listen for client
requests

CONSTRUCTOR DESCRIPTION
ServerSocket(int port) Create a server socket bound to the specified port.
The backlog parameter specifies how many incoming
ServerSocket(int port, int backlog)
clients to store in a wait queue.
The inetaddress specifies the local IP address to bind
ServerSocket(int port, int backlog, to and used for servers that may have multiple IP
InetAddress address) addresses, allowing the server to specify which of its
IP addresses to accept client requests on
Creates an unbound server socket. When using this
ServerSocket() constructor, use the bind() method when you are
ready to bind the server socket

METHODS DESCRIPTION
getLocalPort() Returns the port that the server socket is listening on.
Waits for an incoming client. This method blocks
until either a client connects to the server on the
Socket accept() specified port or the socket times out, assuming that
the time-out value has been set using the
setSoTimeout() method.
Sets the time-out value for how long the server socket
setSoTimeout(int timeout)
waits for a client during the accept().
Binds the socket to the specified server and port in
bind(SocketAddress host, int backlog)
the SocketAddress object.

SOCKET CONSTRUCTOR AND METHODS


The creation of socket is done through the constructor of the class socket. The creation of the socket
objects implicates establishes a connection between the client and the server.
The java.net.Socket class represents the socket that both the client and server use to communicate with
each other. The client obtains a Socket object by instantiating one, whereas the server obtains a Socket
object from the return value of the accept() method.

CONSTRUCTOR DESCRIPTION
Socket(String host, int port) Connect to the specified server at the specified port.
Socket(InetAddress host, int port) The host is denoted by an inetaddress object.
Connects to the specified host and port, creating a
Socket(String host, int port,
socket on the local host at the specified address and
InetAddresslocalAddress, intlocalPort)
port.
Socket(InetAddress host, int port, The host is denoted by an inetaddress object instead of
InetAddresslocalAddress, intlocalPort) a String
Creates an unconnected socket. Use the connect()
Socket()
method to connect this socket to a server.

P a g e |6 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


METHODS DESCRIPTION
Connects the socket to the specified host. This method
connect(SocketAddress host, int
is needed only when you instantiated the Socket using
timeout)
the no-argument constructor.
Returns the address of the other computer that this
InetAddressgetInetAddress()
socket is connected to.
Returns the port the socket is bound to on the remote
getPort()
machine.
Returns the port the socket is bound to on the local
getLocalPort()
machine.
Closes the socket, which makes this Socket object no
close()
longer capable of connecting again to any server

INET ADDRESSING
An internet is a 32 bit number that uniquely identifies each computer on the net and has
sequence of four number between 0 and 255 separated by the data. Not randomly assigned, they are
hierarchically assigned.

Class A1.0.0.0 to 126.255.255.255, About 16 million IP addresses in a class Adomain.


Class B128.1.0.0 to 191.254.255.255, About 64 thousand IP addresses in class B domain.
Class C192.0.1.0 to 223.255.254.255, 256 IP addresses in a class C domain.
Class D224.0.0.1 to 239.255.255.255, denote multicast groups.
Class E 240.0.0.0 to 254.255.255.255, Reserved for future use.

Address types

 UNICAST
An identifier for a single interface. A packet sent to a unicast address is delivered to the interface
identified by that address.

The Unspecified Address


Also called anylocal or wildcard address. It must never be assigned to any node. It indicates the
absence of an address. One example of its use is as the target of bind, which allows a server to accept a
client connection on any interface, in case the server host has multiple interfaces.The unspecified
address must not be used as the destination address of an IP packet.

The Loopback Addresses


This is the address assigned to the loopback interface. Anything sent to this IP address loops
around and becomes IP input on the local host. This address is often used when testing a client.

 MULTICAST
An identifier for a set of interfaces (typically belonging to different nodes). A packet sent to a multicast
address is delivered to all interfaces identified by that address

P a g e |7 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


METHODS DESCRIPTION
Returns an InetAddress object given the raw IP
InetAddressgetByAddress(byte[] addr)
address .
InetAddressgetByAddress(String host, byte[] Create an InetAddress based on the provided
addr) host name and IP address.
Determines the IP address of a host, given the
InetAddressgetByName(String host)
host's name.
Returns the IP address string in textual
getHostAddress()
presentation.
getHostName() Gets the host name for this IP address.
InetAddressInetAddressgetLocalHost() Returns the local host.

Result:
Thus the study of the commands for the socket programming is done.

P a g e |8 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


Ex. No: 2.1 GENERATE RANDOM PORT NUMBER

Aim:
To write a java program for generate random port number

Algorithm:

1. Start.
2. Import the java.net and java.io packages.
3. Declare a new class called RandomPort.
4. Inside RandomPort, declare an object of class ServerSocket, called “server”, with port number 0.
5. Display a message saying which port the object “server” runs on by getting the port number
using the method getLocalPort().
6. Catch and handle any exceptions thrown.
7. Stop

Source code:
import java.net.*;
import java.io.*;
class RandomPort
{
public static void main(String args[])
{
try
{
ServerSocket Server = new ServerSocket(0);
System.out.println("this server runs on port "+ Server.getLocalPort());
}
catch(IOException e)
{
System.out.println(e);
}
}
}

OUTPUT:

Result:

Thus the program random port number generated executed and verified.

P a g e |9 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


Ex. No. 2.2 FIND IP ADDRESS OF CLIENT

Aim:

To write a java program for Finding an IP address of client.

Algorithm:

1. Start.
2. Import the java.net, java.io, and java.util packages.
3. Declare a new class called “GetOwnIp”.
4. Create an object for the InetAddress Method to get the LocalHost IP Address.
5. Display the message received, which is the local host Inet Address.
6. Stop

Source Code:
import java.io.*;
import java.net.*;
class IPAddress
{
public static void main(String args[])throws UnknownHostException
{
InetAddress ip = InetAddress.getLocalHost();
System.out.println("IP address : " + ip);
String sysName = ip.getHostName();
System.out.println("system name : " + sysName);
ip = InetAddress.getByName("www.google.com");
System.out.println("name of other system : " + ip);
}
}

OUTPUT:

Result:

Thus the program finding an IP address of client executed and verified.

P a g e |10 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE


Ex. No. 3.1 ONE WAY COMMUNICATION USING TCP

P a g e |11 COMPUTER NETWORKS LABORATORY DEPARTMENT OF CSE

Вам также может понравиться