JPR Unit 5 Q & A
JPR Unit 5 Q & A
1. What is networking?
Ans.
Networking:
Networking is the process of connecting two or more computers to share data and resources like
files, printers, or internet access.
In Java, networking means communication between programs running on different machines
using TCP/IP or UDP protocols.
Java provides the java.net package, which includes classes like :-
o Socket and ServerSocket (for TCP).
o DatagramSocket (for UDP).
o URL and URLConnection (for web communication).
Example Use :-
Sending data from a client program to a server program.
Ans.
Advantages:
1. Resource Sharing: Devices like printers, files, and internet can be shared.
2. Communication: Enables email, messaging, and video calls.
3. Centralized Data Access: Data can be stored and accessed from one location.
4. Remote Access: Users can access the network from different locations.
Disadvantages:
Ans.
Protocol:
Protocol is a set of rules and standards that define how data is transmitted and received over a
network.
It ensures proper communication between devices or programs by specifying how connections
are made, how data is formatted, and how errors are handled.
List of Protocols:
4. Explain with suitable diagram the HTTP communication between web server and web client.
Ans.
HTTP (Hyper Text Transfer Protocol) is a communication protocol used for data exchange
between a web client (browser) and a web server.
When a user enters a URL, the client sends an HTTP request to the server.
The server processes the request and sends back an HTTP response (like an HTML page, image,
etc.).
Diagram –
Example –
Ans.
The Client/Server model is a type of network architecture where two types of devices interact -
clients and servers.
In this model -
o The client sends a service request to the server.
o The server processes the request and sends back a response.
o They communicate using well-established protocols such as HTTP, FTP, or TCP/IP.
Client and Server Roles -
o A client is any device or software that wants to use the resources of a server.
o A server is a device or program that offers resources or services like data, files, or printer
access.
Examples of Servers -
o Web Server: Shares web pages.
o Print Server: Manages network printers.
o Disk Server: Provides shared disk space.
Working -
o A single server can handle multiple clients at once.
o Each client session is unique, even if connected to the same port.
o To handle many clients at the same time, the server must be multithreaded or support
I/O multiplexing.
Example -
o A browser (client) sends a request to www.google.com.
o Google’s web server processes it and returns the web page.
Diagram -
6.What is socket? Define it. How it works?
Ans.
🔷 Socket:
A socket is one endpoint of a two-way communication link between two programs running on a
network.
In Java networking, a socket acts as an interface between an application and the network,
allowing the application to send and receive data over the internet using TCP or UDP protocols.
🔷 Socket Overview -
🧩 Concept -
🔹 Client Socket -
🔹 Server Socket -
🔹 Diagram –
Ans.
Client/Server Networking:
Client/Server Networking is a communication model where clients and servers interact over a
network using well-established protocols.
The client sends a request for a service, and the server processes and responds to that request.
🔷 Working -
The client makes a service request to the server (e.g., access a webpage).
The server listens to the request and responds with the required resource or action.
Communication takes place using standard protocols such as HTTP, FTP, SMTP, etc.
Each client-server session is unique, even if many clients connect to the same server port.
🔷 Key Concepts -
A server is a system or program that shares its resources like processing power, storage, web
content, or printers.
Types of servers include:
o Web Server (stores web pages)
o Disk Server (shares disk space)
o Print Server (manages networked printers)
A client is any system or application that requests access to these shared resources.
A server can handle multiple client connections at once using multithreading or I/O
multiplexing.
🔷 Diagram –
8.What is port?
Ans.
🔹 Port:
🔹 Need of ports -
If a computer handled only one task over the network, an IP address alone would be enough.
But modern computers perform multiple tasks simultaneously—such as handling email, FTP,
and web browsing—so ports help manage this by assigning a unique number to each service.
🔹 Port Characteristics -
A port number is a 16-bit number ranging from 1 to 65535.
Ports are not physical, like USB or Ethernet ports—they are logical identifiers.
Each port can be associated with a specific service (e.g., HTTP, FTP, etc.).
Note –
o Ports 1–1023 are reserved for standard services.
o These are called well-known ports.
In this code, 8080 is the port number used to connect to a specific service on the server.
🔹 Analogy -
If your computer is a hotel (IP address), then each port number is like a room number.
Multiple services (guests) can stay in the same hotel, but they must each have a different room
(port) to function properly.
Ans.
The Socket class is used to create a TCP connection between a client and a server.
With the help of getOutputStream(), the socket allows sending data to the server.
This stream sends bytes or characters to the connected destination.
✅ 4. Bi-Directional Communication -
The Socket class supports two-way communication between client and server.
Both input and output streams are used for interactive data exchange.
✅ 5. Real-Time Applications -
Using the close() method, the socket can safely terminate the connection.
This prevents data loss and releases system resources after communication ends.
Ans.
In Java, TCP/IP client sockets are used when a client wants to establish a reliable connection to
a server using the TCP protocol.
A client socket is created using the Socket class.
It connects to a specific IP address (host) and port number of the server.
Once connected, the client can –
o Send data using getOutputStream().
o Receive data using getInputStream().
🔹 Key Points -
import java.io.*;
import java.net.*;
out.println("Hello Server");
socket.close();
Ans.
• In Java, TCP/IP server sockets are used when a server needs to listen for and accept incoming client
connections using the TCP protocol.
• A server socket is created using the ServerSocket class.
• The server listens on a specific port for client connections and accepts them when they arrive.
• Once a client connects, the server can –
o Receive data using getInputStream().
o Send data using getOutputStream().
🔹 Key Points -
• TCP is a connection-oriented protocol, ensuring reliable and ordered data transmission between client
and server.
• The server listens for incoming connections, while the client initiates the connection.
• After communication, the server should close the socket to release resources.
import java.io.*;
import java.net.*;
System.out.println("Server is running...");
socket.close();
serverSocket.close();
The server creates a ServerSocket to listen for incoming connections on port 5000.
When a client connects, the server accepts the connection and establishes a Socket for
communication.
Ans.
TCP/IP UDP
Connection-oriented protocol Connectionless protocol
Ensures reliable data transmission No guarantee of delivery
Slower due to error checking and flow control Faster, lightweight communication
Data is received in the same order sent Data may arrive out of order
Used for applications like web, email, file Used for video streaming, gaming, VoIP
transfer
Establishes connection before data transfer No connection setup required
More overhead due to reliability features Less overhead, suitable for real-time use
Example: HTTP, FTP, SMTP (TCP protocols) Example: DNS, DHCP, TFTP (UDP
protocols)
Ans.
After accepting a client connection, a Socket object is created, allowing the server to send and
receive data to/from the client.
The server can handle multiple clients by accepting each client connection one at a time.
You can create separate threads to manage each client connection concurrently.
5. Server-Client Communication -
ServerSocket allows two-way communication, enabling the server to send responses to the
client, making it suitable for applications like chat servers and file transfer.
The ServerSocket binds to a particular port, ensuring that it listens for connections only on
that port, which is important for managing network traffic effectively.
Ans.
The isReachable() method checks if the host is reachable over the network, which is useful
for testing network connectivity before attempting communication.
4. Getting Hostname from IP Address -
InetAddress can handle both IPv4 and IPv6 addresses, making it adaptable to modern
networking protocols.
You can distinguish between them using getHostAddress().
The getHostName() method retrieves the hostname of the machine associated with the IP
address, providing readable details about the connected host.
Ans.
Datagram:
1. DatagramSocket :-
2. DatagramPacket :-
Ans.
Datagram Packets:
A DatagramPacket in Java is used to implement connectionless communication via UDP (User Datagram
Protocol). Unlike TCP, UDP does not establish a connection before data transmission, meaning that
delivery and order of the packets are not guaranteed. Each DatagramPacket carries both the data and
the necessary destination information (IP address and port number).
Key Points -
Constructors of DatagramPacket -
DatagramPacket(byte[] buf, int len): Receives a packet of length len into a byte array buf.
DatagramPacket(byte[] buf, int off, int len): Receives data starting at off for len bytes into
buf.
DatagramPacket(byte[] buf, int len, InetAddress addr, int port): Sends data of length len to
the specified address and port.
DatagramPacket(byte[] buf, int off, int len, InetAddress addr, int port): Sends data with an
offset to the specified address and port.
Methods of DatagramPacket -
1. getData(): Returns the byte array containing the data in the packet.
2. getAddress(): Returns the destination IP address.
3. getPort(): Returns the destination port number.
4. getLength(): Returns the length of valid data in the packet.
Advantages -
Faster Communication: Datagram packets are faster than TCP because they do not require
establishing or maintaining a connection.
Ideal for Real-Time Applications: It is well-suited for scenarios where low latency is critical, and
data loss is tolerable, like live streaming and gaming.
Disadvantages -
Unreliable Delivery: Since UDP does not guarantee packet delivery, the data might be lost,
corrupted, or delivered out of order.
No Flow Control: UDP does not provide any mechanism to control the flow of data between
sender and receiver, potentially causing congestion and packet loss if the network is overloaded.
Ans.
Datagram Server:
A Datagram Server receives data from clients using the UDP protocol, without creating a
connection.
It uses DatagramSocket to listen for incoming packets.
Example –
import java.net.*;
server.close();
Explanation –
o The server opens port 9876 using DatagramSocket to wait for incoming UDP
packets.
o When a packet arrives, it prints the client message and then closes the socket.
Datagram Client:
import java.net.*;
InetAddress ip = InetAddress.getByName("localhost");
client.send(packet);
client.close();
Explanation –
o The client creates a datagram packet with the message "Hello Server" and
sends it to port 9876 on the server.
o It does not wait for a response and immediately closes the socket.