0% found this document useful (0 votes)
15 views

Practical 4

This document contains code for 4 Java programs using network programming concepts: 1. A program to parse parts of a URL like protocol, file, port, and path. 2. A program to download the contents of a web page using URL and URLConnection classes. 3. A client-server program where the client requests a file's last modification time from the server. 4. A mini chat application with client and server that can initiate a "STOP" request to end chatting.
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)
15 views

Practical 4

This document contains code for 4 Java programs using network programming concepts: 1. A program to parse parts of a URL like protocol, file, port, and path. 2. A program to download the contents of a web page using URL and URLConnection classes. 3. A client-server program where the client requests a file's last modification time from the server. 4. A mini chat application with client and server that can initiate a "STOP" request to end chatting.
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
You are on page 1/ 8

Name = Anamika Shrivastav

Roll No = 543

Division = B

1.WAP to get the parts of an URL with the help of url.getProtocol(), url.getFile(), url.getPort(),
url.getPath() methods etc. of java.net.URL class.

CODE :

import java.net.URL;

public class URLinParts{


public static void main(String args[]) throws Exception{
String urlStr = "https://drive.google.com/drive/u/0/my-drive";
URL url = new URL(urlStr);
System.out.println("URL is " + url.toString());
System.out.println("Protocol is " + url.getProtocol());
System.out.println("File name is " + url.getFile());
System.out.println("Host is " + url.getHost());
System.out.println("Path is " + url.getPath());
System.out.println("Port is " + url.getPort());
System.out.println("Default port is " + url.getDefaultPort());

}
}

OUTPUT :
2.Download the contents of a Web page, Use URL & URLConnection classes ?

CODE :

import java.io.*;
import java.net.*;

public class DownloadWebPage {


public static void main(String[] args) {
URL url;
String urlStr =
"file:///C:/Users/RYZEN/Desktop/Trimester9/Advance%20Java/Topic%202%20-
%20Networking/Soctket%20Programming/BufferedInputStreamExample.java";
String line;

try {
url = new URL(urlStr);
InputStream inputStream = url.openStream();
BufferedReader bReader = new BufferedReader(new
InputStreamReader(inputStream));

while ((line = bReader.readLine()) != null) {


System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

OUTPUT :

3.Write a Java Program where the client will send the file request to the server and the server will
respond when was that file last modified in the following Date Format : "dd MM yyyy HH:mm:ss:SSS Z" ?

CODE :

Client

import java.io.*;
import java.net.*;

public class CliReqLastModi {


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

Socket socket = new Socket(InetAddress.getLocalHost(), 5217);


System.out.println("Request sent ... ");

// read file name


BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter tht filename : ");
String fileString = bufferedReader.readLine();

// outputstream
DataOutputStream dataOutputStream = new
DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF(fileString);

DataInputStream dataInputStream = new


DataInputStream(socket.getInputStream());
String msg = dataInputStream.readUTF();
System.out.println(msg);
}

Server

import java.net.*;
import java.io.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class SerResLastModi {


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

ServerSocket serverSocket = new ServerSocket(5217);

System.out.println("Server is running and waiting for client request ");


Socket socket = serverSocket.accept();

//reading from keyboard


DataInputStream dataInputStream = new
DataInputStream(socket.getInputStream());
String fileString = dataInputStream.readUTF();

DataOutputStream dataOutputStream = new


DataOutputStream(socket.getOutputStream());
URL url = new URL(fileString);
URLConnection urlConnection = url.openConnection();

//timestamp
long timestamp = urlConnection.getLastModified();
SimpleDateFormat df = new SimpleDateFormat("dd MM yyyy HH:mm:ss:SSS Z");
Date dt = new Date(timestamp);
dataOutputStream.writeUTF("The last modification time is :"
+df.format(dt));

}
}

OUTPUT :

4.Develop a mini chatting application which includes Java Socket program that runs on a server which
accepts a message from client & server gives the response, either the client or the server can initiate the
"STOP" request to stop chaatting?

CODE :

Server

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {


public static void main(String args[]) throws Exception {
ServerSocket serverSocket = new ServerSocket(3000);
System.out.println("server is running and waiting for client to start ...
");
Socket socket = serverSocket.accept();
System.out.println("Connection established ... ");
System.out.println("Start chatting ... ");
System.out.println("Type 'STOP' to chatting");

BufferedReader keyboardBufferedReader = new BufferedReader(new


InputStreamReader(System.in));
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),
true);
BufferedReader rBufferedReader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

try {
String receiveMessage = "";
String sendMessage = "";

while (!receiveMessage.equals("STOP")) {
sendMessage = keyboardBufferedReader.readLine(); // read keyboard
printWriter.println(sendMessage);// sending to server
printWriter.flush();
if ((receiveMessage = rBufferedReader.readLine()) != null) {
System.out.println(receiveMessage);
} else if ((receiveMessage = rBufferedReader.readLine()) !=
"STOP") {
serverSocket.close();
socket.close();
keyboardBufferedReader.close();
rBufferedReader.close();
printWriter.close();
}

}
} catch (Exception e) {
System.out.println("Chatting stopped ... ");
}

}
}

Client

import java.io.*;
import java.net.*;

public class ChatClient {


public static void main(String args[]) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(), 3000);

BufferedReader keyboardBufferedReader = new BufferedReader(new


InputStreamReader(System.in));
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),
true);
BufferedReader rBufferedReader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

System.out.println("Start chatting ... ");


System.out.println("Type 'STOP' to stop chatting");

try {
String receivedMessage = "", sendMessage = "";
while (!receivedMessage.equals("STOP")) {
sendMessage = keyboardBufferedReader.readLine(); // read keyboard
printWriter.println(sendMessage);// sending to server
printWriter.flush();

if ((receivedMessage = rBufferedReader.readLine()) != null) {


System.out.println(receivedMessage);
} else if ((receivedMessage = rBufferedReader.readLine()) !=
"STOP") {
socket.close();
keyboardBufferedReader.close();
rBufferedReader.close();
printWriter.close();
}
}
} catch (Exception e) {
System.out.println("Chatting Stopped ... ");
}

}
}

OUTPUT :

You might also like