0% found this document useful (0 votes)
14 views6 pages

Java Networking Basics: InetAddress & URL

The document provides an introduction to Java networking, covering key concepts such as the InetAddress and URL classes, which are used to identify computers and web resources respectively. It also explains socket programming, detailing the roles of client and server, and includes examples of TCP socket programming. The document emphasizes the importance of understanding networking fundamentals for tasks like data communication between systems.

Uploaded by

malusatheesh07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Java Networking Basics: InetAddress & URL

The document provides an introduction to Java networking, covering key concepts such as the InetAddress and URL classes, which are used to identify computers and web resources respectively. It also explains socket programming, detailing the roles of client and server, and includes examples of TCP socket programming. The document emphasizes the importance of understanding networking fundamentals for tasks like data communication between systems.

Uploaded by

malusatheesh07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🌐 1.

Introduction to Java Networking


🧠 What is Networking?
Networking means connecting two or more devices to share data or resources (like files, messages, or
videos).
Example:
 Sending a WhatsApp message.
 Accessing a website.
 Watching a YouTube video.
Java provides a powerful [Link] package to handle networking tasks such as:
 Identifying computers (by IP or hostname)
 Sending and receiving data
 Communicating using TCP and UDP protocols

🌍 2. InetAddress Class
🧠 Purpose:
The InetAddress class represents an Internet Protocol (IP) address — the identity of a computer on a
network.
When two devices communicate, they use IP addresses (like [Link] or [Link]).

🏗️ Where It Belongs:
Package → [Link]
Commonly used methods:
Method Description
getLocalHost() Returns IP address of your own computer
getByName(String host) Returns IP address of a given hostname (like [Link])
getHostName() Returns the hostname of the system
getHostAddress() Returns the IP address in string form

💻 Example:
import [Link].*;

public class InetAddressExample {


public static void main(String[] args) throws UnknownHostException {
// Get your local system details
InetAddress local = [Link]();
[Link]("Local Host Name: " + [Link]());
[Link]("Local IP Address: " + [Link]());

// Get details of a website


InetAddress website = [Link]("[Link]");
[Link]("Website Host Name: " + [Link]());
[Link]("Website IP Address: " + [Link]());
}
}

🧠 Expected Output:
Local Host Name: Lizy-PC
Local IP Address: [Link]
Website Host Name: [Link]
Website IP Address: [Link]

🧠 Explanation:
1. InetAddress local = [Link]();
→ Finds your own computer’s IP address and name.
2. getHostName()
→ Returns your computer’s name on the network (like “Lizy-PC”).
3. getHostAddress()
→ Returns your IP address in numeric format.
4. [Link]("[Link]")
→ Java internally contacts the DNS (Domain Name System) to find Google’s IP address.
💡 Analogy:
Just like how your phone needs a contact number to call a friend, your computer needs an IP address to
contact another computer.

🧠 Discussion Points / Questions for Students:


 What is the difference between a Host Name and an IP Address?
→ Host name is human-readable ([Link]), IP address is numeric ([Link]).
 How does Java resolve a hostname to IP?
→ Using DNS lookup internally.

🌎 3. URL Class
🧠 Purpose:
URL stands for Uniform Resource Locator.
It represents a web address that points to a resource (like a web page, image, or video) on the Internet.

