Java Networking

Last Updated : 17 Sept 2026

Java networking is used to connect two or more computing devices so that they can exchange data and share resources over a network. It allows applications to communicate over the internet or local networks.

Java provides built-in support for networking through the java.net package, which includes classes and interfaces for handling communication, sockets, URLs, and network resources.

What is Java Networking?

Java networking is the concept of establishing communication between multiple devices using Java programs. It enables data sharing, remote access, and distributed computing.

Java socket programming is an important part of networking that allows applications running on different machines to communicate with each other.

Java supports:

  1. Client-Server Communication
  2. Peer-To-Peer Communication
  3. Internet-Based Applications

Advantage of Java Networking

  1. Resource Sharing: Share files, printers, databases.
  2. Centralized Software Management: Maintain applications in one place.
  3. Communication: Enables email, chat, video calls.
  4. Scalability: Easy to expand network.
  5. Security Features: Supports authentication and encrypted communication.

Types of Networks

  1. LAN: A LAN is a network that connects computers within a small geographical area like a home, school, office, or building.
  2. MAN: A MAN connects multiple LANs within a city or large campus area.
  3. WAN: A WAN connects computers over a large geographical area like countries or continents.

Network Protocols (java.net Package)

The java.net package supports the following two main types of network protocols that are used for communication between devices.

1. TCP (Transmission Control Protocol)

TCP is a connection-oriented protocol that establishes a connection between the sender and receiver before data transmission begins. It provides reliable and ordered communication which ensures that data is delivered without loss or duplication. TCP is used along with the Internet Protocol, commonly referred to as TCP/IP, and is widely used in applications such as web browsing, file transfer, and email services.

2. UDP (User Datagram Protocol)

UDP is a connection-less protocol that does not establish a connection before sending data. It allows packets of data to be transmitted directly between nodes that makes it faster but less reliable than TCP. UDP does not guarantee delivery, order, or duplication of data, and is commonly used in applications like video streaming, online gaming, and real-time communication.

To read more Connection-Oriented Vs. Connection Less Services

TCP Vs. UDP

FeatureTCPUDP
Full FormTransmission Control ProtocolUser Datagram Protocol
TypeConnection-orientedConnection-less
ReliabilityReliableNot Reliable
SpeedSlowerFaster
AcknowledgementYesNo
UsageWeb, Email, File TransferVideo streaming, Gaming

To read more TCP Vs. UDP

Java Networking Terminology

The widely used Java networking terminologies are given below:

  1. IP Address
  2. Protocol
  3. Port Number
  4. MAC Address
  5. Connection-oriented and connection-less protocol
  6. Socket

1) IP Address

IP address is a unique number assigned to a node of a network. For example, 192.168.0.1. It is composed of octets that range from 0 to 255. It is a logical address that can be changed.

2) Protocol

A protocol is a set of rules basically that is followed in communication process. For example:

3) Port Number

The port number uniquely identify different applications. It acts as a communication endpoint between applications.

The port number is associated with the IP address for communication between two applications.

4) MAC Address

MAC (Media Access Control) address is a unique identifier of NIC (Network Interface Controller). A network node can have multiple NIC but each with unique MAC address.

For example, an ethernet card may have a MAC address of 00:0d:83::b1:c0:8e.

5) Connection-oriented and Connection-less Protocol

In connection-oriented protocol, acknowledgement is sent by the receiver. So, it is reliable but slow. The example of connection-oriented protocol is TCP.

But, in connection-less protocol, acknowledgement is not sent by the receiver. So, it is not reliable but fast. The example of connection-less protocol is UDP.

6) Socket

A socket is an endpoint between two-way communications.

java.net Package

The java.net package can be divided into two sections:

  1. A Low-Level API: It deals with the abstractions of addresses i.e. networking identifiers, Sockets i.e. bidirectional data communication mechanism and Interfaces i.e. network interfaces.
  2. A High-Level API: It deals with the abstraction of URIs i.e. Universal Resource Identifier, URLs i.e. Universal Resource Locator, and Connections i.e. connections to the resource pointed by URLs.

To read more URI Vs. URL

List of java.net Package Classes

The java.net package provides many classes to deal with networking applications in Java.

AuthenticatorCacheRequestCacheResponse
ContentHandlerCookieHandlerCookieManager
DatagramPacketDatagramSocketDatagramSocketImpl
InterfaceAddressJarURLConnectionMulticastSocket
InetSocketAddressInetAddressInet4Address
Inet6AddressIDNHttpURLConnection
HttpCookieNetPermissionNetworkInterface
PasswordAuthenticationProxyProxySelector
ResponseCacheSecureCacheResponseServerSocket
SocketSocketAddressSocketImpl
SocketPermissionStandardSocketOptionsURI
URLURLClassLoaderURLConnection
URLDecoderURLEncoderURLStreamHandler

List of java.net Package Interfaces

The java.net package also provides several interfaces, including:

  • ContentHandlerFactory
  • CookiePolicy
  • CookieStore
  • DatagramSocketImplFactory
  • FileNameMap
  • SocketOption<T>
  • SocketOptions
  • SocketImplFactory
  • URLStreamHandlerFactory
  • ProtocolFamily

Example: Java Networking

In the following Java program, we have created a simple client-server communication using socket programming. The server listens on a specific port, and the client connects to the server to send messages.

Server-Side Code

import java.io.*;  
import java.net.*;  
public class ServerSide {  
    public static void main(String[] args) {  
        try (ServerSocket server = new ServerSocket(5000);  
             Socket socket = server.accept();  
        DataInputStream in = new DataInputStream(socket.getInputStream())) {  
            System.out.println("Server started. Waiting for client...");  
            String message;  
            while (!(message = in.readUTF()).equals("End")) {  
                System.out.println("Client: " + message);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

Client-Side Code

import java.io.*;  
import java.net.*;  
public class ClientSide {  
    public static void main(String[] args) {  
        try (Socket socket = new Socket("127.0.0.1", 5000);  
DataOutputStream out = new DataOutputStream(socket.getOutputStream());  
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {  
            System.out.println("Connected to Server");  
            String input;  
            while (!(input = br.readLine()).equals("End")) {  
                out.writeUTF(input);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

When we run the above code, we get the following output.

Server Output:

Server started. Waiting for client...
Client: Hello
Client: How are you?

Client Output:

Connected to Server

Common Networking Exceptions

  • BindException: Port already in use
  • UnknownHostException: Host not found
  • SocketTimeoutException: Connection timeout
  • ConnectException: Server not running