KEMBAR78
Ex.2 Java | PDF | Uniform Resource Identifier | Filename
0% found this document useful (0 votes)
8 views6 pages

Ex.2 Java

The document contains multiple Java programs demonstrating networking concepts, including obtaining IP addresses using InetAddress, parsing URLs and URIs, and performing file I/O operations. It also includes client-server communication examples using sockets. Each section provides code snippets along with expected outputs for clarity.

Uploaded by

vedanttopare5
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)
8 views6 pages

Ex.2 Java

The document contains multiple Java programs demonstrating networking concepts, including obtaining IP addresses using InetAddress, parsing URLs and URIs, and performing file I/O operations. It also includes client-server communication examples using sockets. Each section provides code snippets along with expected outputs for clarity.

Uploaded by

vedanttopare5
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
You are on page 1/ 6

EXPERIMENT NO: 2

●​ Program For INET Adresses:

PROGRAM:

import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try { String host = "www.google.com";
InetAddress address = InetAddress.getByName(host);
System.out.println("IP Address of " + host + ": " +
address.getHostAddress());

InetAddress localHost = InetAddress.getLocalHost();


System.out.println("Local Host Name: " + localHost.getHostName());
System.out.println("Local Host Address: " +
localHost.getHostAddress());

InetAddress[] allAddresses = InetAddress.getAllByName(host);


System.out.println("All IP Addresses of " + host + ":");
for (InetAddress addr : allAddresses) {
System.out.println(addr.getHostAddress());
}

} catch (UnknownHostException e) {
System.out.println("Host not found: " + e.getMessage());
}
}
}

OUTPUT:
●​ Program For URL :

PROGRAM:
import java.net.*;

public class URL{


public static void main(String[] args) {
try {
URL url = new
URL("https://www.example.com:8080/path/to/page?search=query#section1");

System.out.println("URL: " + url);


System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("Path: " + url.getPath());
System.out.println("Query: " + url.getQuery());
System.out.println("Ref (Fragment): " + url.getRef());

} catch (MalformedURLException e) {
e.printStackTrace();

}
}
}

OUTPUT:
●​ Program for URI :

Program:

import java.net.*;

public class URI {


public static void main(String[] args) {
try {
URI uri = new
URI("https://www.example.com:8080/docs/resource.html?search=test#fragme
nt");

System.out.println("URI: " + uri);


System.out.println("Scheme: " + uri.getScheme());
System.out.println("Host: " + uri.getHost());
System.out.println("Port: " + uri.getPort());
System.out.println("Path: " + uri.getPath());
System.out.println("Query: " + uri.getQuery());
System.out.println("Fragment: " + uri.getFragment());

} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}

OUTPUT:
●​ Program for I/O Stream:

PROGRAM:

import java.io.*;

public class IOStreamExample {


public static void main(String[] args) {
String fileName = "example.txt";

// Writing data to a file using FileOutputStream


try (FileOutputStream fos = new FileOutputStream(fileName)) {
String content = "Hello, this is a test for Java I/O streams!";
fos.write(content.getBytes());
System.out.println("Data written to file successfully.");
} catch (IOException e) {
e.printStackTrace();
}

// Reading data from a file using FileInputStream


try (FileInputStream fis = new FileInputStream(fileName)) {
System.out.println("\nReading from file:");
int ch;
while ((ch = fis.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

OUTPUT:
●​ Program for client and server :

Client Program:

import java.net.";

import java.ίο.;

public class client{

public static void main(String[] args) throws IOException

Socket s = new Socket("localhost", 4999);

PrintWriter pr = new PrintWriter(s.getOutputStream())

pr.println("is it working");

pr.flush();

InputStreamReader in new InputStreamReader(s.getInputStream());

BufferedReader bf = new BufferedReader(in);

String str = bf.readLine();

System.out.println("server: "+str);

Server Program:
import java.net.";

import java.ίο.;

public class server{

public static void main(String[] args) throws IOException


ServerSocket ss = new ServerSocket(4999);

Socket s = ss.accept();

System.out.println("client connected");

InputStreamReader in = new InputStreamReader(s.getInputStream());

BufferedReader bf = new BufferedReader(in);

String str = bf.readLine();

System.out.println("client: "+str);

PrintWriter pr = new PrintWriter(s.getOutputStream())

pr.println("yooo");

pr.flush();

OUTPUT:

You might also like