🏗️ Where It Belongs:
Package → [Link]
Syntax:
URL url = new URL("[Link]

🔍 Parts of a URL:
Part Meaning Example
Protocol Communication method https
Host Website name [Link]
Port Communication channel 443
File Path to resource /docs/[Link]

💻 Example Program:
import [Link].*;

public class URLExample {


public static void main(String[] args) throws MalformedURLException {
URL url = new URL("[Link]

[Link]("Protocol: " + [Link]());


[Link]("Host: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("File: " + [Link]());
}
}

🧠 Output:
Protocol: https
Host: [Link]
Port: 443
File: /docs/[Link]

🧠 Explanation:
1. Protocol → tells how to communicate (http, https, ftp, etc.)
2. Host → domain name of the website.
3. Port → like a "door" used for communication; HTTPS uses 443, HTTP uses 80.
4. File → path or file you are trying to access on the server.
💡 Analogy:
Think of a URL as a complete address —
Protocol = road type, Host = building name, Port = door number, File = room name.

🧠 Discussion Questions:
 What happens if you omit the port number?
→ Java automatically uses the default port for that protocol (80 for HTTP, 443 for HTTPS).
 Can two URLs have the same host but different ports?
→ Yes — they can point to different services running on the same server.
✅ Key Note
 InetAddress → identifies computers using IP addresses.
 URL → identifies web resources using web addresses.
These form the foundation for upcoming topics like Socket Programming (TCP & UDP), where
they’ll actually send data between systems.

💡 1. What Is Socket Programming?


🧠 Concept:
Socket programming allows two computers (or two Java programs) to communicate over a network (like
LAN or Internet).
A socket is an endpoint for communication between two machines.
Think of it like:
 📱 Client = person who sends a message
 💻 Server = person who receives and replies

🧠 2. Key Terms
Term Meaning
Socket Endpoint for sending/receiving data
ServerSocket Used only by server to listen for client requests
Client Program that connects to the server
Server Program that waits for and accepts connections
Port Number A unique number used to identify the application on a device (e.g., 8080)

⚙️ 3. Java Classes Used


From package [Link]:
Class Purpose
ServerSocket Creates a server that listens on a specific port
Socket Used by both client and server to send/receive data
InputStream Reads data from a socket
OutputStream Sends data to a socket

🧠 4. TCP vs UDP
Feature TCP UDP
Type Connection-oriented Connectionless
Reliability Reliable (guarantees delivery) Not guaranteed
Speed Slower Faster
Example WhatsApp messages Online games, video streaming
We’ll focus first on TCP, since it’s easier to understand (like a telephone call).

💻 5. TCP Socket Programming Example


Let’s make a simple client–server chat where:
 The Server waits for a message.
 The Client connects and sends a message.

🖥️ Server Program ([Link])


import [Link].*;
import [Link].*;

public class TCPServer {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5000); // Server listens on port 5000
[Link]("Server started. Waiting for client...");

Socket socket = [Link](); // Wait for client connection


[Link]("Client connected!");

// Create input and output streams


BufferedReader input = new BufferedReader(new InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](), true);

String clientMsg = [Link]();


[Link]("Client says: " + clientMsg);

// Send reply to client


[Link]("Hello Client, message received!");

// Close connections
[Link]();
[Link]();
[Link]();
[Link]();
}
}
💻 Client Program ([Link])
import [Link].*;
import [Link].*;

public class TCPClient {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 5000); // Connect to server running on same system
[Link]("Connected to server!");

// Create input and output streams


BufferedReader input = new BufferedReader(new InputStreamReader([Link]));
PrintWriter output = new PrintWriter([Link](), true);
BufferedReader serverInput = new BufferedReader(new InputStreamReader([Link]()));

[Link]("Enter message for server: ");


String message = [Link]();
[Link](message);

// Read reply from server


String reply = [Link]();
[Link]("Server replied: " + reply);

[Link]();
}
}

⚙️ How to Run (in Lab/Class):


1. Open two terminal windows (or two command prompts).
2. First, compile both programs:
3. javac [Link]
4. javac [Link]
5. Run the Server first:
6. java TCPServer
7. Then run the Client in another window:
8. java TCPClient

🧠 Expected Output:
Server Side:
Server started. Waiting for client...
Client connected!
Client says: Hello Server!
Client Side:
Connected to server!
Enter message for server: Hello Server!
Server replied: Hello Client, message received!

🧠 6. Explanation (Line by Line)


Step Code Explanation
1 new ServerSocket(5000) Creates a server that listens on port 5000
2 accept() Waits for a client to connect
Step Code Explanation
3 new Socket("localhost", 5000) Connects client to server running locally
4 getInputStream() / getOutputStream() Used to send and receive messages
5 PrintWriter and BufferedReader Used for text communication
6 [Link]() Sends data
7 [Link]() Reads data sent by the other side

🧠 7. Real-Life Analogy
Imagine:
 📞 ServerSocket = a telephone that is waiting for calls.
 📞 Socket (Client) = a person dialing the number.
 When connected → they can talk (send and receive messages).

You might also like