EXPERIMENT NO: 2
● Program For INET Adresses:
PROGRAM:
import [Link].*;
public class InetAddressExample {
public static void main(String[] args) {
try { String host = "[Link]";
InetAddress address = [Link](host);
[Link]("IP Address of " + host + ": " +
[Link]());
InetAddress localHost = [Link]();
[Link]("Local Host Name: " + [Link]());
[Link]("Local Host Address: " +
[Link]());
InetAddress[] allAddresses = [Link](host);
[Link]("All IP Addresses of " + host + ":");
for (InetAddress addr : allAddresses) {
[Link]([Link]());
}
} catch (UnknownHostException e) {
[Link]("Host not found: " + [Link]());
}
}
}
OUTPUT:
● Program For URL :
PROGRAM:
import [Link].*;
public class URL{
public static void main(String[] args) {
try {
URL url = new
URL("[Link]
[Link]("URL: " + url);
[Link]("Protocol: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("Path: " + [Link]());
[Link]("Query: " + [Link]());
[Link]("Ref (Fragment): " + [Link]());
} catch (MalformedURLException e) {
[Link]();
}
}
}
OUTPUT:
● Program for URI :
Program:
import [Link].*;
public class URI {
public static void main(String[] args) {
try {
URI uri = new
URI("[Link]
nt");
[Link]("URI: " + uri);
[Link]("Scheme: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("Path: " + [Link]());
[Link]("Query: " + [Link]());
[Link]("Fragment: " + [Link]());
} catch (URISyntaxException e) {
[Link]();
}
}
}
OUTPUT:
● Program for I/O Stream:
PROGRAM:
import [Link].*;
public class IOStreamExample {
public static void main(String[] args) {
String fileName = "[Link]";
// 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!";
[Link]([Link]());
[Link]("Data written to file successfully.");
} catch (IOException e) {
[Link]();
}
// Reading data from a file using FileInputStream
try (FileInputStream fis = new FileInputStream(fileName)) {
[Link]("\nReading from file:");
int ch;
while ((ch = [Link]()) != -1) {
[Link]((char) ch);
}
} catch (IOException e) {
[Link]();
}
}
}
OUTPUT:
● Program for client and server :
Client Program:
import [Link].";
import java.ίο.;
public class client{
public static void main(String[] args) throws IOException
Socket s = new Socket("localhost", 4999);
PrintWriter pr = new PrintWriter([Link]())
[Link]("is it working");
[Link]();
InputStreamReader in new InputStreamReader([Link]());
BufferedReader bf = new BufferedReader(in);
String str = [Link]();
[Link]("server: "+str);
Server Program:
import [Link].";
import java.ίο.;
public class server{
public static void main(String[] args) throws IOException
ServerSocket ss = new ServerSocket(4999);
Socket s = [Link]();
[Link]("client connected");
InputStreamReader in = new InputStreamReader([Link]());
BufferedReader bf = new BufferedReader(in);
String str = [Link]();
[Link]("client: "+str);
PrintWriter pr = new PrintWriter([Link]())
[Link]("yooo");
[Link]();
OUTPUT: