Distributed Computing Project Report
Distributed Computing Project Report
PROJECT REPORT
DISTRIBUTED COMPUTING IN
TRANSFERRING IMAGE USING JAVA LANGUAGE
NAME
ANIS NUR HAMIZAH BINTI KAMARUDIN
RAJA NURUL IZZATI BINTI RAJA HANAFIAH
1.0 INTRODUCTION
0
ID NUMBER
17173
17236
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.
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.
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.getInputS
tream()));
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);
ImageIcon newIcon = new ImageIcon(newimg);
Description
1. Microsoft Windows
6.0 RESULTS
6.1 PROJECT DELIVERABLES
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;
1. Chat Client Window
2. Chat Server Window
3. Chat Message Window
The screenshots:
1. Chat Server Window
Server interface
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.
After all the clients have connected to the server, all the conversation will be
appeared at the server interface
If the client wants to exit from the chatroom, they have to click the logout button
The server will receive the action and notify other clients.
10
To know the available chatters, the client might click on Who is in button.
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
Chatting room Application is one of the Architectural Model in Distributed Computing.
We have almost successfull create the Chat Room and transferring the image between the
chatters. We also have create the chat room using Java that have Client and Server terminal and
using th BufferedImage method.
12
13
APPENDIX
Coding
Server.java
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.imageio.ImageIO;
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(int i = 0; i < al.size(); ++i) {
ClientThread tc = al.get(i);
try {
tc.sInput.close();
tc.sOutput.close();
tc.socket.close();
}
catch(IOException ioE) {
15
}
}
}
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) {
}
}
16
sg.appendEvent(time + "\n");
}
17
}
Server server = new Server(portNumber);
server.start();
}
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
int id;
18
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";
}
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()) {
case ChatMessage.MESSAGE:
broadcast(username + ": " + message);
break;
case ChatMessage.LOGOUT:
display(username + " disconnected with a LOGOUT
message.");
keepGoing = false;
break;
case ChatMessage.WHOISIN:
writeMsg("List of the users connected at " +
sdf.format(new Date()) + "\n");
for(int i = 0; i < al.size(); ++i) {
ClientThread ct = al.get(i);
writeMsg((i+1) + ") " + ct.username + "
since " + ct.date);
}break;
case ChatMessage.AttachPic:
System.out.println("IMAGE IS RECEIVED.");
20
break;
}
}
remove(id);
close();
}
21
try {
sOutput.writeObject(msg);
}
catch(IOException e) {
display("Error sending message to " + username);
display(e.toString());
}
return true;
}
}
}
Client.java
import java.net.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
22
this.cg = cg;
}
try {
socket = new Socket(server, port);
}
catch(Exception ec) {
display("Error connectiong to server:" + ec);
23
return false;
}
try
{
sInput = new ObjectInputStream(socket.getInputStream());
sOutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException eIO) {
display("Exception creating new Input/output Streams: " + eIO);
return false;
}
new ListenFromServer().start();
try
{
sOutput.writeObject(username);
}
catch (IOException eIO) {
display("Exception doing login : " + eIO);
disconnect();
return false;
}
24
return true;
}
try{
if(socket != null) socket.close();
}
catch(Exception e) {}
if(cg != null)
cg.connectionFailed();
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.");
System.out.println("Usage is: > java Client
[username] [portNumber] [serverAddress]");
return;
}
26
case 1:
userName = args[0];
case 0:
break;
default:
System.out.println("Usage is: > java Client [username]
[portNumber] {serverAddress]");
return;
}
if(!client.start())
return;
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();
}
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFileChooser;
import java.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);
addWindowListener(this);
setSize(400, 600);
30
setVisible(true);
}
int port;
try {
port = Integer.parseInt(tPortNumber.getText().trim());
}
catch(Exception er) {
appendEvent("Invalid port number");
31
return;
}
server = new Server(port, this);
new ServerRunning().start();
stopStart.setText("Stop");
tPortNumber.setEditable(false);
}
ClientGUI.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
super("Chat Client");
defaultPort = port;
defaultHost = host;
34
centerPanel.add(new JScrollPane(ta));
ta.setEditable(false);
add(centerPanel, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 600);
setVisible(true);
tf.requestFocus();
35
36
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();
if(portNumber.length() == 0)
return;
int port = 0;
try {
port = Integer.parseInt(portNumber);
}
catch(Exception en) {
37
return;
}
login.setEnabled(false);
logout.setEnabled(true);
whoIsIn.setEnabled(true);
AttachPic.setEnabled(true);
tfServer.setEditable(false);
tfPort.setEditable(false);
tf.addActionListener(this);
}
}
ChatMessage.java
import java.io.*;
public class ChatMessage implements Serializable {
protected static final long serialVersionUID = 1112122200L;
38
39