Java Networking की मूल बातें
In this page:
What is an IP Address and URL?
java.net package में Java में network communication की core classes शामिल हैं। InetAddress IP addresses (IPv4 और IPv6 दोनों) को represent करता है, और URL class एक Uniform Resource Locator को represent करता है, raw strings में हेरफेर करने के बजाय network addresses के साथ काम करने का एक structured तरीका देते हुए।
उदाहरण: What is an IP Address and URL?
// Import java.net.InetAddress so it can be used by its short name
import java.net.InetAddress;
// Import java.net.URL so it can be used by its short name
import java.net.URL;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("127.0.0.1");
// Create a new URL object and store it in url
URL url = new URL("https://example.com/page");
// Print a line to the console
System.out.println(address.getHostAddress() + " " + url.getHost());
}
}
Login to try C/C++/Java/PHP code in the editor
Reading from a URL Connection
URLConnection class आपको किसी URL से communication link खोलने और body पढ़ने से पहले headers जैसी metadata inspect करने देती है। आप getInputStream() का उपयोग करके किसी page की raw byte stream खींच सकते हैं, जो वह lower-level building block है जिस पर higher-level HTTP clients बने हैं।
उदाहरण: Reading from a URL Connection
// Import java.net.URLConnection so it can be used by its short name
import java.net.URLConnection;
// Import java.io.* so it can be used by its short name
import java.io.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws Exception {
// Create a new File object and store it in file
File file = new File("page.txt");
// Try code that might throw an exception
try (PrintWriter out = new PrintWriter(file)) { out.print("sample content"); }
URLConnection conn = file.toURI().toURL().openConnection();
// Try code that might throw an exception
try (InputStream in = conn.getInputStream()) {
// Print a line to the console
System.out.println(new String(in.readAllBytes()));
}
file.delete();
}
}
Login to try C/C++/Java/PHP code in the editor
URI vs URL in Java 11+
एक URI एक abstract resource identifier को represent करता है (इसका resolvable होना ज़रूरी नहीं), जबकि एक URL एक specific, locatable resource है। Java 11 से, recommended pattern यह है कि पहले एक URI बनाएं और सिर्फ connection खोलने की ज़रूरत पड़ने पर ही इसे toURL() से URL में convert करें, क्योंकि URI का constructor syntax को ज़्यादा सख्ती से validate करता है और कुछ legacy URL pitfalls से बचता है।
उदाहरण: URI vs URL in Java 11+
import java.net.URI;
import java.net.URL;
public class Main {
public static void main(String[] args) throws Exception {
URI uri = new URI("https://example.com/page"); // validated more strictly
URL url = uri.toURL(); // converted only when actually needed
System.out.println(uri + " -> " + url);
}
}
Login to try C/C++/Java/PHP code in the editor
Network Protocols Overview
Java ज़्यादातर low-level protocol details को application code से दूर रखता है: Sockets reliable, ordered delivery के लिए TCP पर चलते हैं, और DatagramSockets lightweight, connectionless messaging के लिए UDP पर चलते हैं। यह जानना कि कोई दिया गया API किस protocol के ऊपर बैठा है, यह समझने में मदद करता है कि delivery की गारंटी है या नहीं और कितना overhead उम्मीद करें।
उदाहरण: Network Protocols Overview
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
Socket tcpSocket = new Socket(); // TCP -- reliable, ordered
DatagramSocket udpSocket = new DatagramSocket(); // UDP -- lightweight, connectionless
System.out.println("TCP and UDP sockets created");
udpSocket.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Hostname Resolution
आप किसी domain name को उसके underlying IP address (या addresses, क्योंकि एक नाम कई से map हो सकता है) में resolve करने के लिए InetAddress class के getByName() और getAllByName() methods का उपयोग कर सकते हैं। यह वही DNS lookup step है जो आपका browser किसी server से socket खोलने से पहले करता है।
उदाहरण: Hostname Resolution
import java.net.InetAddress;
public class Main {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("localhost");
System.out.println(address.getHostAddress()); // same DNS-style lookup a browser performs
}
}
Login to try C/C++/Java/PHP code in the editor
- नए code में
new URL(...)का उपयोग करना, जो Java 20 से deprecated है;URI.create(...).toURL()का उपयोग करें। - hostname resolve करते समय
UnknownHostExceptionको handle न करना, जो तब होता है जब नाम मौजूद न हो या कोई network न हो। - connect और read timeouts सेट किए बिना connection खोलना, जिससे program लंबे समय तक hang हो सकता है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: