course: programmation reseaux avec c#eliematta.com/system/files/project report...statuspanel.java it...
TRANSCRIPT
UUNNIIVVEERRSSIITTÉÉ AANNTTOONNIINNEE Faculté d’ingénieurs en Informatique,
Multimédia, Réseaux et Télécommunications
Course: Programmation Reseaux avec C#
Presented by: Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved
Client Server Advanced Management
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 1
Table of Contents 11.. IInnttrroodduuccttiioonn ............................................................................................................... 2
22.. TThhee UUsseerr IInntteerrffaaccee .................................................................................................... 3
A) Client Side .......................................................................................................... 3
i. Header: .............................................................................................................. 3
ii. Footer: ................................................................................................................ 3
B) Server Side ........................................................................................................ 4
i. Header: .............................................................................................................. 4
ii. Right Side:.......................................................................................................... 5
iii. Main Panel: ..................................................................................................... 5
iv. Footer: ............................................................................................................ 6
33.. CCoorree CCooddee ................................................................................................................ 8
A) Package hierarchy ............................................................................................. 8
1. Components: ...................................................................................................... 8
2. res.chat .............................................................................................................. 9
3. res.client ............................................................................................................. 9
4. res.file .............................................................................................................. 10
5. res.remote ........................................................................................................ 10
6. res.server ......................................................................................................... 12
B) Connecting to the Server ................................................................................. 13
C) Disconnecting from the Server ......................................................................... 15
D) Chatting ............................................................................................................ 17
E) File Transfer ..................................................................................................... 20
F) Remote Desktop .............................................................................................. 23
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 2
11.. IInnttrroodduuccttiioonn
For this project, we first thought of a way to manage the full advanced client server application therefore we adopted the following steps:
1. Client-user interface. 2. Server-user interface. 3. Core code.
Those steps will be detailed in the following sections; we can note that we combined those steps with each others to have a fully compatible working application over a network. The application will serve the following purposes:
1. Server listening to incoming client connections. 2. Client(s) connecting to the server. 3. Full communication protocol between the Server-Client (Chatting, File Transfer,
Remote desktop). For this end, the communication will happen over TCP streams therefore each client will have multiple connections with the server to meet the user needs. The application is developed using java programming language. In java the TCP connections are established using: ServerSocket running on the server waiting for the client connections. Socket running on the client side to connect to the server.
Data can be sent over TCP streams in many ways. The simplest way is to use the InputStream and OutputStream classes to send and receive data as arrays of bytes. A more convenient way is to send actual java object over the TCP stream. This can be done through using the ObjectOutputStream and ObjectInputStream classes. The exchange objects must implement the Serializeable interface. We can finally amend that once the client is connected to the server, it’s the Server part that initiates the three forms of communication (Chat, File Transfer, Remote desktop) not the other way around.
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 3
22.. TThhee UUsseerr IInntteerrffaaccee
AA)) CClliieenntt SSiiddee
The interface of the Client Side is divided into 2 big parts which is show in Figure 1 to 6:
i. Header:
Text box that specifies the IP of the server and saves it.
Connect button.
Disconnect button.
Figure 1 – Header in Client Side – Not connected
Figure 2 – Header in Client Side - Connected
ii. Footer:
TextArea to show the status of the Client that is being filled by the
updateStatus() of the StatusPanel component which will be explained in the
following section.
Figure 3 – Footer in Client Side – Initial State
Figure 4 – Footer in Client Side - Connected
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 4
Figure 5 – Footer in Client Side – Disconnected
Figure 6 – Full view on the User interface
BB)) SSeerrvveerr SSiiddee
The interface of the Server Side is divided into 3 parts displayed in Figure 7 to 17:
i. Header:
Start button
Stop button
Header
Footer
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 5
Figure 7 – Header in Server Side – Initial State
Figure 8 – Header in Server Side – Started
ii. Right Side:
ClientList that will be explained in detail in the following section.
Figure 9 – Right Side panel – Initial State Figure 10 – Right Side panel – Connected to a client
iii. Main Panel:
ClientStatusPanel that shows the button to choose.
Figure 11 – Main panel – After selecting a client from the ClientList
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 6
iv. Footer:
TextArea to show the status of the Client that is being filled by the
updateStatus() of the StatusPanel component which will be explained in the
following section.
Figure 12 – Server not running – Initial State
Figure 13 – Server Started – After Clicking the Start button
Figure 14 – Client connected – After the client clicked the Connect button
Figure 15 – Client connected – After the client clicked the Disconnect button
Figure 16 – Server Disconnected – After clicking the Stop button
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 7
Figure 17 – Full view on the Server interface
Header
Footer
Main Panel
Right Side
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 8
33.. CCoorree CCooddee
This is the main and most important part of the application where all the 3 parts
mentioned below communicate together. It is important to mention that the core code is
mainly included in the Common project.
AA)) PPaacckkaaggee hhiieerraarrcchhyy
Common project:
Contains the following packages
Figure 18
1. Components:
This package contains the following classes:
Figure 19
ChatWindow.java
It is the JFrame that allows the user to send and receive messages
during chat.
ClientStatusPanel.java
It is the JPanel used by the server to initiate communication with
the client that contains mainly the Chat, Remote Desktop and the
File transfer button.
ClientList.java
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 9
It is the JPanel that shows the currently connected clients, hence
the server can select a client from this list.
StatusPanel.java
It is the JPanel that contains the TextArea that displays the Status
of the server or the client through the updateStatus() function.
2. res.chat
Tracks the connections between the client and the server.
Figure 20
ChatController.java
Intercepts the client connection to the server to start the chat
session.
Message.java
This class represents any message sent between the client and the
server between the chat sessions, it includes these 2 properties:
content which is an array of bytes and the date that has a type
Date.
3. res.client
This package represents the classes related to the client.
Figure 21
Client.java
This class contains many properties but we will define the most
important ones which are Address of type InetAddress and the
connectiondate of type Date; the two methods connect and
disconnect will be detailed in the following section
.
ClientRequest.java
This class represents the request which will be sent from the client
to the server. It has two properties: type which has one of the
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 10
values of the enum Type (Connect, Disconnect), the second
property is the HashMap named params to send parameters with
the request from the client to the server.
ClientResponse.java
It handles the response of the client to the server. It has the
property type which has one of the values of the enum Type (Ok,
Error, Rejected)
ServerHandler.java
This class will be instantiated once the client is connected to the
server, it will enable the client to receive the server requests.
ServerListener.java
It is the interface that contains two methods: fileSending which is
triggered once upont he server wants to send a file to the client,
and the second method which is the fileReceived which is called
when the file is received on the client side.
4. res.file
This package handles the file transfer between the server and the client.
Figure 22
FileReciever.java
It runs on the client side and waits for the server request to transfer a
file to this client.
FileSender.java
It runs on the server side and sends a file to the client.
TransferableFile.java
Represents the file that should be sent from the server to the client. It
contains two properties: data which is array of byte and the second is
the String that contains the full name of the file sent.
5. res.remote
Handles the remote desktop between the client and the server.
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 11
Figure 23
Command.java
Represents any command sent from the client to the server during the
session. It has two properties: type which has one of the values of the
enum Type (Mouse_Click, Mouse_Enter, Mouse_Move, Key_Press),
the second property is the HashMap named arguments to send
parameters with the request from the client to the server.
RemoteCommandReciever.java
Runs on the client side, it allows the client to receive and process the
commands sent by the server.
RemoteCommandSender.java
Runs on the server side, it allows us to send the command from the
server to the client.
RemoteDesktopFrame.java
It is a JFrame that represents the client screen on the server. It
captures the server actions and sends them back to the
RemoteCommandSender to send them as command to the client
commands.
ScreenShotGrabber.java
It works on the client side; its purpose is to take the screenshots in a
continuous way and sends them to the server.
ScreenShotReciever.java
It works on the server side, it hands back the screenshots to the
JFrame included in RemoteDesktopFrame to be able to show them.
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 12
6. res.server
Represents the server classes
Figure 24
1. ClientHandler.java
It is instantiated once the client connects to the server; it serves the
server to receive the client requests.
2. ClientListener.java
It is an interface that includes 3 methods: clientConnected which is
called each time a new client connected to the server,
clientDisconnected which is called when the client gets disconnected
from the server and finally the fileReceived which is called when the
client receives the file transfer from the server.
3. ClientsConnectionTracker.java
This class is constituently running on the server side to detect any
client request to connect to the server.
4. Server.java
It represents the server. It is important to mention that this class can be
instantiated once and the instance can be accessed through the static
method : getInstance(). The 4 most important properties of this class
are address of type InetAddress, clients of type List<Client>, status of
type string and finally the startingDate of type Date.
This class has also 4 important methods which are: addClient,
removeClient, start, stop.
5. ServerRequest.java
It represents the request from the server to the client.
It has one property: type which has one of the values of the enum Type
(Disconnect, BeginChat, BeginRemoteDesktop).
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 13
6. ServerResponse.java
It represents the response from the server to the client. It has one
property: type which has one of the values of the enum Type (Ok,
Error).
BB)) CCoonnnneeccttiinngg ttoo tthhee SSeerrvveerr
Once the server is correctly initiated, the clients can begin connecting to it.
Any client can connect to the server by calling the connect method which takes as
argument the IP address of the server.
This method will perform the following tasks on the client side:
1. Takes the IP of the client and stores it in the property named address.
2. It opens a TCP socket to the server on port 4000.
3. Creates an instance of ObjectOutputStream.
4. Creates new ClientRequest of type Connect and passes to it a hashMap of
arguments containing the client itself.
5. Sends this client request over the ObjectOutputStream instance.
6. Creates an instance of ObjectInputStream.
7. Reads the ServerResponse that the server has sent back to the client.
8. If the response is of type Ok, the client will create a new instance of
ServerHandler and FileReceiver and starts them.
On the server side, in the ClientsConnectTracker class:
1. The client connection is intercepted through the accept method of the server
socket.
2. A new instance of ClientHandler is created and started.
The ClientHandler instance will:
1. Read the ClientRequest object.
2. Gets the Client object from the ClientRequest.
3. This Client object will be added to the server.
4. An instance of ObjectOutputStream is created.
5. An instance of ServerResponse is created and given the type Ok.
6. The response is sent to the client over the ObjectOutputStream instance.
All of these steps can be clearly shown in the following figures:
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 14
Figure 25 – Client side connect/disconnect
Figure 26 – Server side connect/disconnect
1 2
1 2
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 15
CC)) DDiissccoonnnneeccttiinngg ffrroomm tthhee SSeerrvveerr
The client can disconnect from the server in one of two ways:
1. The client clicks the disconnect button.
2. The client closes his own application.
This disconnect happens by calling the disconnect method of the Client class, this
method will:
1. Check if the connection is still opened with the server.
2. Create an ObjectOutputStream instance.
3. Create a ClientRequest object of type Disconnect to which we will pass the client
as arguments.
4. We will send the request on the ObjectOutputStream instance.
On the server side, the client disconnection is handled by the ClientHandler class
which will:
1. Intercept the client request to disconnect from the server.
2. It will close the connection of the client with the server.
3. This class will stop handling all incoming requests from the client.
All of these steps can be clearly shown in the following figures:
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 16
Figure 27 – Client side disconnect
Figure 28 – Server side disconnect
1 2
1 2
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 17
In the following we will detail the three methods that enable the communication
between the client and the server.
DD)) CChhaattttiinngg
The chat session is initiated when the server selects a client and presses the chat
button.
This action on the server side will:
1. Retrieve the socket from the client.
2. Create an instance of ObjectOutputStream.
3. Create an instance of ServerRequest of type BeginChat.
4. Send the request over the ObjectOutputStream instance.
On the client side, the ServerHandler class will:
1. Intercept the server request.
2. Create a new TCP socket on port 4002.
3. Create a new instance of ChatWindow.
4. Make the ChatWindow visible.
Once again, we will go back to the server side to the ChatController class that will:
1. Intercept the client connection on port 4002.
2. Create an instance of ChatWindow.
3. Make the ChatWindow visible.
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 18
1 2 3
Figure 29 – Chat session establishment
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 19
Once the chat session is established, the client and the server can exchange
messages; this is done in the ChatWindow class.
Sending a message is performed by the action named sendAction that is fired when the
user clicks the send button or presses Enter in the text area.
This action will:
1. Get the text that was typed in the text area.
2. Create a new instance of Message and pass to it the inputed text as an array of
bytes as well as the current date.
3. Create an instance of ObjectOutputStream.
4. Send the message over the instance of ObjectOutputStream.
5. Show the sent message in the ChatWindow.
Receiving a message is also performed in the ChatWindow class by:
1. Creating an instance of ObjectInputStream.
2. Retrieve the Message object from the ObjectInputStream instance.
3. Construct a new String object from the message’s content.
4. Display the text in the ChatWindow.
Figure 30 – Chat between Client and Server
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 20
EE)) FFiillee TTrraannssffeerr
This functionality enables the server to send a file to the client.
This can be achieved when the server selects a client and presses File Transfer button.
This will:
1. Show the server JFileChooser component from where a file to send can be
selected.
2. A new FileSender instance is created and started.
The FileSender class will:
1. Create a new TCP socket on port 4001.
2. Create a new FileInputStream object.
3. Read the contents of the file and place in an array of bytes through the read
method of the FileInputStream object.
4. Create a new instance of TransferableFile.
5. It assigns the name and the data of the file to this instance.
6. It creates an instance of ObjectOutputStream.
7. It will send the transferable file on the ObjectOutputStream instance to be
handled by the FileReceiver class which will be explained later on.
8. A new instance of ObjectInputStream is created.
9. The ClientResponse object is read from this instance.
10. If the response is of type Ok, then the file is received to the client; Otherwise the
server is notified that the client has rejected the request to send the file.
On the client side the FileReceiver class will:
1. Accept the connection of the server.
2. Shows the client the awaiting confirmation.
3. If the user accepts to receive the file:
a. A new instance of ObjectInputStream is created.
b. The TransferableFile object is read from the ObjectInputStream instance.
c. A new File instance is created in the user home directory.
d. A new FileOutputStream object is created.
e. It is then used to write the received data to the created file.
f. A new instance of ObjectOutputStream is created.
g. A new instance of ClientResponse is created and given the type Ok.
h. This instance is sent on the ObjectOutputStream.
4. If the user doesn’t accept the file:
a. A new instance of ObjectOutputStream is created.
b. A new instance of ClientResponse is created and given the type Rejected.
c. This instances is sent on the ObjectOutputStream.
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 21
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 22
Figure 31 – Receiving a file from the server
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 23
FF)) RReemmoottee DDeesskkttoopp
This functionality allows the server to remotely control the clients desktop.
This can be achieved when the server presses the remote desktop button which will:
1. Create a new instance of ScreenShotReceiver and start it.
2. Get the socket of the client.
3. Create a new instance of ObjectOutpuStream.
4. Create a new instance of ServerRequest of type BeginRemoteDesktop.
5. Send this instance over the ObjectOutputStream instance.
On the client side, the ServerHandler class:
1. Will intercept the server’s request to begin remote desktop.
2. Create a new instance of ScreenShotGrabber and start it.
3. Create a new instance of RemoteCommandReceiver and start it.
The ScreenShotGrabber class will:
1. Open a TCP socket on port 4003.
2. Consciously take a snapshot of the client’s desktop.
3. Compress the image to reduce its size.
4. Create a new instance of ObjectOutpuStream.
5. Write the obtained image over the instance of ObjectOutputStream.
The RemoteCommandReceiver class will:
1. Create a new TCP socket on port 4004.
2. Create a new instance of ObjectInpuStream.
3. Read the Command object from the instance of ObjectInputStream.
4. Process the received command.
The ScreenShotReceiver class will:
1. Create a new ServerSocket on port 4003.
2. Accept connection from the client to start sending screenshots.
3. Create a new instance of RemoteDesktopFrame and make it visible to the user.
4. Create a new instance of RemoteCommandSender and start it.
The RemoteClientSender class will:
1. Intercept the server mouse and keyboard events.
2. Create a new instance of TCP socket on port 4004.
3. Create a new instance of ObjectOutputStream.
4. Create a Command object that contains the mouse or keyboard event.
5. Send the command object over the ObjectOutputStream instance.
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 24
The RemoteDesktopFrame class will:
1. Create an instance of ObjectInputStream.
2. Read the ImageIcon object from the ObjectInputStream instance.
3. Get the Image object from the ImageIcon instance.
4. Scale this image to fit the server desktop size.
5. Paint this image on the frame.
Figure 32 – Server side Remote destop
Presented by Elie Matta et al.
Copyright © 2010-2011, eliematta.com. All rights reserved | Page 25
Figure 33 – Server side remote desktop