Wednesday, January 22, 2020

Networking in Java

Introduction to Networking in Java

Networking is used to enable multiple devices connected through the network to communicate and share the resources. package provides all the classes and interfaces for network programming. All these classes and Interfaces enable us to communicate over the network at the application layer. 
     Communication between devices follows some protocols. A Protocol is a set of rules which are followed by both the communicating devices. TCP/IP is a standard protocol suite and conceptual model composed of a set of communications protocols used on the Internet and similar computer networks. TCP and UDP are two fundamental protocols available in TCP/IP. package package provides support for TCP (Transmission Control Protocol) and UDP(User Datagram Protocol). package can be roughly divided into two parts low-level APIs with implementations of Addresses, Sockets and Network interfaces and high-level APIs of URI, URL and different connections implementations. 

TCP(Transmission control protocol)

TCP is the part of the Internet protocol suite. Transmission control protocol enables reliable communication between devices. TCP is a network layer protocol that provides acknowledgments of the received packets and resends lost packets. TCP is better than UDP, but it has an additional overhead due to high capabilities. It is used by application-layer protocols like HTTP, FTP, and SMTP. This is the connection-oriented protocol.

UDP(User datagram protocol)

UDP is part of the Internet protocol suite. The user datagram protocol provides unreliable delivery of packets and does not send acknowledgments. This is a connectionless protocol. This method also operates on the Application layer. UDP is used to send short messages called datagrams. DHCP(Dynamic host configuration protocol) is an example of UDP.


A socket can be considered an endpoint of a bi-directional communication link between two programs communicating on the network to enable the exchange of data between them. 

Port Number:

A socket is bound to a port number which is 2-byte unsigned integer that is kept in the header appended with the message. The range for the port number is from 0 to 65535. Port numbers from 0 to 1024 are reserved for standard services. Programmers can use port numbers beyond this limit.


URL is the class representing a Universal Resource Locator, which is a means to access the resources.


URLConnection is created from a URL and is the communication link used to access the resource pointed by the URL. This abstract class and its subclasses implement HTTP or FTP connections.


HttpURLConnection is a subclass of URLConnection and provides additional functionalities specific to the HTTP protocol.


public class Demo {

    public static void main(String[] args) throws IOException {
            URL url=new URL(""); // URL
            URLConnection con=url.openConnection();// URL Connection
            InputStream is=con.getInputStream();// fetching data from input stream
            int ch;
            while((!=-1) //reading the received data
                System.out.print((char)ch);// displaying received data



The InetAddress class is the abstraction representing an IP (Internet Protocol) address. InetAddress has two subclasses:
  1. Inet4Address for IPv4 addresses.
  2. Inet6Address for IPv6 addresses.


public class InetDemo {

    public static void main(String[] args) throws UnknownHostException {

        InetAddress address=InetAddress.getByName("");
        System.out.println("Address is : "+ address);



Video Tutorial