CS8603 Distributed-Computing-Project-Report
CS8603 Distributed-Computing-Project-Report
ENGINEERING
DISTRIBUTED SYSTEM
Mrs.Malathi AP/CSE
Submitted by:
NAME:GOPI.P
Sharing files using devices became essential nowadays. To show the contribution of
Distributed Computing in this application, we decided to create an online chatting room for
sharing images and chatting. It is based on Client-Server application using java. A server will be
established for the connection between the client and servers. Client/server architecture is a
computing model in which the server hosts, delivers and manages most of the resources and
services to be consumed by the client. This type of architecture has one or more client computers
connected to a central server over a network or Internet connection. This system shares
computing resources.
2.0 OBJECTIVE
This project is aimed to establish a connection between Client and Server to allow the
user to chat and share images among them.
This project is to show the uses of Distributed Computing in our daily life. As example
we would like to create an application to allow the user chatting and share images between them.
The conversation will be join by many Clients from different desktop and a server like a
broadcast message. They can view the conversation in the window and sending each other
messages. The connection is established using the LAN network and Internet
0
access and/or direct access to the server’s raw computing power. So in this chatting application,
we create the sharing images between multiple clients based on the file sharing services. The
clients will enter the chatroom when the server is up and they can chat and share images.
We have decided to use Java Language to create our project. Using Java, we have build a
terminal and GUI for Client and Server. A server computer can manage several clients
simultaneously, whereas one client can be connected to several servers at a time, each providing
a different set of services. Then, the client must enter their nickname to register into the server
with the specific port number given. So, all client will be in the same chatroom, if they registered
with the similar port number. After they have entering the chatroom, they can chatting and
sharing images among them.
1
4.0 METHODOLOGY
Since we are using Java Language to produce Client/Server terminal for our chatroom,
here we also include the code for attaching image in the chatroom:
BufferedImage
For transferring image in the chat application, there are some line of code that has to be added.
The image will be send to the server and it will broadcasted to the other clients using socket.
Java BufferedImage class is a subclass of Image class. It is used to handle and manipulate the
image data. It also need ImageIO which is the class containing static convenience methods for
locating ImageReaders and ImageWriters, and performing simple encoding and decoding. The
BufferedImage coding is located in the Server program to run and it is output in a Client
program.
Sample code:
In Server:
BufferedImage
img=ImageIO.read(ImageIO.createImageInputStream(socket.getInputSt
ream()));
System.out.println("Image received!!!!");
lblimg.setIcon(img);
In Client:
ImageIcon img1=new ImageIcon("PIC2.jpg");
Image img = img1.getImage();
Image newimg = img.getScaledInstance(100, 120
java.awt.Image.SCALE_SMOOTH);
ImageIconnewIcon = new ImageIcon(newimg);
2
System.out.println("Image sent!!!!");
Tools Description
1. Microsoft Windows Operating System of student laptop
2. Eclipse Luna 4.4.1 Main Software for creating the Client/Server
terminal
6.0 RESULTS
In this section it will contain the information of the developed transferring image using
java. We have produce the prototype using Java Language by Eclipse. The coding has been
executed successfully. The application should consisted the following pages;
3
The screenshots:
Server interface
4
2. Chat Server Window
The client need to enter username and port number. The other client who wants to join the
conversation need to use the same port number.
5
3. Chat Server Window
After all the clients have connected to the server, all the conversation will be appeared at the server
interface
6
4. Chat Server Window
7
5. Chat Client Window
If the client wants to exit from the chatroom, they have to click the logout button
8
6. Chat Server Window
The server will receive the action and notify other clients.
9
7. Chat Client Window
To know the available chatters, the client might click on “Who is in” button.
10
8. Chat Client Window
The attach picture button is for transferring image to other clients through the server. The server will
notify the client whether the image is receive or not and then broadcast to other client to view or
download.
7. 0 CONCLUSION
11
12
APPENDIX
Coding
Server.java
importjava.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
importjava.text.SimpleDateFormat;
importjava.util.*;
importjavax.imageio.ImageIO;
13
sdf = new SimpleDateFormat("HH:mm:ss");
al = new ArrayList<ClientThread>();
}
while(keepGoing)
{
display("Server waiting for Clients on port " + port
+ ".");
if(!keepGoing)
break;
ClientThread t = new ClientThread(socket);
al.add(t);
t.start();
}
try {
serverSocket.close();
for(inti = 0; i<al.size(); ++i) {
ClientThreadtc = al.get(i);
try {
tc.sInput.close();
tc.sOutput.close();
tc.socket.close();
14
}
catch(IOExceptionioE) {
}
}
}
catch(Exception e) {
display("Exception closing the server and clients: "
+ e);
}
}
catch (IOException e) {
String msg = sdf.format(new Date()) + " Exception on new
ServerSocket: " + e + "\n";
display(msg);
}
}
try {
new Socket("localhost", port);
}
catch(Exception e) {
}
}
15
System.out.println(time);
else
sg.appendEvent(time + "\n");
}
16
}
}
Server server = new Server(portNumber);
server.start();
}
Socket socket;
ObjectInputStreamsInput;
17
ObjectOutputStreamsOutput;
int id;
String username;
ChatMessage cm;
String date;
ClientThread(Socket socket) {
id = ++uniqueId;
this.socket = socket;
System.out.println("Thread trying to create Object
Input/Output Streams");
try
{
sOutput = new
ObjectOutputStream(socket.getOutputStream());
sInput = new
ObjectInputStream(socket.getInputStream());
username = (String) sInput.readObject();
display(username + " just connected.");
}
catch (IOException e) {
display("Exception creating new Input/output Streams:
" + e);
return;
}
catch (ClassNotFoundException e) {
}
date = new Date().toString() + "\n";
}
18
booleankeepGoing = true;
while(keepGoing) {
try {
cm = (ChatMessage) sInput.readObject();
}
catch (IOException e) {
display(username + " Exception reading Streams:
" + e);
break;
}
catch(ClassNotFoundException e2) {
break;
}
String message = cm.getMessage();
switch(cm.getType()) {
caseChatMessage.MESSAGE:
broadcast(username + ": " + message);
break;
caseChatMessage.LOGOUT:
display(username + " disconnected with a LOGOUT
message.");
keepGoing = false;
break;
caseChatMessage.WHOISIN:
writeMsg("List of the users connected at " +
sdf.format(new Date()) + "\n");
for(inti = 0; i<al.size(); ++i) {
ClientThreadct = al.get(i);
writeMsg((i+1) + ") " + ct.username + "
since " + ct.date);
19
}break;
caseChatMessage.AttachPic:
System.out.println("IMAGE IS RECEIVED.");
//writeMsg("List of the users connected at " +
sdf.format(new Date()) + "\n");
break;
}
}
remove(id);
close();
}
privatebooleanwriteMsg(String msg) {
if(!socket.isConnected()) {
20
close();
return false;
}
try {
sOutput.writeObject(msg);
}
catch(IOException e) {
display("Error sending message to " + username);
display(e.toString());
}
return true;
}
}
}
Client.java
import java.net.*;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
import java.io.*;
importjava.util.*;
21
privateObjectOutputStreamsOutput; // to write on the socket
private Socket socket;
privateClientGUI cg;
this.cg = cg;
}
publicboolean start() {
try {
socket = new Socket(server, port);
}
22
catch(Exception ec) {
display("Error connectiong to server:" + ec);
return false;
}
try
{
sInput = new ObjectInputStream(socket.getInputStream());
sOutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOExceptioneIO) {
display("Exception creating new Input/output Streams: " +
eIO);
return false;
}
newListenFromServer().start();
try
{
sOutput.writeObject(username);
}
catch (IOExceptioneIO) {
display("Exception doing login : " + eIO);
disconnect();
23
return false;
}
return true;
}
voidsendMessage(ChatMessagemsg) {
try {
sOutput.writeObject(msg);
}
catch(IOException e) {
display("Exception writing to server: " + e);
}
}
24
if(sOutput != null) sOutput.close();
}
catch(Exception e) {}
try{
if(socket != null) socket.close();
}
catch(Exception e) {}
if(cg != null)
cg.connectionFailed();
intportNumber = 1111;
String serverAddress = "localhost";
String userName = "Anonymous";
switch(args.length) {
case 3:
serverAddress = args[2];
case 2:
try {
portNumber = Integer.parseInt(args[1]);
}
catch(Exception e) {
System.out.println("Invalid port number.");
25
System.out.println("Usage is: > java Client
[username] [portNumber] [serverAddress]");
return;
}
case 1:
userName = args[0];
case 0:
break;
default:
System.out.println("Usage is: > java Client
[username] [portNumber] {serverAddress]");
return;
}
if(!client.start())
return;
26
while(true) {
System.out.print("> ");
if(msg.equalsIgnoreCase("LOGOUT")) {
client.sendMessage(new
ChatMessage(ChatMessage.LOGOUT, ""));
break;
}
else if(msg.equalsIgnoreCase("WHOISIN")) {
client.sendMessage(new
ChatMessage(ChatMessage.WHOISIN, ""));
}
else if(msg.equalsIgnoreCase("AttachPic")) {
client.sendMessage(new
ChatMessage(ChatMessage.AttachPic, ""));
}
else {
client.sendMessage(new
ChatMessage(ChatMessage.MESSAGE, msg));
}
}
client.disconnect();
}
27
while(true) {
try {
String msg = (String) sInput.readObject();
if(cg == null) {
System.out.println(msg);
System.out.print("> ");
}
else {
cg.append(msg);
}
}
catch(IOException e) {
display("Server has close the connection: " +
e);
if(cg != null)
cg.connectionFailed();
break;
}
catch(ClassNotFoundException e2) {
}
}
}
}
}
ServerGui.java
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
28
importjavax.swing.JFileChooser;
importjava.io.File;
ServerGUI(int port) {
super("Chat Server");
server = null;
JPanel north = new JPanel();
north.add(new JLabel("Port number: "));
tPortNumber = new JTextField(" " + port);
north.add(tPortNumber);
stopStart = new JButton("Start");
stopStart.addActionListener(this);
north.add(stopStart);
add(north, BorderLayout.NORTH);
29
event.setEditable(false);
appendEvent("Events log.\n");
center.add(new JScrollPane(event));
add(center);
addWindowListener(this);
setSize(400, 600);
setVisible(true);
}
voidappendRoom(String str) {
chat.append(str);
chat.setCaretPosition(chat.getText().length() - 1);
}
voidappendEvent(String str) {
event.append(str);
event.setCaretPosition(chat.getText().length() - 1);
30
int port;
try {
port = Integer.parseInt(tPortNumber.getText().trim());
}
catch(Exception er) {
appendEvent("Invalid port number");
return;
}
server = new Server(port, this);
newServerRunning().start();
stopStart.setText("Stop");
tPortNumber.setEditable(false);
}
31
}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
ClientGUI.java
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
32
privateJTextFieldtf;
privateJTextFieldtfServer, tfPort;
privateJButton login, logout, whoIsIn, AttachPic;
privateJTextArea ta;
privateboolean connected;
private Client client;
privateintdefaultPort;
private String defaultHost;
super("Chat Client");
defaultPort = port;
defaultHost = host;
33
tf = new JTextField("Enter your name here");
tf.setBackground(Color.PINK);
northPanel.add(tf);
add(northPanel, BorderLayout.NORTH);
34
southPanel.add(AttachPic);
add(southPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 600);
setVisible(true);
tf.requestFocus();
35
Object o = e.getSource();
if(o == logout) {
client.sendMessage(new ChatMessage(ChatMessage.LOGOUT,
""));
return;
}
if(o == whoIsIn) {
client.sendMessage(new ChatMessage(ChatMessage.WHOISIN,
""));
return;
}
if(o == AttachPic) {
client.sendMessage(new ChatMessage(ChatMessage.AttachPic,
""));
return;
}
if(connected) {
client.sendMessage(new ChatMessage(ChatMessage.MESSAGE,
tf.getText()));
tf.setText("");
return;
}
if(o == login) {
String username = tf.getText().trim();
if(username.length() == 0)
return;
String server = tfServer.getText().trim();
if(server.length() == 0)
return;
String portNumber = tfPort.getText().trim();
36
if(portNumber.length() == 0)
return;
int port = 0;
try {
port = Integer.parseInt(portNumber);
}
catch(Exception en) {
return;
}
login.setEnabled(false);
logout.setEnabled(true);
whoIsIn.setEnabled(true);
AttachPic.setEnabled(true);
tfServer.setEditable(false);
tfPort.setEditable(false);
tf.addActionListener(this);
}
37
}
ChatMessage.java
import java.io.*;
publicclassChatMessageimplements Serializable {
protectedstaticfinallongserialVersionUID = 1112122200L;
staticfinalintWHOISIN = 0, MESSAGE = 1, LOGOUT = 2, AttachPic = 3;
privateinttype;
private String message;
intgetType() {
returntype;
}
String getMessage() {
returnmessage;
}
}
38