Attachment
Attachment
Chapter 5 - K Scheme
Basics of Network Programming
Notes
Socket Overview
In Java networking, a socket is one endpoint of a two-way communication link between two
programs running on the network. A socket is bound to a port number so that the
Transmission Control Protocol (TCP) or User Datagram Protocol (UDP) layer can
identify the application that data is destined for.
What is a Socket ?
It allows the application to send and receive data over the network.
Java provides different classes for implementing sockets in the java.net package.
In Java networking, client and server sockets are used to establish a TCP connection for
reliable communication between two systems. The server socket listens for incoming
connections, while the client socket initiates the connection.
Client Socket
Server Socket
Constructor:
ServerSocket serverSocket = new ServerSocket(int port);
Example:
ServerSocket serverSocket = new ServerSocket(5000);Socket clientSocket =
serverSocket.accept();
Reserved Socket
Reserved sockets refer to a range of port numbers that are predefined or reserved by the
Internet Assigned Numbers Authority (IANA) for specific services and protocols. These
sockets are not specific to Java but are important to understand while writing Java networking
programs.
Proxy Server
A proxy server is an intermediate server that sits between a client and the main server. It
acts as a gateway, handling requests from the client and forwarding them to the actual server.
The response from the server is then sent back to the client through the proxy.
InetAddress Class
The InetAddress class in Java is part of the java.net package and is used to represent an IP
address (both IPv4 and IPv6) of a host (either local or remote). It provides methods to
resolve hostnames to IP addresses and vice versa.
Key Feature
Example :
import java.net.*;
Example 2 :
IP (Internet Protocol)
IP is the foundational protocol that routes data from the source to the destination
across networks.
It breaks data into packets and labels them with the sender's and receiver's IP
addresses.
IP is connectionless and does not guarantee delivery, order, or error checking.
✅ Responsibilities of IP:
✅ Features:
Reliable delivery
Acknowledgement of data
Resends lost packet
Maintains order
✅ Features:
No connection setup
Low latency
Best-effort delivery (no guarantee)
Example in Java:
In Java, a TCP/IP server socket allows a server program to listen for incoming TCP
connections from client programs over the network. It is implemented using the
ServerSocket class in the java.net package.
Method Description
ServerSocket(int port) Creates a server socket on the specified port
accept() Waits for a client to connect and returns a new Socket
close() Closes the server socket
Server Program
// File: TCPServer.java
import java.io.*;
import java.net.*;
System.out.println("Client connected!");
// Close connections
reader.close();
writer.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
Client Program
// File: TCPClient.java
import java.io.*;
import java.net.*;
try {
writer.println("Hello Server!");
writer.close();
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
Datagram in Java
In Java networking, a Datagram is a self-contained, independent packet of data sent over a
UDP (User Datagram Protocol) connection. Unlike TCP, UDP is connectionless, meaning
there is no need to establish a connection between the sender and receiver.
Example -:
Server Program
// File: UDPServer.java
import java.net.*;
try {
serverSocket.receive(receivePacket);
serverSocket.send(sendPacket);
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
Client Program
// File: UDPClient.java
import java.net.*;
try {
clientSocket.send(sendPacket);
clientSocket.receive(receivePacket);
clientSocket.close();
e.printStackTrace();
Example
import java.net.*;
try {
} catch (URISyntaxException e) {
e.printStackTrace();
import java.net.*;
import java.io.*;
try {
String line;
System.out.println("\nWebsite Content:");
System.out.println(line);
reader.close();
} catch (Exception e) {
e.printStackTrace();