Chat Server
Chat Server
Chat Server
CONTENTS
INTRODUCTION
ANALYSIS
TOOLS OVERVIEW
o NETWORKING
o GUI
o DATABASE
DFD
IMPLEMENTATION
o DESIGN FORM
o CODE
o USER MANUAL
SYSTEM SPECIFICATION
APPLICATION & LIMITATION
REFERENCES
Project Report
Chat Server
1. Introduction
Client server computing is a phrase used to describe a model for
computer networking. This model offers an efficient way to provide
information and services to many users. A network connection only made
when information need to be accessed by user. This lack of continuous
network connection provides network efficiency.
In client server computing, processes are divided between the client
and server. This relationship based on series of request and responses.
client computer.
Project Report
Chat Server
Request
Response
Send a request
Process a request
Read Result
Project Report
Chat Server
E-Mail: - A mail client program enables the user to interact with a server in
order to access, read and send an electronic mail messages.
In Summary: - Client/server computing is a common networking
model, which enables many users to access information in an efficient
manner. Generally, the user computer is called client and the machine that
contains the information being accessed is called server. The client program
runs an application called client program. A client program enables the user
send request fir information to the server and read the results that server send
back. The server computer runs a server program, which processes the
requests and sends the results back to the client. Most Internet transaction,
such as FTP, E-Mail and accessing web pages are based on client/server
networking.
Project Report
Chat Server
2. ANALYSIS
In this project we are going to design an application of socket
programming through which number of clients can communicate with each
other with the help of server. This is the main objective of this project.
For this project we are going to use java as front-end and SQL
database as backend tool. To implement this project we use Windows 2000
as software.
The requirement specification of this project is Windows 98,Windows
NT, and Windows 2000, javas JDK1.3 version, SQL as database to store
users IDs and passwords and floppy drive and CD-drive.
It is a global chat application for Windows sockets. It allows two
clients to carry on a conversation using a server as the intermediary. Chat
applications allow inexpensive and efficient social interactions between
communities members spread across the globe. A community can use the
Chat application to allow its members to have an interactive session with a
special guest or a community member through the means of a moderated
Project Report
Chat Server
chat. A customer support site can use the Chat application to offer instant
responses to its customers regarding its products.
For e.g. if any person have urgent information which is in one place
and hence wants to talks with another person for that purpose chat server is
useful. Because of this the time is spend less.
3. TOOL OVERVIEW
3.1 NETWORKING:
Client-Server Definition:
Client/server is a computational architecture that involves the client
processes
requesting services
from server
processes.
Client/server
Project Report
Chat Server
A Client Process: The client is a process (program) that sends a message to a server
process (program), requesting that server perform a task (services), Client
program usually manage the user-interface portion of the application,
validate data entered by the user, dispatch request to the server programs,
and sometimes executes business logic. The client-based process is the frontend application that user sees and interacts with. The client process contains
solution specific logic and provides the interface between the user and the
rest of the application system. The client process also manages the local
resources that the users interact with such as the monitor, keyboard,
workstation CPU and peripherals. One of the key elements of a client
workstation is the Graphical User Interface (GUI). Normally a part of
operating system i.e. the windows manager detects user actions, manages the
windows on the displays the data in a windows.
A Server Process: A server process (program) fulfills the client request by performing
the task requested. Server programs generally receive request from a client
program, execute database retrieval and updates, and manage data integrity
and dispatch responses to the client requests. Sometimes server programs
execute common or complex business logic. The server-based process may
run on another machine on the network. This server could be host operating
system or network file server; the server is then provided both the file
Institute of Petrochemical Engineering, Lonere
Project Report
Chat Server
Interconnection
(OSI)
and
Government
Open
System
Project Report
Chat Server
Client-Server System:
Client
Client
Client
Network
Client
Server
Front-End
SQL user
interface
Forms
interface
Report
Writer
Graphical
interface
Interface
SQL engine
Project Report
Chat Server
Back End
10
Project Report
Chat Server
11
Project Report
Chat Server
calls from the client are enclosed in a single transaction at the server end.
Thus, if the transaction aborts, the server can undo the effect of the
individual remote procedure calls
Socket Connection:
A socket is an abstraction through which an application may send and
receive data, in much the same way as an open file allows an application to
read and write data to stable storage. A socket is a software endpoint of
communication.
For e.g.: an electrical circuit board has to have a socket to enable it to
send current to a TV. Similarly the TV has a socket. The connection plug is
fixed at one end into the TV socket and at the other end into the circuit board
socket.
Information written to the socket by an application on one machine
can be read by application on a different machine and vice versa.
This application layer on one computer has to create a socket in order
to connect to the application layer on another computer sockets at both ends
seems to be connected horizontally between application layers. But, actually,
the socket is bound to a software port. It is the duty of the lower layers to
route data to the appropriate port.
constructor is
12
Project Report
Chat Server
13
Project Report
Chat Server
sending
bytes
(such
as
ObjectOutputStream
and
14
Project Report
Chat Server
communicate via
inputStream and OutputStream objects.
Step 4:
15
Project Report
Chat Server
16
Project Report
Chat Server
Connect(): Establishes a connection between the given the socket and the
remote socket associated with the foreign address, if any. Upon returning
successfully, the given sockets local and remote IP address and port
information are filled in. if the socket was not previously bound to a local
port, one is assigned randomly. For TCP sockets, connect() returns
successfully only after completing a handshake with the remote TCP
implementation; success implies the existence of a reliable channel to that
socket.
Listen(): Indicate the given socket is ready to accept incoming connection.
The socket must already be associated with the local port. After this call,
incoming TCP connection request addressed to the given local port will be
completed and queued until they are passed to the program via accept().
Accept(): Blocks waiting for connections addressed to the IP address and
port to which the given socket is bound. When a connection arrives and the
TCP handshake is successfully completed, a new socket have been filled in
with the local port number of the new socket, and the addressed information
has been returned in the sockaddr structure.
Send(): Sends the bytes contained in the buffer over the given socket. The
socket must be in a connected state. When the call returns, the data has been
queued for transmission over the connection. For a stream socket, the data
will eventually be transmitted, provided the connection closes normally. For
a datagram socket there are no guarantees of delivery. However, the data
from a single send() call will never be split across multiple recv() calls. The
return value indicates the number of bytes actually transmitted. The flags
Institute of Petrochemical Engineering, Lonere
17
Project Report
Chat Server
argument allows various special protocol features, such as out of band data,
to be accessed.
Sendto(): Sends the bytes contained in the buffer over the given the socket.
To use sendto() on a TCP socket requires a socket to be in the connected
state.
Recv(): Copies up to a specified number of bytes, received on the socket,
into a specified location. The given socket must be in the connected state.
Normally, the call blocks until either at least one byte is returned or the
connection closes. The return value indicates the number of bytes actually
copied into the buffer starting at the pointed to location. For a stream socket,
the bytes are delivered in the same order as they were transmitted, without
omission. For a datagram socket, each recv() returns the data from at most
one send() and order is not necessarily preserved. If the buffer provided to
recv() is not big enough for the next available datagram, the datagram is
silently truncated to the size of the buffer.
Recvfrom(): Copies up to a specified number of bytes, received on the
socket, into a specified location. To use recvfrom() on a TCP socket requires
the socket to be in the connected state.
Close(): Terminates communication on a socket. The socket is marked to
disallow further sends and receives.
Shutdown(): Terminates communication on a socket. The socket is marked
to disallow further sends and receives, or both, according to the second
Institute of Petrochemical Engineering, Lonere
18
Project Report
Chat Server
Exception Handling:
Exception handling should be used to process only exceptional
situations where a method to complete its task for reasons it cannot control.
To process exceptions from program components that are not geared to
handling those exceptions directly. To process exceptions from software
components such as methods, libraries and classes that are likely to be used
and where those components cannot handle their own exceptions. On large
projects to handle exceptions in a uniform manner project wide. Exception is
an indication that a problem occurred during the programs execution. Using
java exception handling enables the programmer to remove the error
handling code from the main line of the programs execution. This improves
program clarity and enhances modifiability.
Java exception handling enables a program to catch all types of
exceptions, or to catch all exceptions of a certain type, or to catch all
exceptions of related types. Exception handling is provided to enable
programs to catch and handle errors rather than letting them occur and
suffering the consequences. Exception handling is designed for error
processing, which is an infrequent activity that is often used because a
program is about to terminate.
Exception handling helps improve a programs fault tolerance. It
becomes more pleasant to write error-processing code, so programmers
are more likely to provide it. Exception handling is particularly well suited
Institute of Petrochemical Engineering, Lonere
19
Project Report
Chat Server
20
Project Report
Chat Server
Class thread:
Class thread (package java.lang) has several constructors. The
constructor public Thread (string threadName) constructs a Thread objects
whose name is threadName. The constructor public Thread constructs a
Thread whose name is Thread concatenated with a number, like Thread-1,
Thread-2 and so on.
The code that does the real work of a thread is placed in its run
method. The run method can be overridden in a subclass of Thread
A program launches a threads execution by calling the threads start
method, which in turn, calls the run method. After start launches the thread,
start returns to its caller immediately. The caller then executes concurrently
with
the
launched
thread.
The
start
method
throws
an
21
Project Report
Chat Server
that controls the thread is placed in the run method. We create a thread with
the new class by passing to the Thread class constructor public Thread
(Runnable runnableobject)
A reference to an object of the class that implements the Runnable
interface. The Thread constructor registers the run method of the
runnableobject as the method to be invoked when the thread begins
execution.
3.2 Graphical User Interface(GUI):
BASIC GRAPHICAL USER INTERFACE COMPONENT:
22
Project Report
Chat Server
Component
Description
JLabel
An area where uneditable text or icons can
JTextField
be displayed.
An area in which the user inputs data from
the keyboard. The area can also display
JButton
JCheckBox
information.
An area that triggers an event when clicked.
A GUI component that is either selected or
not selected.
JComboBox A drop-down list of items from which the
user can make a selection by clicking an
item in the list or by typing into the box, if
JList
permitted.
An area where a list of items displayed from
which the user can make a selection by
clicking once on any element in the list.
Double Clicking an element in the list
generates an action event. Multiple elements
JPanel
can be selected.
A container in which components can be
placed.
3.6.1.Swing Overview: The classes that are used create the GUI components are part of the
Swing GUI components from package javax.swing. These are the newest
GUI components of the Java 2 platform. Swing components are written,
manipulated and displayed completely in java (so called pure Java
components).
23
Project Report
Chat Server
24
Project Report
Chat Server
javax.swing.JComponent
25
Project Report
Chat Server
class Container is setLayout, which has been used to specify the layout
manager that helps a Container position and size its components.
Swing component that subclass JComponent have many features, including:
i.
A pluggable look and feel that can be used to customized the look and
feel when the program executes on different platforms.
ii.
iii.
iv.
v.
vi.
26
Project Report
Chat Server
27
Project Report
Java.lang.Object
Java.util.EventObject
Java.awt.AWTEvent
Chat Server
AdjustmentEvent
FocusEvent
ItemEvent
PaintEvent
ComponentEvent
WindowEvent
InputEvent
keyEvent
mouseEvent
28
Project Report
Chat Server
JLabel:
Labels provide text instructions or information on a GUI. Labels are
defined with class JLabel a subclass of JComponent. A label displays a
single line of read only text. Once labels are created, programs rarely
change a labels contents.
JButton:
A button is a component the user clicks to trigger a specific action. A
java program can use several types of buttons, including command
buttons, check boxes, toggle buttons and radio buttons. A command
button generates an ActionEvent when the user clicks the button with the
mouse. Command buttons are created with class JButton, which inherits
from class abstracts Button. The text on the face of a JButton is called a
button label. A GUI can have many JButtons, but each button label
should typically the unique.
JList:
29
Project Report
Chat Server
A list displays a series of items from which the user may select one or
more items. Lists are created with class JList, which inherits from class
JComponent. Class JList supports single selection lists
Panels:
Complex GUIs require that each component be placed in an exact
location. They often consist of multiple panels with each panel
components arranged in a specific layout. Panels are created with class
JPanel a subclass of JComponent. Class JComponent inherits from claas
java.awt.Container, so every JPanel is a Container. Thus JPanels may
have components, including other panels, added to them.
JTextArea:
JTextArea provide an area for manipulating multiple lines of the text.
Like class JTextField, class JTextArea inherits from the JTextComponent,
which defines common methods for JTextField, JTextAreas and other
several text-based GUI component.
Layout Managers:
Layout managers are provided to arrange GUI components on a
container for presentation purposes. The layout managers provide basic
layout capabilities that are easier to use than determining the exact
30
Project Report
Chat Server
position and size of every GUI component. This enables the programmer
to concentrate on the basic look and feel and lets the layout managers
process most of the layout details.
Layout
manager
FlowLayout
Description
Default for java.applet.Applet java.awt.panel and
javax.swing.JPanel places components sequentially
in the order they were added .it is also possible to
specify the order of the components using the
container method add that takes a component and an
BorderLayout
GridLayout
FlowLayout:
FlowLayout is the most basic layout manager. GUI components are
placed on a container from left to right in the order in which they are
added to the container. When the edge of the container is reached,
components to be left aligned centered and right aligned.
BorderLayou:t
The BorderLayout layout manager arranges components into five
regions. North, South, East, West and Center. Class BorderLayout
inherits from object and implements interface LayoutManager. Up to five
Institute of Petrochemical Engineering, Lonere
31
Project Report
Chat Server
directly
from
class
object
and
implements
interface
3.6.3.Adapter Classes:
32
Project Report
Chat Server
33
Project Report
Chat Server
34
Project Report
Chat Server
35
Project Report
Chat Server
ODBC. The ODBC API offers connectivity to almost all databases on almost
all platforms and is the most widely used programming interface for
accessing relational databases. But ODBC cannot be used directly with Java
programs due to various reasons.
ODBC uses a C interface. This has drawbacks in security,
implementation, robustness, etc. ODBC makes use of pointers (which have
been removed from Java).
Hence JDBC came into existence. If you've done database
programming using Visual Basic, then you will be familiar with ODBC. You
can connect a VB application to an Access database or an Oracle table
directly via ODBC. Since Java is a product of Sun Microsystems, you have
to make use of JDBC along with ODBC in order to develop Java database
applications. JDBC is a set of Java APIs for executing SQL statements. This
API consists of a set of classes and interfaces to enable programmers to
write pure database applications.
Advantages of database systems
36
Project Report
Chat Server
37
Project Report
Chat Server
38
Project Report
Chat Server
4. System Analysis
Institute of Petrochemical Engineering, Lonere
39
Project Report
Chat Server
4.1.Flow of Chat-Server:
As the name suggests chat-server means, multiple users can chat with
each other. For that purpose, we have invented a concept server and client. A
server is nothing but a program, which handles the communication between
clients using the server socket class. What we have planned is we made a
simple program for server, which takes the entire request from users and
fulfill it. We made program for client, who only generates a request to server
and receive a fulfilled reply from server.
Server does the following things:
After you start the server program, it will look for the clients, which
want to connect to server. The lines of code for this purpose is:
If (server! =null)
{
System.out.println (listening for clients);
}
while (true)
{
socket client =server.accept();
in
=new
BufferReader(new
InputStreamReader(client.getInputStream()));
40
Project Report
Chat Server
Stream receivedString= ;
while (receivedString.equals( ))
{
receivedString=in.readLine();
}
If server finds any of the clients want to connect to server, it will
create a clientThread for a particular client. The thread maintains all the
communication made from server. When we create a clientThread, the
reference of server is being passed.
The clientThread runs continuously to see request received from
particular client. To distinguish between requests, we made proper protocol
with request. E.g.: Suppose one client wants to chat with others, the request
from client will be CMD_SEND_MESSAGE+from user+actual chat
message. From this, we can easily understand that what request has been
passed and now how to process on that request. We have written the methods
like sendMessageToAll(), sendMessage(), getIndex(), setIndex() for several
purpose. The sendMessageToAll() method takes a string parameter and it
calls the servers broadcast method.
To understand the servers broadcast method, the code is
public void broadcast(String msg)
{
41
Project Report
Chat Server
System.out.println(while bradcasting+clients.size());
for(int I=0;I<clients.size();I++)
{
clientThread c=(clientThread)client.elementAt(i);
c.sendMessage(msg);
}
}
Once client connects the server, server makes a entry in vector called
clients, because server has to look after all clients at a given time.
The broadcast method takes all the clients from client vector. Client
vector hold the clientThread. Once it gets all the clientThread, the
clientThread send message() method get in activation which does the
following.
The sendMessage()code should be written here:
public void sendMessage(String msg)
{
this.out.println(msg);
this.out.flush();
}
42
Project Report
Chat Server
43
Project Report
in
Chat Server
=new
BufferedReader
(new
InputStreamReader
(client.getInputStream()));
out =new PrintWriter (client.getOutputStream());
String sendString =CMD_GET_USER_LIST:;
out.println (sendString+userName);
out.flush ();
String receivedString =;
while (receivedString.equals ( )){
receivedString =in.readLine ();
}
}
When clientWindow connects to server, it sends the request of
currently logged clients. The server program processes using the clients
vector and sends the list of users using proper protocol that can be easily
understand by clientWindow program. When a client is generated, one
serverThread is generated to manipulate communication between this client
and server. Actually the clientThread and serverThread classes are logically
the same. They evolved for the same purpose.
The login class is made to log in the users. The user/client has to enter
valid username and password to access the chat server. Once user enters a
44
Project Report
Chat Server
valid user and password, the client window comes in activation. The rest of
the project is managed by both clientWindow and server class.
To manipulate users operations like adding of users, changing the
passwords of existing users and validating the username and password. We
build basic class users. The users class directly manipulates the database. It
has methods like add(), edit(), delete(), update() and find() method to work
on particular user. When user want to log in the chat server the users find
method comes in picture. It gives the record from user, but the name of
computer where our server program is running.
server started
client started
45
Project Report
Chat Server
client process
terminate.
validity
false
Chat window is
open for chatting
true
clients chat with
each other.
If any client
wants to logout
then cliks on
logout button.
client process
terminate.
end
46
Project Report
Chat Server
47
Project Report
Chat Server
5. Implementation
Figure 5.1 shows the server window, which is always listening for clients.
Figure 5.1
Figure
5.2
shows client
window. In
this
client
window user
must enter his/her ID & password for entering into chat window. When
password does not match with password, which is stored in database, then
server sends password does not agree message.
Figure 5.2
48
Project Report
Chat Server
Figure 5.3 shows client window when password match with the password
stored in the database then server opens chat window for that client.
Figure 5.3
Figure 5.4 shows the chat window in which number of clients can chat with
each other. In this window three clients are enter for chat. Names of those
clients are displayed on the right side of window. Incoming messages from
those clients are displayed on the upper side of the window.
49
Project Report
Chat Server
Figure 5.4
Figure 5.5 shows the window for enter new user. If new user enter then that
users ID & password is stored into database of the server.
Figure 5.5
Figure 5.6 shows new user that is enter for chat.
50
Project Report
Chat Server
Figure 5.6
Figure 5.7 shows the window for change the password of user.
Figure.5.7
Institute of Petrochemical Engineering, Lonere
51
Project Report
Chat Server
52
Project Report
Chat Server
7. Conclusion
As the name suggests chat-server means, multiple users can chat with
each other. For that purpose, we have invented a concept server and client. A
server is nothing but a program, which handles the communication between
clients using the server socket class. What we have planned is we made a
simple program for server, which takes the entire request from users and
fulfill it. We made program for client, who only generates a request to server
and receive a fulfilled reply from server.
53
Project Report
Chat Server
Reference
1. Java: How To Program: - Third edition
By Deitel and Deitel.
2. The Complete Reference of Java: - Fifth edition
54
Project Report
Chat Server
By Herbert Schield.
55