← Back to Advanced Java Course | Chapter 7: Networking & APIs | Lesson 1 of 4

Java Networking की मूल बातें

Networking basics यह है कि programs addresses का उपयोग करके दूसरे computers को कैसे ढूंढते और उनसे बात करते हैं, जैसे किसी घर को चिट्ठियां भेजना। Java addresses lookup कर सकता है और web pages से पढ़ सकता है।

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?

java
// 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());
	}
}

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

java
// 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();
	}
}

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+

java
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);
	}
}

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

java
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();
	}
}

Hostname Resolution

आप किसी domain name को उसके underlying IP address (या addresses, क्योंकि एक नाम कई से map हो सकता है) में resolve करने के लिए InetAddress class के getByName() और getAllByName() methods का उपयोग कर सकते हैं। यह वही DNS lookup step है जो आपका browser किसी server से socket खोलने से पहले करता है।

उदाहरण: Hostname Resolution

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. नए code में new URL(...) का उपयोग करना, जो Java 20 से deprecated है; URI.create(...).toURL() का उपयोग करें।
  2. hostname resolve करते समय UnknownHostException को handle न करना, जो तब होता है जब नाम मौजूद न हो या कोई network न हो।
  3. connect और read timeouts सेट किए बिना connection खोलना, जिससे program लंबे समय तक hang हो सकता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.