Network Programming Lab Report
Network Programming Lab Report
LAB REPORT ON
Submitted to:
Bhadrapur, Jhapa
Computer Application
Submitted By:
Karna Tamang
TABLE OF CONTENTS
[Link] a program that finds the address of the local machine. ....................................... 1
[Link] a program that find the canonical hostname of a given address. ...................... 2
[Link] a program to find the IP address and host name of the local machine. .............. 2
[Link] a program to get IPV4 and IPV6 address of a given web address. ..................... 3
[Link] a program for Determining whether an IP address is IPV4 and IPV6. ............... 3
[Link] a program that splits the parts of a URL (Splitting URL into pieces information.
.......................................................................................................................................... 4
8. Write a program that checks the which protocols does a virtual machine support or
Not? .................................................................................................................................. 5
12. Write a program that communicate with Server- side program through GET. ......... 9
13. Write a program that shows a simple CookiePolicy that blocks cookies from .gov
domains. ......................................................................................................................... 11
19. Write a program to get the time when a URL was last changed.............................. 19
27. Write a program to input two numbers and calculate addition of two numbers by
using client and server RMI ........................................................................................... 28
The following fragment connect to the daytime server on port13 of the [Link]
and display the data it’s sent timeClient ........................................................................ 29
} catch (Exception e) {
// Handle cases where the local host cannot be resolved
[Link]("Error: " + e);
}
}
}
1
[Link] a program that find the canonical hostname of a given address.
import [Link];
} catch (Exception e) {
// Catches UnknownHostException if the URL is wrong or internet is down
[Link]("Error: " + e);
}
}
}
[Link] a program to find the IP address and host name of the local machine.
import [Link];
public class Main {
public static void main(String[] args) {
try {
// Get the local host details
InetAddress localHost = [Link]();
// Print the computer's name and its network IP
[Link]("Host Name: " + [Link]());
[Link]("IP Address: " + [Link]());
} catch (Exception e) {
// Handle cases where the network card is disabled or hostname is invalid
[Link]("Error: " + e);
}
2
}
}
[Link] a program to get IPV4 and IPV6 address of a given web address.
import [Link];
} catch (Exception e) {
// Catches UnknownHostException if the site is down or name is invalid
[Link]("Error: " + e);
}
}
}
[Link] a program for Determining whether an IP address is IPV4 and IPV6.
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
3
Scanner sc = new Scanner([Link]);
[Link]("Enter an IP address: ");
String ip = [Link]();
try {
// Convert string input into an InetAddress object
InetAddress address = [Link](ip);
// Check for ":" to identify IPv6 or "." for IPv4
if ([Link]().contains(":")) {
[Link](ip + " is an IPv6 address.");
} else if ([Link]().contains(".")) {
[Link](ip + " is an IPv4 address.");
}
} catch (Exception e) {
// This triggers if the input isn't a valid IP or host
[Link]("Invalid IP address.");
} finally {
[Link]();
}
}
}
[Link] a program that splits the parts of a URL (Splitting URL into pieces
information.
import [Link];
4
[Link]("Protocol : " + [Link]());
[Link]("Host Name : " + [Link]());
[Link]("Port Number : " + [Link]());
[Link]("Path : " + [Link]());
[Link]("File : " + [Link]());
[Link]("Query : " + [Link]());
[Link]("Reference : " + [Link]());
} catch (Exception e) {
// Triggers if the URL string is malformed
[Link]("Invalid URL");
}
}
}
8. Write a program that checks the which protocols does a virtual machine support
or Not?
import [Link];
import [Link];
import [Link];
import [Link];
5
}
checkTCP(testPort);
checkUDP(testPort);
}
}
[Link] a program to download a web page of a given address.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
6
new InputStreamReader([Link]())
);
String line;
[Link]("Downloading content from: " + webAddress + "\n");
7
"../images/[Link]", // Go up one level, then into images
"./[Link]" // Explicitly stay in current directory
};
} catch (MalformedURLException e) {
[Link]("Error: " + [Link]());
}
}
}
8
try (BufferedInputStream in = new BufferedInputStream(new
URL(fileURL).openStream());
FileOutputStream out = new FileOutputStream(saveAs)) {
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
12. Write a program that communicate with Server- side program through GET.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
9
// 2. Open the connection
HttpURLConnection connection = (HttpURLConnection) [Link]();
String inputLine;
StringBuilder response = new StringBuilder();
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
13. Write a program that shows a simple CookiePolicy that blocks cookies from .gov
domains.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
11
};
// 3. Make this the default manager for all future network connections
[Link](cookieManager);
try {
// 4. Create example URIs and cookies for testing
URI govURI = new URI("[Link]
URI eduURI = new URI("[Link]
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
15. Write a program to read value of HTTP header Fields.
import [Link];
import [Link];
import [Link];
import [Link];
14
Map<String, List<String>> headerFields = [Link]();
// Note: The 'null' key usually contains the HTTP Status Line
[Link]((headerName == null ? "Status Line" : headerName) + " : " +
headerValues);
}
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
16. Write a program to print the entire HTTP header.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
15
// 2. Open HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) [Link]();
// 4. Establish connection
[Link]();
// If headerName is null, it represents the Status Line (e.g., HTTP/1.1 200 OK)
String displayName = (headerName == null) ? "Status-Line" : headerName;
[Link](displayName + " : " + headerValues);
}
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
16
17. Write a program for HTTP Request Methods.
import [Link];
import [Link];
import [Link];
} catch (Exception e) {
[Link]("Error (" + method + "): " + [Link]());
}
}
}
18. Write a program to print the URL of a URLConnection to "[Link]"
import [Link];
import [Link];
18
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
19. Write a program to get the time when a URL was last changed.
import [Link];
import [Link];
import [Link];
import [Link];
// 2. Open connection
URLConnection connection = [Link]();
if (lastModified == 0) {
// Some servers disable this header for security or dynamic content
[Link]("The server did not provide Last-Modified information.");
} else {
// 4. Convert the 'long' timestamp into a readable Date object
Date lastModifiedDate = new Date(lastModified);
19
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy
HH:mm:ss z");
20
// 2. Read and print the raw response (Headers + HTML)
[Link]("Response from server:\n");
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
21. Write a program writing from servers with socket.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
21
// 2. Construct the HTTP POST request manually
[Link]("POST /post HTTP/1.1");
[Link]("Host: " + server);
[Link]("User-Agent: Java Socket Client");
[Link]("Content-Type: application/x-www-form-urlencoded");
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
22. Write a program socket to read Time Client.
import [Link];
import [Link];
import [Link];
22
public class TimeClient {
public static void main(String[] args) {
// public-facing server provided by NIST
String server = "[Link]";
// Port 13 is the standard port for the Daytime Protocol
int port = 13;
// The server sends one or more lines of text and then disconnects
String time;
while ((time = [Link]()) != null) {
if ([Link]() > 0) {
[Link](time);
}
}
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
23. Write a program socket to Low Port Scanner.
import [Link];
23
[Link]("Scanning low ports on: " + host);
[Link]("Checking Ports 1 to 1024...\n");
24
[Link]("=== Socket Information ===");
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
25. Write a program socket for a server.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
25
[Link]("Waiting for client connection...");
String clientMessage;
// 3. Keep reading until the client stops sending data
while ((clientMessage = [Link]()) != null) {
[Link]("Client says: " + clientMessage);
// 5. Termination condition
if ([Link]("bye")) {
[Link]("Client disconnected.");
break;
}
}
[Link]();
[Link]("Server stopped.");
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
26
26. Write a program for Secure Socket with – [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
try {
// 1. Obtain the default SSLSocketFactory
SSLSocketFactory factory = (SSLSocketFactory) [Link]();
27
[Link]("\nResponse from server:\n");
// 5. Cleanup
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]("Security Error: " + [Link]());
}
}
}
27. Write a program to input two numbers and calculate addition of two numbers by
using client and server RMI
import [Link];
import [Link];
import [Link];
28
return a + b;
}
} catch (Exception e) {
[Link]("Server exception: " + [Link]());
[Link]();
}
}
}
[Link] a program for reading input from a Socket:
The following fragment connect to the daytime server on port13 of the [Link]
and display the data it’s sent timeClient
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
29
try (Socket socket = new Socket(server, port);
// Explicitly use ASCII encoding as per NIST protocol standards
BufferedReader br = new BufferedReader(new
InputStreamReader([Link](), "ASCII"))) {
String line;
while ((line = [Link]()) != null) {
// Skip the blank line often sent at the start of NIST responses
if ([Link]().isEmpty()) continue;
if ([Link] >= 3) {
String datePart = parts[1]; // Index 1: yy-MM-dd
String timePart = parts[2]; // Index 2: HH:mm:ss
String dateTimeStr = datePart + " " + timePart;
try {
// 3. Convert String -> Date Object
Date nistDate = [Link](dateTimeStr);
[Link]("Parsed NIST Time: " + nistDate);
} catch (ParseException pe) {
[Link]("Could not parse NIST time.");
}
}
}
30
// 4. Comparison with local clock
[Link]("Local System Time: " + new Date());
} catch (IOException e) {
[Link]("Connection error: " + e);
}
}
}
[Link] a program in java for getting a socket’s information.
import [Link];
import [Link];
import [Link];
import [Link];
31
[Link]("Port scan completed.");
} catch (UnknownHostException e) {
[Link]("Cannot resolve local host: " + e);
}
}
}
30. Write a program for Reading Data with a ServerSocket:
(Server Program)
import [Link].*;
import [Link].*;
[Link]("Hello, I am server\n");
[Link](); // Ensure data is actually sent over the wire
32
[Link](e);
}
}
}
(Client Program)
import [Link].*;
import [Link].*;
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
[Link] a program in java to display Date and time
import [Link].*;
import [Link].*;
import [Link];
33
public final static int PORT = 2430;
34
import [Link].*;
import [Link];
while (true) {
// 1. Listen for a new connection
Socket clientSocket = [Link]();
[Link]("Client connected: " + [Link]());
DaytimeThread(Socket connection) {
[Link] = connection;
}
35
@Override
public void run() {
try (Writer out = new BufferedWriter(new
OutputStreamWriter([Link]()))) {
[Link]("I am a multi-threaded server:\n");
Date now = new Date();
[Link]("Date and time: " + [Link]() + "\n");
[Link]();
} catch (IOException e) {
[Link]("Error with client " + [Link]() + ": " +
[Link]());
} finally {
try {
[Link]();
} catch (IOException e) {
[Link]("Error closing socket: " + [Link]());
}
}
}
}
}
36