Introduction to Java Networking: Basics, Classes, and Examples

8/16/2025

#Introduction Java Networking: Basics, Classes, Examples

Go Back

Java Networking Basics


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.


#Introduction  Java Networking: Basics, Classes,  Examples

Core Concepts of Java Networking

  1. IP Address – A unique identifier for devices in a network.

  2. Port Number – A logical address within the device used to identify specific processes.

  3. Socket – An endpoint for two-way communication between two programs.

  4. Protocol – A set of rules (e.g., TCP/UDP, HTTP/HTTPS) for communication.

  5. URL (Uniform Resource Locator) – A reference to a web resource.


Important Java Networking Classes

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.


Example 1: Getting IP Address

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

Example 2: Simple Client-Server Communication

Server Program:

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();
    }
}

Client Program:

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

Protocols in Java Networking

  • TCP (Transmission Control Protocol): Reliable, connection-oriented.

  • UDP (User Datagram Protocol): Fast, connectionless.

  • HTTP/HTTPS: Used for web communication.


Advantages of Java Networking

  • Platform-independent networking support.

  • Easy-to-use classes in java.net.

  • Supports both TCP and UDP protocols.

  • Enables building distributed systems.


Conclusion

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

Table of content