Java Networking Basics

📘 Java 👁 59 views 📅 Dec 01, 2025
⏱ Estimated reading time: 3 min

Java provides a rich set of API classes in the java.net package to create network-based applications. Networking allows computers to communicate and exchange data over LAN, WAN, or the Internet.


1. Key Networking Concepts

TermDescription
IP AddressUnique identifier for a device on a network
PortCommunication endpoint in a device
SocketConnection between two machines for data transfer
ServerProgram that listens for requests and responds
ClientProgram that sends requests to a server

2. Java Networking Classes

  • InetAddress → Represents IP addresses and hostnames

  • URL & URLConnection → Access resources over HTTP, FTP, or other protocols

  • Socket → Client-side communication endpoint

  • ServerSocket → Server-side socket that listens for connections


3. InetAddress Example

import java.net.*; public class InetAddressExample { public static void main(String[] args) throws Exception { InetAddress address = InetAddress.getByName("www.google.com"); System.out.println("Host Name: " + address.getHostName()); System.out.println("IP Address: " + address.getHostAddress()); } }

Output (example):

Host Name: www.google.com IP Address: 142.250.191.4

4. URL and URLConnection Example

import java.net.*; import java.io.*; public class URLExample { public static void main(String[] args) throws Exception { URL url = new URL("https://www.example.com"); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String line; while((line = br.readLine()) != null) { System.out.println(line); } br.close(); } }
  • Reads HTML content from a web page

  • Demonstrates client-side HTTP communication


5. Socket Programming

A. Server Example

import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(5000); System.out.println("Server is listening..."); Socket socket = server.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String message = br.readLine(); System.out.println("Message from client: " + message); br.close(); socket.close(); server.close(); } }

B. Client Example

import java.net.*; import java.io.*; public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 5000); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println("Hello, Server!"); out.close(); socket.close(); } }
  • Server listens on port 5000

  • Client connects and sends a message

  • Demonstrates TCP communication


6. Key Points

  • Java networking uses TCP/IP protocols

  • Socket → Client-side; ServerSocket → Server-side

  • InetAddress → Handles IP addresses and hostnames

  • URL & URLConnection → Access remote resources over protocols

  • Supports both synchronous and asynchronous communication


7. Advantages of Java Networking

  • Enables distributed applications

  • Platform-independent network communication

  • Simplifies HTTP, FTP, and TCP/IP programming

  • Integrates easily with Java GUI, Swing, and multithreading for client-server applications


8. Conclusion

Java Networking APIs allow developers to build robust, platform-independent network applications, including web clients, chat applications, and distributed systems. Understanding sockets, URLs, and IP addressing is fundamental for modern Java programming.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes