0% found this document useful (0 votes)
5 views5 pages

Java Socket Program For HTTP Web Page

The document outlines a Java Socket Program for uploading and downloading web pages or images through client-server communication. It includes detailed algorithms for both server and client sides, along with sample code for implementation. The program successfully demonstrates file transfer over a network using sockets, with instructions on how to run the program in NetBeans.

Uploaded by

Saksham Sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Java Socket Program For HTTP Web Page

The document outlines a Java Socket Program for uploading and downloading web pages or images through client-server communication. It includes detailed algorithms for both server and client sides, along with sample code for implementation. The program successfully demonstrates file transfer over a network using sockets, with instructions on how to run the program in NetBeans.

Uploaded by

Saksham Sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Socket Program for HTTP Web Page/Image Upload & Download

1. AIM

To write a program using Java Socket Programming to upload and download a web page or
image using client–server communication.

2. ALGORITHM

Server Side

1. Start the server.

2. Create a ServerSocket on port 5000.

3. Wait for the client connection.

4. Receive file name from client.

5. Read the requested file (HTML/image).

6. Send file bytes to the client.

7. Close connection.

Client Side

1. Start client program.

2. Connect to server using IP and port.

3. Send requested file name.

4. Receive file bytes from server.

5. Save file locally.

6. Display download success.

3. SERVER PROGRAM ([Link])

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

public class Server {

public static void main(String[] args) {


try {
ServerSocket server = new ServerSocket(5000);
[Link]("Server started...");
[Link]("Waiting for client...");

Socket socket = [Link]();


[Link]("Client connected");

DataInputStream dis = new DataInputStream([Link]());


DataOutputStream dos = new DataOutputStream([Link]());

String fileName = [Link]();


[Link]("Client requested file: " + fileName);

File file = new File(fileName);

if ([Link]()) {

FileInputStream fis = new FileInputStream(file);


byte[] buffer = new byte[4096];

[Link](true);

int bytes;
while ((bytes = [Link](buffer)) > 0) {
[Link](buffer, 0, bytes);
}

[Link]();
} else {
[Link](false);
}

[Link]();
[Link]();

} catch (Exception e) {
[Link](e);
}
}
}
4. CLIENT PROGRAM ([Link])

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

public class Client {

public static void main(String[] args) {

try {

Socket socket = new Socket("localhost", 5000);

DataInputStream dis = new DataInputStream([Link]());


DataOutputStream dos = new DataOutputStream([Link]());

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

[Link]("Enter file name to download:");


String fileName = [Link]();

[Link](fileName);

boolean fileExists = [Link]();

if (fileExists) {

FileOutputStream fos = new FileOutputStream("download_" + fileName);

byte[] buffer = new byte[4096];


int bytes;

while ((bytes = [Link](buffer)) > 0) {


[Link](buffer, 0, bytes);
if (bytes < 4096)
break;
}

[Link]();
[Link]("File downloaded successfully!");

} else {
[Link]("File not found on server.");
}

[Link]();

} catch (Exception e) {
[Link](e);
}
}
}

5. HOW TO RUN IN NETBEANS

Step 1

Open NetBeans

Step 2

Create Project
File → New Project → Java Application

Step 3

Create two classes

[Link]
[Link]

Step 4

Place HTML page or image in project folder

Example files

[Link]
[Link]

Step 5

Run [Link] first

Output

Server started
Waiting for client...
Client connected

Step 6
Run [Link]

Enter file name

[Link]

Output

File downloaded successfully!

6. SAMPLE OUTPUT

Server

Server started...
Waiting for client...
Client connected
Client requested file: [Link]

Client

Enter file name to download:


[Link]
File downloaded successfully!

7. RESULT

Thus the Java socket program for HTTP web page/image upload and download using
client–server communication was executed successfully.

You might also like