Networking
Networking
Networking
np
Code
import java.net.*;
try {
Output
2. Write a program that finds the address of the local machine.
Code
import java.net.*;
try {
System.out.println(address);
Output
3. Write a program that finds the canonical hostname of a given the
address.
Code
import java.net.*;
InetAddress ia = InetAddress.getByName("182.93.84.136");
System.out.println(ia.getCanonicalHostName());
Output
4. Write a program that lists all the network interfaces.
Code
import java.net.*;
import java.util.*;
Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
System.out.println(ni);
Output
5. Write a program to check remote is reachable or not [Testing
Reachability].
Code
import java.net.*;
try {
if (net.isReachable(100)) {
System.out.println("Success");
} else {
System.out.println("Failed");
} catch (Exception e) {
e.printStackTrace();
Output
6. Write a program to demonstrate the SpamCheck.
Code
import java.net.*;
public class SpamCheck { // Corrected the class name to start with an uppercase
letter
try {
} catch (Exception e) {
System.out.println("Spam");
try {
return true;
} catch (UnknownHostException e) {
return false;
}
Output
7. Write a program to checks the which protocols does a virtual
machine support or not?
Code
import java.net.*;
testProtocol("http://www.adc.org");
testProtocol("https://www.amazon.com/exec/obidos/order2/");
testProtocol("ftp://ibiblio.org/pub/languages/java/javafaq");
testProtocol("mailto:elharo@ibiblio.org");
// Telnet
testProtocol("telnet://dibner.poly.edu/");
testProtocol("file:///etc/passwd");
// Gopher
testProtocol("gopher://gopher.anc.org.za/");
try {
Output
8. Write a program for resolving relatives URI.
Code
import java.net.URI;
Output
9. Write a program to implement the CookieStore Methods (add, read,
delete) cookies.
Code
import java.io.*;
import java.net.*;
import java.util.*;
cookieStore.add(uri, cookieA);
cookieStore.add(uri, cookieB);
// Method 3 - getCookies()
// Method 4 - getURIs()
// Method 6 - removeAll()
}
Output
10. Write a program to download a web page by using
URLConnection.
Code
import java.io.*;
import java.net.*;
try {
URLConnection uc = u.openConnection();
int c;
System.out.print((char) c);
System.err.println(ex);
Output
11. Write a program to print the entire HTTP header.
Code
import java.io.*;
import java.net.*;
try {
URLConnection uc = u.openConnection();
if (header == null) {
break;
if (headerKey == null) {
System.err.println(ex);
Output
12. Write a program for HTTP request method.
Code
import java.io.*;
import java.net.*;
import java.util.*;
try {
http.setRequestMethod("HEAD");
System.err.println(ex);
System.out.println();
}
Output
12. Write a program to print the URL or URL Connection to
“hafoss.edu.np”.
Code
import java.io.*;
import java.net.*;
try {
URLConnection uc = u.openConnection();
System.out.println(uc.getURL());
System.err.println(ex);
Output
13. Write a program socket to client.
Code
import java.net.*;
import java.io.*;
public class ClientChat {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 8888);
System.out.println("Server Connected : " + s);
do {
str = br.readLine();
dout.writeUTF(str);
dout.flush();
System.out.println("Server Message: " + din.readUTF());
} while (!str.equals("stop"));
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output
14. Write a program for Creating Secure Sockets with tufhoss.edu.np
Code
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
try {
socket.setEnabledCipherSuites(supported);
out.write("GET / HTTP/1.1\r\n");
out.write("Host: tufohss.edu.np\r\n");
out.write("\r\n");
out.flush();
String line;
System.out.println(line);
socket.close();
} catch (IOException e) {
e.printStackTrace();
Output
15. Write a program to implement the concept on Data Conversion.
Code
import java.nio.ByteBuffer;
import java.nio.BufferUnderflowException;
int capacity = 8;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.asIntBuffer()
.put(10)
.put(20);
bb.rewind();
System.out.println("Original ByteBuffer:");
bb.rewind();
} catch (BufferUnderflowException e) {
Output
16. Write a program for UDP Client example.
Code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
class UDPClient {
byte[] sendData;
sendData = sentence.getBytes();
clientSocket.send(sendPacket);
clientSocket.close();
Output
17. Write a program for UDP Server example.
Code
import java.net.*;
import java.io.*;
byte[] sendData;
while (true) {
serverSocket.receive(receivePacket);
sendData = sentence.getBytes();
serverSocket.send(sendPacket);
}
}
Output
17. Write a program to verify that you are receiving multicast data at
a particular host.
Code
MultiSocketServer.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
serverSocket.send(msgPacket);
ex.printStackTrace();
}
}
MultiSocketClient.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
while (true) {
ex.printStackTrace();
Output
18. Write a program to add two numbers using RMI.
Code
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
ImplExample.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
super();
return x + y;
}
ServerRMI.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public Server() {}
try {
registry.bind("RMITest", skeleton);
System.err.println("Server ready");
} catch (Exception e) {
e.printStackTrace();
ClientRMI.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
private ClientRMI() {}
try {
} catch (Exception e) {
e.printStackTrace();
Output