Unit - I Networking
Unit - I Networking
Networking Basics :-
A) Protocol :-
A protocol is a set of rules and standards for communication.
A protocol specifies the format of data being sent over the Internet, along with
how and when it is sent.
On the other end of the communication, the protocol also defines how the data
is received along with its structure and what it means.
There are three important protocols used in the Internet.
1) IP (Internet Protocol): Is a network layer protocol that breaks data into small
packets and routes them using IP addresses.
B) Addressing :-
Each machine on the network is uniquely identified by a physical address.
This address is the address of the network interface card (LAN card) and is
called the MAC address which is a 48 bit address represented as 12
hexadecimal characters.
For example: 00:09:5B:EC:EE:F2
However, to identify machines across networks, a global addressing scheme
is needed.
Prof. Babar Patil R. M. Page 1
The internet uses IP address to uniquely identify a network and a machine
in the network.
The IP address is a logical address.
The IPv4 addresses are 32 bit and the IPv6 addresses are 128 bits.
Example: IPv4-192.168.100.24
IPv6 0001:0BA0:01E0:D001:0000:0000:D0F0:0010
Any communication between multiple networks in the internet is done using
IP addresses.
Another address that is used by the transport layer for communication is the
port address which identifies the application.
DNS is a system that is used to map the user friendly host names to their
IP addresses and vice versa and used over the Internet.
Basically, there are a number of DNS servers on the Internet, each
listening through UDP software to a port. When a computer on the Internet
needs DNS services-for example, to convert a host name such as
www.google.com to a corresponding IP address-it uses the UDP software
running on its system to send a UDP message to one of these DNS servers,
requesting the IP address.
E) Sockets :-
It is possible to establish a logical connecting point between a server and a
client so that communication can be done through that point. This point is
called socket.
A socket is a bi-directional communication channel between hosts i.e., a
computer on a network often termed as host.
A socket is a point of connection between a server and a client on a
network.
A socket represents the end-point of a network communication.
Sockets provide a simple read/write interface and hide the implementation
details network and transport layer protocols.
When a client wishes to make connection to a server, it will create a socket
at its end of the communication link.
In Java sockets are created with the help of certain classes and these
classes are found in the java.net package.
There are separate classes in the java.net package for creating TCP sockets
and UDP sockets.
The client socket is created with the help of the Socket class and the server
socket is created using the ServerSocket class
The socket concept was developed in the first networked version of UNIX
developed at the University of California at Berkeley.
So sockets are also known as Berkeley Sockets.
F) Ports :-
A port number identifies a specific application running in the machine.
A port is a number in the range 1-65535.
It is a transport layer address used by TCP and UDP to handle
communication between applications.
For example, an email application on one machine wants to communicate
with an email application on another machine.
The machines are identified by the IP addresses but the email applications
are identified by the port numbers at which they communicate i.e. Port 25.
Reserved Ports:
TCP/IP protocol reserves port number in the range 1-1023 for the use of
specified standard services, often referred to as well-known port.
The following table lists some reserved ports and their services.
Through the classes in java.net, Java programs can use TCP or UDP to
communicate over the Internet. The URL, URLConnection, Socket, and
InetAddress :-
The java.net.InetAddress class provides methods to get the IP address of
any hostname.
An IP address is represented by 32-bit or 128-bit unsigned number.
InetAddress can handle both IPv4 and IPv6 addresses.
The InetAddress class is used to encapsulate both, the numerical IP address
and the domain name for that address.
We interact with this class by using the name of an IP host, which is more
convenient and understandable than its IP address.
The InetAddress class hides the number inside.
The InetAddress class has no visible constructors.
The InetAddress class has the inability to create objects directly,
hence factory methods are used for the purpose.
Factory Methods are static methods in a class that return an object of that
class.
Factory methods: -
There are 5 factory methods available in InetAddress class –
Program 1:-
Below is the Java implementation of InetAddress class to demonstrate the use of
factory methods –
import java.io.*;
import java.net.*;
import java.util.*;
class abc {
public static void main(String[] args) throws UnknownHostException
{
// To get and print InetAddress of Local Host
Program 2:-
import java.io.*;
import java.net.*;
import java.util.*;
class abc {
public static void main(String[] args) throws UnknownHostException
{
// To get and print InetAddress of Local Host
InetAddress address1 = InetAddress.getLocalHost();
System.out.println("InetAddress of Local Host : " + address1);
}
}
Output :-
InetAddress of Local Host : localhost/127.0.0.1
InetAddress of Named Host : /45.22.30.39
ALL InetAddresses of Named Host : /172.19.25.29
InetAddresses of Host with specified IP Address : /125.0.0.1
2) byte[] getAddress()
This method returns the IP address associated with this object as an array of
bytes in network order.
3) String getHostAddress()
This method returns a string representation of the IP address associated with
this object. For example: "206.175.64.78".
4) String getHostName()
Returns the hostname associated with this object.
5) Int hashCode()
Returns the hashcode based on the IP address of the object.
6) boolen isMulticastAddress()
True if this object represents a multicast address; false otherwise.
7) String toString()
This method returns a String that contains both the hostname and IP address of
this object.
Program:
Using Instance methods of InetAddress class.
import java.net.*;
public class NetDemo {
public static void main(String[] args)
{ try {
InetAddress i = InetAddress.getLocalHost();
System.out.println("Local Address: " + i);
InetAddress i1 = InetAddress.getByName("www.google.com");
System.out.println("Host Address: " + i1);
TCP/IP Programming :-
A socket is one of a two way communication link between two programs
running on the computer network.
A socket is bound to a port number so that the TCP layer can identify the
application that data is destined to be sent.
Java offers good support for TCP socket, in the form of two socket
classes,
1) java.net.Socket
2) java.net.ServerSocket
Th java.net.Socket class represents a TCP client socket, which connect to
server socket and infinite protocol exchange.
The java.net.ServerSocket class is used to create a server that listens for
incoming connections from one or more clients.
Two TCP class are – Socket class and ServerSocket class.
1) Socket Class :-
It is possible to establish a logical connecting point between a server and a
client so that communication can be done through that point.
This point is called „Socket‟.
Socket is a point of communication between a server and a client on a
network.
The java.net.Socket class allows us to create socket objects that help us in
implementing all fundamental socket operations.
We can perform various networking operations such as sending, reading data
and closing connections.
Each Socket object that has been created using with java.net.Socket class has
been associated exactly with 1 remote host, for connecting to another
different host, we must create a new socket object.
Prof. Babar Patil R. M. Page 10
Constructors for socket Class : -
1) public Socket(String host, int port) throws UnknownHostException,
IOException.
This method attempts to connect to the specified server at the specified port. If
this constructor does not throw an exception, the connection is successful and
the client is connected to the server.
5) public Socket()
Creates an unconnected socket. Use the connect() method to connect this
socket to a server.
Program 1:-
import java.net.*;
import java.io.*;
Output:
Connected to www.google.com/172.217.27.196 on port 88 from port 53771 of
/192.168.1.4
2) ServerSocket Class :-
Java has a different socket class that must be used for creating server
applications.
The ServerSocket class is used to create servers that listen for either local or
remote client programs to connect to them on published ports.
1 Returns the port that the server socket is listening on. This method is useful if
you passed in 0 as the port number in a constructor and let the server find a port
for you.
4 Binds the socket to the specified server and port in the SocketAddress object.
Use this method if you have instantiated the ServerSocket using the no-
argument constructor.
When the ServerSocket invokes accept(), the method does not return until a client
connects. After a client does connect, the ServerSocket creates a new Socket on an
unspecified port and returns a reference to this new Socket. A TCP connection now
exists between the client and the server, and communication can begin.
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
System.out.println("Client Connected");
os.close();
out.close();
s1.close();
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
URL Class :-
The URL class is the gateway to any of the resources available on the
internet.
A Class URL represents a Uniform Resource Locator, which is a pointer to a
“resource” on the World Wide Web.
A resource can point to a simple file or directory, or it can refer to a more
complicated object, such as a query to a database or to a search engine.
Create URL object from the specified protocol, host, port number and
file.
Create URL object from the specified protocol, host name and file name.
Method Description
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getPath());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}catch(Exception e){System.out.println(e);}
}
}
URLConnection Class :-
2. An URLConnection lets us send data back to a web server with POST or PUT
and use other request methods.
The biggest differences between the URL and URLConnection classes are:
1. URLConnection provides access to the HTTP header.
2. URLConnection can configure the request parameters sent to the server.
3. URLConnection can write data to the server as well as read data from theserver.
Method Description
InputStream getInputStream() It returns an input stream that reads from the open
condition.
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
URLConnection c = u.openConnection();
InputStream s = c.getInputStream();
int i;
while ((i = s.read()) != -1)
System.out.print((char)i);