Network Programming Lab
Network Programming Lab
Maitidevi, Kathmandu
Contents
LAB 1 – write a program to retrieve host name ip address of local machine...........................3
LAB 2 -write a java program to check loopback address, private address , multicast address,
any local address...........................................................................................................................4
LAB 3 – Write a java program to split different components of url from given url................7
Lab 4: Write a java program to find baseurl,relativeurl and resolvedurl from given url
https://samriddhicollege.edu.np/wp-content/uploads/2019/09/Networking_Programming-
Syllabus.zip....................................................................................................................................8
Lab 5:Write a java program to get different parts of URI in given URI=
http://www.xml.com/pub/a/2003/09/17/stax.html#id=_hbc........................................................9
LAB 6 -Write a java program to fetch website content using URLConnectionClass............11
Lab 7: Write a program to handle HTTP cookies in Java using the CookieManager and
HttpCookie classes also retrieve and display cookie information from a specified URL.......13
Lab 8: Write a java program to manage HTTP cookies using the CookieStore and HttpCookie
classes also add, retrieve, and remove cookies from a CookieStore........................................15
LAB 9 – Write a program to fetch all HTTP header Fields using URLConnection..............18
Lab 10: Write a java program to perform url encoding and decoding...................................20
LAB 11- Write a program to read data from server using socket...........................................22
LAB 12 – Write a java program to write data to server using socket.....................................25
LAB 13 – write a java program to serve binary data using socket..........................................28
LAB 14 – Write a Simple UDP java program to send data from client to server..................29
LAB 15- Write a simple chat client server application using nio.............................................31
Source code
import java.net.InetAddress;
import java.net.UnknownHostException;
Output
Source code
import java.net.InetAddress;
import java.net.UnknownHostException;
Output
LAB 3 – Write a java program to split different components of
url from given url
import java.net.MalformedURLException;
import java.net.URL;
Output
Lab 4: Write a java program to find baseurl,relativeurl and
resolvedurl from given url https://samriddhicollege.edu.np/wp-
content/uploads/2019/09/Networking_Programming-Syllabus.zip
import java.net.MalformedURLException;
import java.net.URL;
Output
Lab 5:Write a java program to get different parts of URI in given URI=
http://www.xml.com/pub/a/2003/09/17/stax.html#id=_hbc
import java.net.*;
public class Lab5
{
public static void main(String[] args) throws Exception {
String str ="http://www.xml.com/pub/a/2003/09/17/stax.html#id=_hbc";
URI uri = URI.create(str);
System.out.println(uri.normalize().toString());
System.out.println("Scheme = " + uri.getScheme());
System.out.println("Schemespecificpart = "+ uri.getSchemeSpecificPart());
System.out.println("Raw User Info = " + uri.getFragment());
System.out.println("User Info = " + uri.getUserInfo());
System.out.println("Authority = " + uri.getAuthority());
System.out.println("Host = " + uri.getHost());
System.out.println("Path = " + uri.getPath());
System.out.println("Port = " + uri.getPort());
System.out.println("Query = " + uri.getQuery());
}
}
Output
LAB 6 -Write a java program to fetch website content using
URLConnectionClass
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
try {
// Create a URL object
URL url = new URL(urlString);
Output
Lab 7: Write a program to handle HTTP cookies in Java using the
CookieManager and HttpCookie classes also retrieve and display cookie
information from a specified URL.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
System.out.println(response.toString());
} catch (MalformedURLException e) {
System.out.println("MalformedURLException: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
Output
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.util.List;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
LAB 9 – Write a program to fetch all HTTP header Fields using
URLConnection
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
Output
Lab 10: Write a java program to perform url encoding and
decoding.
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;
try {
// URL Encoding
String encodedString = URLEncoder.encode(originalString, charset);
System.out.println("Encoded String: " + encodedString);
// URL Decoding
String decodedString = URLDecoder.decode(encodedString, charset);
System.out.println("Decoded String: " + decodedString);
} catch (UnsupportedEncodingException e) {
System.err.println("Encoding not supported: " + e.getMessage());
}
}
}
Output
LAB 11- Write a program to read data from server using socket
Client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
Server
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) throws IOException {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8080);
Client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
Server
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
Output
LAB 13 – write a java program to serve binary data using socket.
Client
import java.io.*;
import java.net.*;
try {
// Connect to the server on localhost, port 5000
socket = new Socket("localhost", 5000);
System.out.println("Connected to server!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close resources
try {
if (dataIn != null) dataIn.close();
if (socket != null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Server
import java.io.*;
import java.net.*;
public class BinaryDataServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket clientSocket = null;
DataOutputStream dataOut = null;
try {
// Create a server socket that listens on port 5000
serverSocket = new ServerSocket(5000);
System.out.println("Server is listening on port 5000...");
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close resources
try {
if (dataOut != null) dataOut.close();
if (clientSocket != null) clientSocket.close();
if (serverSocket != null) serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Output
LAB 14 – Write a Simple UDP java program to send data from client to
server
Client
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
Server
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class udpserver {
public static void main(String[] args) {
try{
DatagramSocket serverSocket = new DatagramSocket(5000);
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivPacket= new DatagramPacket(receiveBuffer,
receiveBuffer.length);
System.out.println("Server is waiting for a packet...");
serverSocket.receive(receivPacket);
String receivedMessage = new
String(receivPacket.getData(),0,receivPacket.getLength());
System.out.println("Received:"+receivedMessage);
serverSocket.close();
}catch(Exception e){
e.printStackTrace();
}
}
Output
LAB 15- Write a simple chat client server application using nio
Client
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
}
}catch (IOException ex){
ex.printStackTrace();
}
}
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
Output