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.
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:
The java.net package supports the following two main types of network protocols that are used for communication between devices.
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.
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
| Feature | TCP | UDP |
|---|---|---|
| Full Form | Transmission Control Protocol | User Datagram Protocol |
| Type | Connection-oriented | Connection-less |
| Reliability | Reliable | Not Reliable |
| Speed | Slower | Faster |
| Acknowledgement | Yes | No |
| Usage | Web, Email, File Transfer | Video streaming, Gaming |
To read more TCP Vs. UDP
The widely used Java networking terminologies are given below:
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.
A protocol is a set of rules basically that is followed in communication process. For example:
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.
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.
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.
A socket is an endpoint between two-way communications.
The java.net package can be divided into two sections:
To read more URI Vs. URL
The java.net package provides many classes to deal with networking applications in Java.
| Authenticator | CacheRequest | CacheResponse |
| ContentHandler | CookieHandler | CookieManager |
| DatagramPacket | DatagramSocket | DatagramSocketImpl |
| InterfaceAddress | JarURLConnection | MulticastSocket |
| InetSocketAddress | InetAddress | Inet4Address |
| Inet6Address | IDN | HttpURLConnection |
| HttpCookie | NetPermission | NetworkInterface |
| PasswordAuthentication | Proxy | ProxySelector |
| ResponseCache | SecureCacheResponse | ServerSocket |
| Socket | SocketAddress | SocketImpl |
| SocketPermission | StandardSocketOptions | URI |
| URL | URLClassLoader | URLConnection |
| URLDecoder | URLEncoder | URLStreamHandler |
The java.net package also provides several interfaces, including:
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.
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();
}
}
}
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
We request you to subscribe our newsletter for upcoming updates.