Unit 4 - Networking Basics
Unit 4 - Networking Basics
Java Networking is a concept of connecting two or more computing devices together so that we
can share resources.
Java socket programming provides a facility to share data between different computing devices.
● TCP: Transmission Control Protocol provides reliable communication between the sender
and receiver. TCP is used along with the Internet Protocol referred to as TCP/IP.
● UDP: User Datagram Protocol provides a connection-less protocol service by allowing
packet of data to be transferred along two or more nodes
2) Protocol
A protocol is a set of rules basically that is followed for communication. For example:
TCP
FTP
Telnet
SMTP
POP etc.
3) Port Number
The port number is used to uniquely identify different applications. It acts as a communication
endpoint between applications.
The port number is associated with the IP address for communication between two applications.
4) MAC Address
MAC (Media Access Control) address is a unique identifier of NIC (Network Interface
Controller). A network node can have multiple NIC but each with unique MAC address.
For example, an ethernet card may have a MAC address of 00:0d:83::b1:c0:8e.
6) Socket
A socket is an endpoint between two way communications.
java.net package
The java.net package can be divided into two sections:
● A Low-Level API: It deals with the abstractions of addresses i.e. networking identifiers,
Sockets i.e. bidirectional data communication mechanism and Interfaces i.e. network
interfaces.
● A High Level API: It deals with the abstraction of URIs i.e. Universal Resource
Identifier, URLs i.e. Universal Resource Locator, and Connections i.e. connections to the
resource pointed by URLs.
The java.net package provides many classes to deal with networking applications in Java.
Client-Server Model
The client-server model is a network architecture where a client requests information or services
from a server, which then responds to the request.
Reserved Sockets
Once connected, a higher-level protocol ensues, which is dependent on which port we are using.
TCP/IP reserves the lower 1,024 ports for specific protocols. Many of these will seem familiar to
us if we have spent any time surfing the Internet. Port number 21 is for FTP, 23 is for Telnet, 25
is for e- mail, 79 is for finger, 80 is for HTTP, 119 is for net-news and the list goes on. It is up to
each protocol to determine how a client should interact with the port.
Daytime
Full Name: Daytime Protocol
Use: Provides an ASCII representation of the current time on the server.
Port No: 13 (TCP/UDP)
FTP Data
Full Name: File Transfer Protocol Data
Use: Used to transfer files.
Port No: 20 (TCP)
FTP
Full Name: File Transfer Protocol
Use: Used to send FTP commands like put and get.
Port No: 21 (TCP)
SSH
Full Name: Secure Shell
Use: Used for encrypted, remote logins.
Port No: 22 (TCP)
Telnet
Full Name: Telnet Protocol
Use: Used for interactive, remote command-line sessions.
Port No: 23 (TCP)
SMTP
Full Name: Simple Mail Transfer Protocol
Use: Used to send email between machines.
Port No: 25 (TCP)
Time
Full Name: Time Protocol
Use: Returns the number of seconds elapsed since midnight, January 1, 1900.
Whois
Full Name: Whois Protocol
Use: A simple directory service for Internet network administrators.
Port No: 43 (TCP)
Finger
Full Name: Finger Protocol
Use: Returns information about a user or users on the local system.
Port No: 79 (TCP)
HTTP
Full Name: Hypertext Transfer Protocol
Use: The underlying protocol of the World Wide Web.
Port No: 80 (TCP)
POP3
Full Name: Post Office Protocol Version 3
Use: Transfers accumulated email from the host to sporadically connected clients.
Port No: 110 (TCP)
NNTP
Full Name: Network News Transfer Protocol
Use: Usenet news transfer.
Port No: 119 (TCP)
IMAP
Full Name: Internet Message Access Protocol
Use: Protocol for accessing mailboxes stored on a server.
Port No: 143 (TCP)
RMI Registry
Full Name: Remote Method Invocation Registry
Use: The registry service for Java remote objects.
Port No: 1099 (TCP)
java.net.InetAddress
The java.net.InetAddress class provides methods to get the IP address of any hostname. An IP
address is represented by a 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4
and IPv6 addresses.
Instance Methods
equals(Object other) - Returns true if this object has the same Internet address as other.
getAddress() - Returns a byte array that represents the object’s Internet address in network byte
order.
getHostAddress() - Returns a string that represents the host address associated with the
InetAddress object.
getHostName() - Returns a string that represents the host name associated with the InetAddress
object.
isMulticastAddress() - Returns true if this Internet address is a multicast address; otherwise,
returns false.
toString() - Returns a string that lists the host name and the IP address for convenience.
Server
● Role: Waits for incoming requests from clients and provides resources or services in
response.
● Listens: Servers actively listen on specific ports for incoming client connections.
● Connections: Accepts connections from clients, establishing a communication channel
for data exchange.
● Processes Requests: Handles client requests, performs necessary operations (like database
queries), and generates responses.
● Sends Responses: Sends data back to the client or confirms that the requested operation
was successful.
● Examples: Web servers (e.g., Apache, Nginx), database servers (e.g., MySQL), file
servers (e.g., FTP servers).
Working of Socket
Create a ServerSocket:
A new ServerSocket is instantiated on a specified port using the ServerSocket() constructor.
Establish Connection:
Once a client connects, the accept() method returns a Socket object, which represents the
connection between the server and the client.
Server-Client Interaction:
The server and the client exchange data following a predefined protocol.
ServerSocket Constructors
ServerSocket(int port)
Description: Creates a server socket bound to the specified port. The queue length defaults to 50,
meaning that up to 50 incoming connection requests can be queued while the server is busy.
Throws:
BindException if the port is already in use.
IOException for general I/O errors.
Methods
accept()
Description: Blocks and waits for a client to connect. Once a connection is made, it returns a
Socket object.
Signature: public Socket accept() throws IOException
close()
Description: Closes the ServerSocket, freeing up the port and terminating all active connections.
Signature: public void close() throws IOException
getInetAddress()
Description: Returns the local IP address to which the ServerSocket is bound. If not bound, it
returns null.
Signature: public InetAddress getInetAddress()
getLocalPort()
Description: Returns the port number on which the ServerSocket is listening. If it is not bound, it
returns -1.
Signature: public int getLocalPort()
URL
The URL (Uniform Resource Locator) is a standardized way to uniquely identify and locate
resources on the Internet. Tim Berners-Lee devised URLs to provide a scalable method for
naming resources, which became fundamental to the Web's functionality.
URL Components:
Protocol: The communication method, such as http, ftp, file, etc. (e.g., http://).
Host Name or IP Address: The server's address, delimited by // and / (e.g., www.rediff.com).
Port Number (optional): Specifies the communication port, delimited by a colon : (e.g., :80 for
HTTP, which is often defaulted).
File Path: The path to the specific file or resource on the server (e.g., /index.htm).
Java’s URL class provides an API for handling and accessing information via URLs. It includes
constructors for specifying URLs, and they can throw a MalformedURLException if the format
is invalid.
The URL https://darshankhapekar.web.app/ can be broken down into the following four
components:
Protocol: https
Specifies the communication protocol (HyperText Transfer Protocol Secure).
Host Name: darshankhapekar.web.app
The domain name of the host server.
Port Number: (Implicit, not shown)
The port number is not explicitly mentioned, but it defaults to port 443 for https.
File Path: /
The root directory or default resource (this could load an index.html file on the server).
Constructor
URL(String urlSpecifier)
This constructor creates a URL from a single string, where the string contains the full URL
(protocol, host, optional port, and path).
URLConnection Class
URLConnection is an abstract class that represents an active connection to a resource specified
by a URL.
It provides more control over server interactions compared to the URL class, especially with
HTTP servers.
Key Features:
Header Management: Inspect and modify headers sent to and received from the server.
Binary Data Transfer: Supports downloading binary files.
HTTP Methods: Allows sending data to servers using methods like POST or PUT.
Custom Configuration: The URLConnection object can be configured before interacting with the
server.
The class constructor is protected, meaning it is designed to be subclassed and is not instantiated
directly. Instead, use the openConnection() method from a URL object to retrieve a
URLConnection instance.
Overview of Datagrams
Definition: Datagrams are packets of information sent over a network without guarantees of
delivery or order, contrasting with TCP's reliable stream.
Usage: Useful in scenarios where speed is critical and occasional packet loss is acceptable.
Key Classes
DatagramPacket: Represents a packet of data to be sent or received.
Constructors:
DatagramSocket()
DatagramSocket(int port)
DatagramSocket(int port, InetAddress interface)
DatagramPacket Methods
Get Methods:
getAddress(): Returns the remote host's address.
getPort(): Returns the remote port.
getSocketAddress(): Returns the complete socket address (IP and port).
getData(): Returns the data array.
getLength(): Returns the number of bytes of data in the packet.
getOffset(): Returns where the data starts in the array.
Set Methods:
setData(byte[] data): Changes the packet's data.
setAddress(InetAddress remote): Changes the destination address.
setPort(int port): Changes the destination port.
setLength(int length): Changes the number of bytes considered as data.
DatagramSocket Methods
Sending and Receiving: