0% found this document useful (0 votes)
3 views21 pages

Network Program Lab

The document contains a series of Java programs that demonstrate various networking concepts, including retrieving local machine information, handling URLs and URIs, managing HTTP cookies, and implementing client-server communication using sockets. Each program is designed to perform specific tasks such as finding IP addresses, fetching website content, and creating a simple chat application. The examples cover a wide range of networking functionalities in Java, showcasing the use of classes from the java.net package.

Uploaded by

dubsmashking981
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)
3 views21 pages

Network Program Lab

The document contains a series of Java programs that demonstrate various networking concepts, including retrieving local machine information, handling URLs and URIs, managing HTTP cookies, and implementing client-server communication using sockets. Each program is designed to perform specific tasks such as finding IP addresses, fetching website content, and creating a simple chat application. The examples cover a wide range of networking functionalities in Java, showcasing the use of classes from the java.net package.

Uploaded by

dubsmashking981
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

1. Find the address and hostname of the local machine.

>>Codeimport [Link].*;
public class LocalHost {
public static void main(String[] args) throws Exception {
InetAddress address = [Link]();
[Link]([Link]());
[Link]([Link]());
}
}

[Link] a java program to find canonicalHostName, HostName, IpAddress of [Link].


>>Code
import [Link].*;
public class App2 {
public static void main(String[] args) throws UnknownHostException {
InetAddress address = [Link]("[Link]");
[Link]("Sujan Pandey");
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
byte[] arr= [Link]();
for(byte b : arr){
[Link](b);
} }}
[Link] a program to retrieve HostName, IP Address and Mac Address of Local Machine.
>>Code
import [Link].*;
public class App3 {
public static void main(String[] args) throws Exception {
InetAddress localHost = [Link]();
String hostName = [Link]();
String ipAddress = [Link]();

NetworkInterface network = [Link](localHost);


byte[] mac = [Link]();

// Print the details


[Link]("Hostname: " + hostName);
[Link]("IP Address: " + ipAddress);
[Link]("MacAddress: ");
if (mac != null) {
for (int i = 0; i < [Link]; i++) {
[Link]("%02X", mac[i]);
}
}
}
}

[Link] a java program to check Loopbackaddress, private address, multicast address, anylocaladdress.
>>Code
import [Link].*;
public class App4 {
public static void main(String[] args) throws UnknownHostException {
InetAddress address = [Link]("[Link]");
if ([Link]()) {
[Link](address + " is a loopback address.");
}
address = [Link]("[Link]");
if ([Link]()) {
[Link](address + " is a private address.");
}
address = [Link]("[Link]");
if ([Link]()) {
[Link](address + " is a multicast address.");
}
address = [Link]("[Link]");
if ([Link]()) {
[Link](address + " is the unspecified address.");
}
}
}

[Link] a java program to list all the network interfaces on the localmachine.
>>Code
import [Link].*;
import [Link].*;
import [Link].*;

public class App5 {


public static void main(String[] args) throws IOException {
Enumeration<NetworkInterface> interfaces = [Link]();
while ([Link]()) {
NetworkInterface ni = (NetworkInterface) [Link]();
[Link](ni);
}

}
}

[Link] a program to find baseurl, relativeurl and resolvedurl form the given url
[Link]
>>Code
import [Link].*;
public class App7 {
public static void main(String[] args) throws MalformedURLException {
String baseurl = "[Link]
String relativeUrl = "Networking_Programming-[Link]";
URL baseUrl = new URL(baseurl);
URL resolvedRelativeUrl = new URL(baseUrl, relativeUrl);
[Link]("BaseUrl:" + baseurl);
[Link]("Relative Url:" + relativeUrl);
[Link]("Resolved Relative Url:" + resolvedRelativeUrl);
}
}

[Link] a java program to spit different component of url from given url:
([Link]
>>Code
import [Link].*;
public class App8 {
public static void main(String[] args) throws MalformedURLException {
URL url1 = new
URL("[Link]
[Link]([Link]());
[Link]();
[Link](
"Different components of the URL1-");
[Link]("Protocol:- " + [Link]());
[Link]("Hostname:- " + [Link]()); [Link]("Default port:- "+
[Link]());
[Link]("Query:- " + [Link]());
[Link]("Path:- " + [Link]());
[Link]("File:- " + [Link]());
[Link]("Reference:- " + [Link]());
}

}
[Link] a java program to get different parts of URI in given
URI=[Link]
>>Code
import [Link].*;
public class App10
{
public static void main(String[] args) throws Exception {
String str = "[Link]
URI uri = [Link](str);
[Link]([Link]().toString());
[Link]("Scheme = " + [Link]());
[Link]("Schemespecificpart = "+ [Link]());
[Link]("Raw User Info = " + [Link]());
[Link]("User Info = " + [Link]());
[Link]("Authority = " + [Link]());
[Link]("Host = " + [Link]());
[Link]("Path = " + [Link]());
[Link]("Port = " + [Link]());
[Link]("Query = " + [Link]());

}
}
12. Write a program to handle HTTP cookie in java using the CookieManager and HttpCookie c;asses
also eretrive and display cookie information from a specified URL.
>> Code
import [Link].*;

import [Link].*;

public class App12

private final static String URL_STRING = "[Link]

public static void main(String[] args) throws Exception {

CookieManager cookieManager = new CookieManager();

[Link](cookieManager);

URL url = new URL(URL_STRING);

URLConnection connection = [Link]();

[Link]();

CookieStore cookieStore = [Link]();

List<HttpCookie> cookieList = [Link]();

for (HttpCookie cookie : cookieList)

[Link]("Domain: " + [Link]());

[Link]("max age: " + [Link]());

[Link]("name of cookie: " + [Link]());

[Link]("server path: " + [Link]());

[Link]("is cookie secure: " + [Link]());

[Link]("value of cookie: " + [Link]());


[Link]("value of cookie version: " + [Link]());

13. Write a java program to manage HTTP cookie using cookieStore and HttpCookie classes also
add,retrieve and remove cookie from a cookieStore.

>> Code

import [Link].*;

import [Link].*;

public class App13{

private final static String URL_STRING="[Link]

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

CookieManager cookieManager = new CookieManager();

CookieStore cookieStore = [Link]();

HttpCookie cookieA = new HttpCookie("First", "1");

HttpCookie cookieB = new HttpCookie("Second", "2");

[Link](3600);

[Link](false);

URI uri = [Link](URL_STRING);


[Link](uri, cookieA);

[Link](null, cookieB);

[Link]("Cookie sucessfully added\n");

var cookiesWithURI = [Link](uri);

[Link]("Cookie associated with URI in cookieStore:"+cookiesWithURI+"\n");

var cookieList = [Link]();

[Link]("Cookies in CookieStore: " + cookieList + "\n");

var uriList = [Link]();

[Link]("URIs in CookieStore: " + uriList + "\n");

[Link]("Removal of Cookie: " + [Link](uri, cookieA));

[Link]("Remaining Cookies: " + cookieList + "\n");

[Link]("Removal all Cookies: " + [Link]());

[Link]("Empty CookieStore: " + cookieList);

}
14. Write a java program to fetch Website content using URL connection class.

>> Code

import [Link].*;

import [Link].*;

public class App14 {

public static void main(String[] args) throws Exception {

try {

URL u=new URL("[Link]

URLConnection uc=[Link]();

[Link]();

InputStream in=[Link]();

BufferedReader br=new BufferedReader(new InputStreamReader(in));

String line;

while ((line=[Link]())!=null) {
[Link](line);

[Link]();

[Link]();

} catch (Exception e) {

[Link]();

15. Write a java program to fetch all HTTP header Fields using URL connection.

>> Code

import [Link].*;
import [Link];

import [Link];

public class App15 {

public static void main(String[] args) throws Exception {

URL u = new URL("[Link]

URLConnection uc = [Link]();

Map<String, List<String>> header = [Link]();

for ([Link]<String, List<String>> mp1 : [Link]())

[Link]([Link]() + " : ");

[Link]([Link]().toString());

[Link] a java program to retrieving Specific Header Fields from URL using URL connection Class.

>> Code

import [Link].*;

import [Link].*;

public class App16 {

public static void main(String[] args) throws Exception {

URL u = new URL("[Link]

URLConnection uc = [Link]();

[Link]("Content-type: " + [Link]());


[Link]("Content-encoding: " + [Link]());

[Link]("Date: " + new Date([Link]()));

[Link]("Last modified: " + new Date([Link]()));

[Link]("Expiration date: "+ new Date([Link]()));

[Link]("Content-length: " + [Link]());

[Link] a java program to read data from server using socket.

>>Code

Client:

import [Link].*;
import [Link];

public class ServerClient {


public static void main(String[] args) throws IOException {
Socket clientSocket = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));//read data so InputStream
String serverResponse = [Link]();
[Link]("Server says: " + serverResponse);
[Link]();
}
}
Server:
import [Link].*;
import [Link].*;

public class MyServer {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
Socket clientSocket = [Link]();
[Link]("Connected to Client");
PrintWriter out = new PrintWriter([Link](), true);
[Link]("Hello from server");
[Link]();
[Link]();
}
}

[Link] a server side program for daytime service using socket.

>>Code

Server Side:

import [Link].*;

import [Link].*;

public class WriteServer {

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

ServerSocket serverSocket = new ServerSocket(1234);

[Link]("Server started");
Socket server = [Link]();

BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));

String clientMessage = [Link]();

[Link]("Client says: " + clientMessage);

[Link]();

[Link]();

Client Side:

import [Link].*;

import [Link].*;

public class WriteClient {

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

Socket client = new Socket("localhost", 1234);

PrintWriter out = new PrintWriter([Link](), true);

[Link]("Hello from client");

[Link]();

[Link] a server side program for daytime service using socket.

>>Code

ClientSide:

import [Link].*;

import [Link].*;
public class MyClient {

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

Socket clientSocket = new Socket("localhost", 1234);

BufferedReader in = new BufferedReader(new


InputStreamReader([Link]()));

String serverResponse = [Link]();

[Link]("Server says: " + serverResponse);

[Link]();

ServerSide:

import [Link].*;

import [Link].*;

import [Link].*;

public class DayTimeServer {

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

ServerSocket serverSocket = new ServerSocket(1234);

Socket clientSocket = [Link]();

[Link]("Connected to Client");

String currentDateTime = new Date().toString();

PrintWriter out = new PrintWriter([Link](), true);

[Link](currentDateTime);

[Link]();

[Link]();

}
[Link] a program for canning lical TCP ports from 1024 port to up.

>>Code

import [Link].*;

import [Link].*;

public class LocalPort {

public static void main(String[] args) throws Exception {

int startingPort = 1024;

int endinPort = 65535;

[Link]("Scanning local TCP ports...");

for (int p= startingPort; p <= endinPort; p++) {

if (isportvalid(p)) {

[Link](p + " ");

private static boolean isportvalid(int port) {

try {

ServerSocket serverSocket = new ServerSocket(port);

[Link]();

return true;

} catch (IOException e) {

return false;

}
[Link] a program to find WebAddress, IPAddress, Server Port Local Port, Local IPAddress from
socket with host address [Link] .com.

>>Code

import [Link].*;

import [Link].*;

public class Finding {

public static void main(String[] args) throws Exception {

String host="[Link]";

try {

Socket theSocket = new Socket(host, 80);

[Link]("Connected to " + [Link]()

+ " on port " + [Link]() + " from port "

+ [Link]() + " of "

+ [Link]());

} catch (UnknownHostException ex) {

[Link]("I can't find " + host);

} catch (SocketException ex) {

[Link]("Could not connect to " + host);

} catch (IOException ex) {

[Link](ex);

}
}

[Link] a simple chat application using TCP Socket.

>>Code

Client:

import [Link].*;

import [Link].*;

public class ChatClient {

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

Socket clientSocket = new Socket("localhost", 1234);

PrintWriter out = new PrintWriter([Link](), true);

BufferedReader in = new BufferedReader(new


InputStreamReader([Link]()));

int a=5;

int b=6;

[Link](a + " " + b);

String serverResponse = [Link]();

[Link]("Sum from server: " + serverResponse);

[Link]();

Server:

import [Link].*;

import [Link].*;

public class ChatServer {

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

ServerSocket serverSocket = new ServerSocket(1234);

Socket clientSocket = [Link]();


[Link]("Connected to Client");

PrintWriter out = new PrintWriter([Link](), true);

BufferedReader in = new BufferedReader(new


InputStreamReader([Link]()));

String inputLine = [Link]();

[Link]("Received from client: " + inputLine);

String[] numbers = [Link](" ");

int a = [Link](numbers[0]);

int b = [Link](numbers[1]);

int sum = a + b;

[Link]("Calculated sum: " + sum);

[Link](sum);

[Link]();

[Link]();

You might also like