Cs P52 Computer Networks Laboratory Manual (JUNE 2018-NOVEMBER 2018) V Semester
Cs P52 Computer Networks Laboratory Manual (JUNE 2018-NOVEMBER 2018) V Semester
Cs P52 Computer Networks Laboratory Manual (JUNE 2018-NOVEMBER 2018) V Semester
MANUAL
V SEMESTER
LIST OF EXPERIMENTS
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
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.
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();
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.
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.
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.
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.
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
Result:
Thus the study of the commands for the socket programming is done.
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.
Aim:
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: