You are on page 1of 6

UNIERSITY OF THE GAMBIA

COMPUTER PROGRAMING II (JAVA)

INSTANT MESSAGING APPLICATION

Landing Jatta
November 24, 2015

Abstract
This project is a java Graphical user interface(GUI) chat appli-
cation that uses java’s networking abilities to enable end users chat
to each other via a network. It consisting of two programs, a client
and a server. the client to connect to the server using the server Ip
address(numbers that enable any computer on the internet to find any
other computer on the network) and port number(.

1
Contents
1 Introduction 3

2 Design 3

3 implementation 4

4 Conclusion 5

Appendix:

A Future Work 5

B Classes 6

2
1 Introduction
The aim of this project is to fullfill the requirenet of the computer programing
II (Java) course and to demonstrate java networking capabilities by using
sockets and stream base communication, which enables application to view
networking as streams of data.Streams sockets are connection oriented.

2 Design
The program generally is a GUI that make use of the JTextArea of the swing
class to display the messages during the conversation and the JTextField
which enable the user to send messages. it also cconsist of two parts, a
server and a a client.the client and the server have some common instance
variables namely: JTextField and JTextArea from the Swing class, Object-
InputStream,ObjectOutputStream and sockets from the net class.The server
also consist of a ServerSocket instance variable to setup the port number
and the number of users allow to connect to the server and a constructor
to instanciant the instance variables and eight methods namely StartRun-
ningServer(),waitforConnection(),setupStreams(), whileChatting(),

public void startRunningServer ( ) {


try {
// p o r t number and how many can c o n n e c t )
s e r v e r= new S e r v e r S o c k e t ( 6 7 8 9 , 1 0 0 ) ;
while ( true ){
try {
waitForConnection ( ) ;
setupStreams ( ) ;
whileChatting () ;
}
c a t c h ( EOFException e ) {
showMessage ( ” \ n S e r v e r ended t h e c o n n e c t i o n ” ) ;

Listing 1: The startRunningServer method


closeConnection(),sendMessage(), showMessage(), ableTotype(). Moreover,
the client also consist of a server Ip instance variable to instanciant the ip
address of the server.

// c o n n e c t t o s e r v e r

3
p r i v a t e v o i d c o n n e c t T o S e r v e r ( ) throws
UnknownHostException , IOException {
showMessage ( ” \n Attempting Connection ” ) ;
c o n n e c t i o n= new S o c k e t ( I n e t A d d r e s s . getByName ( s e r v e r I P )
,6789) ;
showMessage ( ” \n Connected t o ”+c o n n e c t i o n .
g e t I n e t A d d r e s s ( ) . getHostName ( ) ) ;
}

Listing 2: The connectToServer method of the client

3 implementation
The streams ,sockets and the swingUtilities class are amonng the crucial part
of the application,Streams enable a processes establish a connection with
another processes by sending and recieving btes of data.the server invokes
method getOutput stream on the socket to get a refrence to the socket’s
output stream and the flush method push any leftover bits of infomation
on the stream, it also invokes method getinput stream on the socket to get
a refrence to the sockets input stream. Sockets on the other hand is and
inferface between the application layer and the transport layer.
p r i v a t e v o i d s e t u p S t r e a m s ( ) throws IOException {
output= new ObjectOutputStream ( c o n n e c t i o n .
getOutputStream ( ) ) ;
output . f l u s h ( ) ;
i n p u t= new ObjectInputStream ( c o n n e c t i o n . g e t I n p u t S t r e a m ( )
);
showMessage ( ” \n s t r e a m s a r e not setUp ” ) ;
}

Listing 3: setupStreams method


The swingUtilities class uses a thread and it’s invokeLater method to
update a portion of the Graphical User interface instead of creating a new
one every time the a messages pop’s up.
p r i v a t e v o i d showMessage ( f i n a l S t r i n g t e x t ) {
S w i n g U t i l i t i e s . invokeLater (

new Runnable ( ) {
p u b l i c v o i d run ( ) {
chatWindow . append ( t e x t ) ;
}
}
);

4
Listing 4: the showMessage method that updates a portion of the GUI
The ableTotype method of both the server and the client take a boolean
arguement as on it’s parameter and will not allow the user to start chatting
without being Connected to the server it also implements Runnable on the
swing SwingUtilities class.
p r i v a t e v o i d ableToType ( f i n a l b o o l e a n f a l ) {
S w i n g U t i l i t i e s . invokeLater (

new Runnable ( ) {
p u b l i c v o i d run ( ) {
userText . s e t E d i t a b l e ( f a l ) ;

}
}

Listing 5: ableTotype method

4 Conclusion
The project help me to know some of the java’s powerful networking capa-
bilities and their application to networking at first hand, it has beeen very
educative and fun and i hope the user of the will serve it’s users the purpose
it was buiild for.

A Future Work
The project in coming versions will also contain a a user login inferface and
a database to manage to store the information of users and I will also want
to add the capabilities of allowing users to send videos, mp3 files, zip files
documents etc.

5
B Classes
Class Name Description
Server it is the implenentation class Initates the con-
nection and wait for the client to connect
ServerTest [l]Tester class to execute the server
Client [l] it contains the implementaton class of the
client,the client use it to Connect to server
using the server’s Ip address
ClientTest [l]testr class of the client

Table 1: List of Added classes

You might also like