Networking in Java
Overview and Key Programming
Concepts
Introduction to Networking
• Networking allows communication between
computers.
• Java provides the java.net package for
network programming.
• It supports both TCP and UDP protocols.
Networking Basics
• - IP Address: Unique address of a device on
the network
• - Port Number: Identifies a specific process or
service
• - Protocols: TCP (connection-based), UDP
(connectionless)
java.net Package
• Key classes in java.net package:
• - InetAddress: Handles IP addresses
• - Socket: Client-side socket
• - ServerSocket: Server-side socket
• - DatagramSocket & DatagramPacket: Used for
UDP communication
TCP Programming in Java
• Steps:
• 1. Server creates ServerSocket and waits for
client
• 2. Client creates Socket and connects to server
• 3. Use InputStream/OutputStream for
communication
• 4. Close connections
UDP Programming in Java
• Steps:
• 1. Create DatagramSocket
• 2. Create DatagramPacket to send/receive
data
• 3. Send/Receive data
• 4. Close socket
• UDP is faster but does not guarantee delivery.
Example Programs
• - TCP Client/Server Chat
• - UDP Echo Program
• - IP address fetch using InetAddress