CN Project 3 Report On Computer Networks
CN Project 3 Report On Computer Networks
Network Desktop
Manager
Submitted by
Arnab Banerjee Reg. No.: 2141002018
Abhishek Bhatta Reg. No.: 2141002088
Badal Kumar Mohapatra Reg. No.: 2141013261
Abhishek Jyethi Reg. No.: 2141010069
ii
Abstract
iii
Contents
1. 1 Introduction 1
2. 2 Problem Statement 2
3. 3 Methodology 3-4
4. 4 Implementation 5 - 10
6. 6 Conclusion 12
7. References 13
iv
1. Introduction
1
2.Problem Statement
2
3.Methodology
Client Algorithm:
1. Class Definition:
Define a class NetworkDesktopManagerClient that extends JFrame and implements the
ActionListener interface.
2. Constants:
Declare a constant PORT with the port number on which the client will connect to the server.
3. Instance Variables:
Declare instance variables for the client socket (clientSocket), input stream (input), output
stream (output), text area (chatArea), text field (chatField), and buttons (fileButton,
desktopButton).
Declare a boolean variable sharingDesktop to control desktop sharing.
4. Constructor:
In the constructor NetworkDesktopManagerClient() throws IOException:
Call the superclass constructor with the window title.
Set the layout to BorderLayout.
Initialize and configure GUI components (text area, text field, buttons).
Establish a connection to the server using a socket (clientSocket).
Initialize input and output streams (input, output).
Display a message indicating a successful connection.
5. ActionListener Implementation:
Implement the actionPerformed method:
If the source is fileButton, call the sendFiles method.
If the source is desktopButton, call the sendDesktop method.
6. sendFiles Method:
Implement the sendFiles method:
Use a JFileChooser to allow the user to select multiple files.
Send the number of files to the server.
For each selected file:
Send the file size.
Read the file content and send it in chunks.
7. sendDesktop Method:
Implement the sendDesktop method:
Get the desktop resolution using Toolkit.
Create a Robot object to capture the desktop.
Capture the desktop image using createScreenCapture.
Convert the BufferedImage to a byte array.
Break the byte array into smaller packets and send them to the server.
8. Main Method:
Implement the main method:
Create an instance of NetworkDesktopManagerClient in a try-catch block.
Handle IOException by printing the stack trace.
9. Adjustments:
Uncomment the code related to the sendButton if you want to enable sending text messages.
Adjust the packet size in the sendDesktop method based on your network condition
3
Server algorithm:
1. Class Definition:
Define a class NetworkDesktopManagerServer that extends JFrame and implements the
ActionListener interface.
2. Constants:
Declare a constant PORT with the port number on which the server will listen for
connections.
3. Instance Variables:
Declare instance variables for the server socket (serverSocket), client socket (clientSocket),
input stream (input), output stream (output), text area (chatArea), text field (chatField), and
buttons (fileButton, desktopButton).
4. Constructor:
In the constructor NetworkDesktopManagerServer() throws IOException:
Call the superclass constructor with the window title.
Set the layout to BorderLayout.
Initialize and configure GUI components (text area, text field, buttons).
Create a server socket and wait for a client to connect.
Initialize input and output streams (input, output) once the client is connected.
Display a message indicating a successful client connection.
5. ActionListener Implementation:
Implement the actionPerformed method:
If the source is fileButton, call the receiveFiles method.
If the source is desktopButton, call the receiveDesktop method.
6. receiveFiles Method:
Implement the receiveFiles method:
Read the number of files being sent by the client.
For each file:
Read the file size and create a FileOutputStream for the received file.
Read and write the file content in chunks until the entire file is received.
Close the FileOutputStream.
Display a message indicating the successful receipt of the file.
7. receiveDesktop Method:
Implement the receiveDesktop method:
Read the number of packets from the client.
Create a ByteArrayOutputStream to accumulate the desktop image data.
Loop through the packets:
Read a packet of image data and write it to the ByteArrayOutputStream.
Convert the accumulated byte array to a BufferedImage.
Display the desktop image in a new JFrame with a JLabel.
Display a message indicating the successful receipt of the desktop image.
8. Main Method:
Implement the main method:
Create an instance of NetworkDesktopManagerServer in a try-catch block.
Handle IOException by printing the stack trace.
4
4
4. Implementation
Client code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
4
5
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton) {
String message = chatField.getText();
try {
output.writeUTF(message);
chatArea.append("You: " + message + "\n");
} catch (IOException ex) {
chatArea.append("Error sending message: " + ex.getMessage() + "\n");
}
chatField.setText("");
} else if (e.getSource() == fileButton) {
sendFiles(); // Updated method name
} else if (e.getSource() == desktopButton) {
sendDesktop();
}
}
// Break the image into smaller packets and send them in a loop
int packetSize = 1024; // Adjust this value based on your network conditions
int numPackets = (int) Math.ceil((double) desktopBytes.length / packetSize);
output.writeInt(numPackets); // Send the number of packets
7
7
Server code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8
8
// Accept client connection
clientSocket = serverSocket.accept();
chatArea.append("Client connected");
input = new DataInputStream(clientSocket.getInputStream());
output = new DataOutputStream(clientSocket.getOutputStream());
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton) {
String message = chatField.getText();
try {
output.writeUTF(message);
chatArea.append("Server: " + message + "\n");
} catch (IOException ex) {
chatArea.append("Error sending message: " + ex.getMessage() + "\n");
}
chatField.setText("");
} else if (e.getSource() == fileButton) {
receiveFiles();
} else if (e.getSource() == desktopButton) {
receiveDesktop();
}
}
10
10
Results & Interpretation
Server side:
Client side:
11
11
Conclusion
12
1
References
(as per the IEEE recommendations)
[3] https://www.oracle.com/in/java/
13
1