Networking in Java

Overview

  • Java Networking is a concept of connecting two or more computing devices togetherso that we can share resources.​
  • Java socket programming provides facility to share data between different computingdevices.

Java Networking

  • The java.net package supports two protocols,​
  • TCP: Transmission Control Protocol provides reliable communication between thesender and receiver. TCP is used along with the Internet Protocol referred as TCP/IP.​
  • UDP: User Datagram Protocol provides a connection-less protocol service byallowing packet of data to be transferred along two or more nodes.

Java Networking Terminology

  • The widely used Java networking terminologies are given below:​
    • IP Address​
    • Protocol​
    • Port Number​
    • MAC Address​
    • Connection-oriented and connection-less protocol​
    • Socket

IP Address

  • IP address is a unique number assigned to a node of a network e.g. 192.168.0.1 ​
  • It is composed of octets that range from 0 to 255.​
  • It is a logical address that can be changed.

Protocol

  • A protocol is a set of rules basically that is followed for communication. ​
  • For example:​
    • TCP​
    • FTP​
    • Telnet​
    • SMTP​
    • POP etc.

Port Number

  • The port number is used to uniquely identify different applications. ​
  • It acts as a communication endpoint between applications.​
  • The port number is associated with the IP address for communication between twoapplications.

MAC Address

  • MAC (Media Access Control) address is a unique identifier of NIC (Network InterfaceController). ​
  • 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.

Connection-oriented and Connection-less Protocol

  • In connection-oriented protocol, acknowledgement is sent by the receiver. So, it isreliable but slow. ​
  • The example of connection-oriented protocol is TCP.​
  • But, in connection-less protocol, acknowledgement is not sent by the receiver. So, it isnot reliable but fast. ​
  • The example of connection-less protocol is UDP.

Socket

  • A socket is an endpoint between two-way communications.​
  • Visit next page for Java socket programming.

java.net Package

  • The java.net package can be divided into two sections:​
    • A Low-Level API: It deals with the abstractions of addresses High-Level networkingidentifiers, Sockets High-Level bidirectional data communication mechanism andInterfaces i.e. network interfaces.​
    • A High-Level API: It deals with the abstraction of URIs i.e. Universal ResourceIdentifier, URLs i.e. Universal Resource Locator, and Connections i.e. connections tothe resource pointed by URLs.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/*
 * this is a simple tcp server
 * this server will receive a message from a client upon connection
 * then it will display the message to output
 */
public class TCPServer {

	public static void main(String[] args) {
		// start the server for listening

		try (ServerSocket server = new ServerSocket(8080)) {
			// if server is up and running
			System.out.println("Server is listening....");
			
			// accept incoming client request
			Socket socket = server.accept();
			
			PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
			BufferedReader reader = 
					new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
			// read message from client
			String msg = reader.readLine();
			System.out.println("Received: " + msg);
			
			// write (send) message to client
			writer.println("Hello from server!");
			
			
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

}
/*
 * this is  a simple tcp client
 * this client will try to connect to the simple server and send message
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TCPClient {

	public static void main(String[] args) {
		// try connect to server
		try (Socket socket = new Socket("localhost", 8080)) {
			// if successfully connected
			System.out.println("Connected!");
			
			PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
			BufferedReader reader = 
					new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
			// send message to server
			writer.println("Hello from client!");
			
			// read message from server
			String msg = reader.readLine();
			System.out.println("Response: " + msg);
			
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

}