Introduction to Java Networking: Basics, Classes, and Examples
#Introduction Java Networking: Basics, Classes, Examples
Introduction
Networking is a crucial aspect of modern applications. Java Networking provides a robust set of APIs for building networked applications, enabling communication between devices through TCP/IP protocols. With Java’s built-in networking classes, developers can easily create client-server applications, work with URLs, and manage data transfer over the internet.
This article explores the basics of Java networking, including important classes, concepts, and practical examples.
IP Address – A unique identifier for devices in a network.
Port Number – A logical address within the device used to identify specific processes.
Socket – An endpoint for two-way communication between two programs.
Protocol – A set of rules (e.g., TCP/UDP, HTTP/HTTPS) for communication.
URL (Uniform Resource Locator) – A reference to a web resource.
Java provides the java.net
package, which includes classes for networking:
InetAddress
– Represents an IP address.
URL
– Represents a Uniform Resource Locator.
URLConnection
– Represents a communication link between an app and a URL.
Socket
– Implements client-side communication.
ServerSocket
– Implements server-side communication.
DatagramSocket
and DatagramPacket` – Used for UDP communication.
import java.net.*;
public class IPExample {
public static void main(String[] args) throws Exception {
InetAddress ip = InetAddress.getByName("www.google.com");
System.out.println("Host Name: " + ip.getHostName());
System.out.println("IP Address: " + ip.getHostAddress());
}
}
Output:
Host Name: www.google.com
IP Address: 142.250.xxx.xxx
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept(); // waits for client
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
System.out.println("Message from client: " + str);
ss.close();
}
}
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 6666);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}
}
Output:
Message from client: Hello Server
TCP (Transmission Control Protocol): Reliable, connection-oriented.
UDP (User Datagram Protocol): Fast, connectionless.
HTTP/HTTPS: Used for web communication.
Platform-independent networking support.
Easy-to-use classes in java.net
.
Supports both TCP and UDP protocols.
Enables building distributed systems.
Java Networking provides a strong foundation for creating distributed applications. From simple IP lookups to client-server communication, Java’s java.net
package offers all necessary tools. Understanding networking basics is essential for web applications, chat systems, and real-time communication tools.
SEO Keywords to Target:
Java networking basics
Java sockets example
Client-server communication in Java
Java URL and InetAddress
Networking in Java tutorial