You are on page 1of 79

Tables of Content

Chapter 1: Introduction of project

PageNo:
2

Chapter 2: Study of System

Chapter 3: Java Programming

Chapter 4: MS-Access An-RDBMS

12

Chapter 5: Tables

14

Chapter 6: Hardware and Software requirement

16

Chapter 7:Coding docx.

18

Chapter 8: Snapshots docx.

62

Chapter 9: Data Base docx

72

CONCLUSION

73

REFERENCES

74

CHAPTER 1

Introduction of Project:Chat Server

Purpose of the Project:


This Project Entitled as INTERNET CHATTING is used basically for chatting purpose for the
remote clients or users on Internet or local networks. Here In this project, a java client/server
combination is used to chat with remote users. When a Client wants to chat with a user on a
remote host, he sends a request to the server with a identification name like chat-id, the server
responds to the request by identifying the client-id which is already registered in the server
domain and when matched his request is granted and the client can begin to chat with the remote
users present on the internet or local network.
The power of Internet is such that it integrates together LANs located across diverse software
and hardware forms into a single large communication network that spans the globe. The client
needs to have a client software such as Netscape Navigator or internet explorer to retrieve
information as well as chat on the www. WWW is referred to as Netsurfing and it supports
chatting, text, video and audio.
The benefit of using INTERNET CHATTING over other chatting tools is that, with the help of
java, the programmer can create applet applications which can be use the internet as a server.
Applets are machine independent and so java programs can run on any computer on the internet.
The term client/server is used in the context of networking, what it actually means.
It is important to understand the term client/server because the INTRANET CHATTING project
is supported completely by client/server model. A server is anything that has some recourse that
can be shared. There are compute servers, which provide computing power, web servers, which
store web pages. A client is simply any other entity that wants to gain access to a particular
server. The interaction between client/server is like an interaction between an electrical socket
and a lamp. The server is a permanently available recourse while client is free to unplug after it
has been served. The programs premier feature is its whiteboard drawing utility. You can draw
freehand, do circles, squares, lines, text, or paste image files to the canvas. This is ideal when
users want to sketch concepts for one another. Users can interact publicly with everyone else
on the server, or they can chat/draw privately using java Chats whisper mode. Users can
create and manage chat rooms, which can be either public or private. The server will also
store and manage answering machine-style messages for users who arent online, and instant
messages can be sent to those who are.

CHAPTER-2

Study of the system:


This application can be mainly divided into two modules:
1. Server
2. Client
This project Chat Server is mainly depended model. The client requests the server and server
responses by granting the clients request.
The proposed system should provide both of the above features along with the followed ones:

Server:
The server should be able to perform the following features:
The first and foremost problem is to find the server. We should identify the program in the server
which processes the clients request.
Administrator client who will be acting as a super user.
Creating of private room with the password facility to enable private chats with the users online.
The server is always waiting for clients requests. The clients come and go down but the server
remains the
same.

Client:
The client should be able to perform the following features:
Should be able to send message to anybody in the room with clients unique chat name created in
the server for chatting purpose.Should be provided with the drawing tools like free hand,
rectangle, oval, line and also sending text message over the room.
In all the network applications, we find two sort program where the first i.e., server sends the
information and the second i.e., client receives the information

Chapter-3

Java Programming
Java (with a capital J) is a high-level, third generation programming language, like C, Fortran,
Smalltalk, Perl, and many others. You can use Java to write computer applications that crunch
numbers, process words, play games, store data or do any of the thousands of other things
computer software can do.
Compared to other programming languages, Java is most similar to C. However although Java
shares much of C's syntax, it is not C. Knowing how to program in C or, better yet, C++, will
certainly help you to learn Java more quickly, but you don't need to know C to learn Java. Unlike
C++ Java is not a superset of C. A Java compiler won't compile C code, and most large C
programs need to be changed substantially before they can become Java programs.
What's most special about Java in relation to other programming languages is that it lets you
write special programs called applets that can be downloaded from the Internet and played
safely within a web browser. Traditional computer programs have far too much access to your
system to be downloaded and executed willy-nilly. Although you generally trust the maintainers
of various ftp archives and bulletin boards to do basic virus checking and not to post destructive
software, a lot still slips through the cracks. Even more dangerous software would be
promulgated if any web page you visited could run programs on your system. You have no way
of checking these programs for bugs or for out-and-out malicious behavior before downloading
and running them.
Java solves this problem by severely restricting what an applet can do. A Java applet cannot write
to your hard disk without your permission. It cannot write to arbitrary addresses in memory and
thereby introduce a virus into your computer. It should not crash your system.

Java is a Platform
Java is a platform for application development. Java solves the problem of platformindependence by using byte code. The Java compiler does not produce native executable code for
a particular machine like a C compiler would. Instead it produces a special format called byte
code.

Java is Simple
Java was designed to make it much easier to write bug free code. According to Sun's Bill Joy,
shipping C code has, on average, one bug per 55 lines of code. The most important part of
helping programmers write bug-free code is keeping the language simple.
Java has the bare bones functionality needed to implement its rich feature set. It does not add lots
of syntactic sugar or unnecessary features. Despite its simplicity Java has considerably more
functionality than C, primarily because of the large class library.
Because Java is simple, it is easy to read and write. Obfuscated Java isn't nearly as common as
obfuscated C. There aren't a lot of special cases or tricks that will confuse beginners.
About half of the bugs in C and C++ programs are related to memory allocation and
deallocation. Therefore the second important addition Java makes to providing bug-free code is
automatic memory allocation and deallocation. The C library memory allocation functions
malloc () and free () are gone as are C++'s destructors.
Java is an excellent teaching language, and an excellent choice with which to learn
programming. The language is small so it's easy to become fluent. The language is interpreted so
the compile-run-link cycle is much shorter. The runtime environment provides automatic
memory allocation and garbage collection so there's less for the programmer to think about. Java
is object-oriented unlike Basic so the beginning programmer doesn't have to unlearn bad
programming habits when moving into real world projects. Finally, it's very difficult (if not quite
impossible) to write a Java program that will crash your system, something that you can't say
about any other language.

Java is Object-Oriented
Object oriented programming is the catch phrase of computer programming in the 1990's.
Although object oriented programming has been around in one form or another since the Simula
language was invented in the 1960's, it's really begun to take hold in modern GUI environments
like Windows, Motif and the Mac. In object-oriented programs data is represented by objects.
10

Objects have two sections, fields (instance variables) and methods. Fields tell you what an object
is. Methods tell you what an object does. These fields and methods are closely tied to the object's
real world characteristics and behavior. When a program is run messages are passed back and
forth between objects. When an object receives a message it responds accordingly as defined by
its methods.
Object oriented programming is alleged to have a number of advantages including:

Simpler, easier to read programs

More efficient reuse of code

Faster time to market

More robust, error-free code

Java is Platform Independent


Java was designed to not only be cross-platform in source form like C, but also in compiled
binary form. Since this is frankly impossible across processor architectures Java is compiled to
an intermediate form called byte-code. A Java program never really executes natively on the host
machine. Rather a special native program called the Java interpreter reads the byte code and
executes the corresponding native machine instructions. Thus to port Java programs to a new
platform all that is needed is to port the interpreter and some of the library routines. Even the
compiler is written in Java. The byte codes are precisely defined, and remain the same on all
platforms.
The second important part of making Java cross-platform is the elimination of undefined or
architecture dependent constructs.
However the virtual machine itself and some parts of the class library must be written in native
code. These are not always as easy or as quick to port as pure Java programs.

11

Java is Safe
Java was designed from the ground up to allow for secure execution of code across a network,
even when the source of that code was untrusted and possibly malicious.
This required the elimination of many features of C and C++. Most notably there are no pointers
in Java. Java programs cannot access arbitrary addresses in memory. All memory access is
handled behind the scenes by the (presumably) trusted runtime environment. Furthermore Java
has strong typing. Variables must be declared, and variables do not change types when you aren't
looking. Casts are strictly limited to casts between types that make sense. Thus you can cast an
int to a long or a byte to a short but not a long to a boolean or an int to a String.
Java implements a robust exception handling mechanism to deal with both expected and
unexpected errors. The worst that an applet can do to a host system is bring down the runtime
environment. It cannot bring down the entire system.
Most importantly Java applets can be executed in an environment that prohibits them from
introducing viruses, deleting or modifying files, or otherwise destroying data and crashing the
host computer. A Java enabled web browser checks the byte codes of an applet to verify that it
doesn't do anything nasty before it will run the applet.

Java is High Performance


Java byte codes can be compiled on the fly to code that rivals C++ in speed using a "just-in-time
compiler." Several companies are also working on native-machine-architecture compilers for
Java. These will produce executable code that does not require a separate interpreter, and that is
indistinguishable in speed from C++.
While you'll never get that last ounce of speed out of a Java program that you might be able to
wring from C or Fortran, the results will be suitable for all but the most demanding applications.

12

Java is Multi-Threaded
Java is inherently multi-threaded. A single Java program can have many different threads
executing independently and continuously. Three Java applets on the same page can run together
with each getting equal time from the CPU with very little extra effort on the part of the
programmer.
There is a cost associated with multi-threading. Multi-threading is to Java what pointer
arithmetic is to C, that is, a source of devilishly hard to find bugs. Nonetheless, in simple
programs it's possible to leave multi-threading alone and normally be OK.
Java is Garbage Collected
You do not need to explicitly allocate or deallocate memory in Java. Memory is allocated as
needed, both on the stack and the heap, and reclaimed by the garbage collector when it is no
longer needed. There's no malloc (), free (), or destructor methods.
There are constructors and these do allocate memory on the heap, but this is transparent to the
programmer.The exact algorithm used for garbage collection varies from one virtual machine to
the next. The most common approach in modern VMs is generational garbage collection for
short-lived objects, followed by mark and sweep for longer lived objects. I have never
encountered a Java VM that used reference counting.

13

Chapter-4

14

MS-Access An RDBMS

A database is a collection of information that's related to a particular subject or purpose, such as


tracking customer orders or maintaining a music collection. If your database isn't stored on a
computer, or only parts of it are, you may be tracking information from a variety of sources that
you're having to coordinate and organize yourself.
For example, suppose the phone numbers of your suppliers are stored in various locations: in a
card file containing supplier phone numbers, in product information files in a file cabinet, and in
a spreadsheet containing order information. If a supplier's phone number changes, you might
have to update that information in all three places. In a database, however, you only have to
update that information in one place the supplier's phone number is automatically updated
wherever you use it in the database.
Using Microsoft Access, you can manage all your information from a single database file. Within
the file, you can use:

Tables to store your data.

Queries to find and retrieve just the data you want.

Forms to view, add, and update data in tables.

Reports to analyze or print data in a specific layout.

Data access pages to view, update, or analyze the database's data from the Internet or an
intranet.

15

Chapter-5

16

Tables
Tables and relationships
To store your data, create one table for each type of information that you track. To bring the data
from multiple tables together in a query, form, report, or data access page, define relationships
between the tables

Tables: Store and manage your data


A table is a collection of data about a specific topic, such as products or suppliers. Using a
separate table for each topic means that you store that data only once. This results in a more
efficient database and fewer data-entry errors

Queries
To find and retrieve just the data that meets conditions that you specify, including data from
multiple tables, create a query. A query can also update or delete multiple records at the same
time, and perform predefined or custom calculations on your data

Forms
To easily view, enter, and change data directly in a table, create a form. When you
open a form, Microsoft Access retrieves the data from one or more tables, and
displays it on the screen with the layout you choose in the Form Wizard or with the
layout that you created on your own in Design view.

Reports

17

To analyze your data or present it a certain way in print, create a report. For example, you might
print one report that groups data and calculates totals, and another report with different data
formatted for printing mailing labels

18

Chapter-6

19

Hardware and Software Requirements

Hardware Requirements
1. Pentium Processor
2. 2 G.B. RAM
3. 320 G.B. Hard disk
4. Keyboard
5. Mouse
6. Monitor
7. Printer
Software Requirements
1. Windows XP/7/8
2. JDK1.6/JDK1.7
3. Office 2007

20

Chapter-7

21

Coding Docx.

Server.java
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
class Server
{
public static void main (String arg[])
{
int pno=2000;
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
Print Stream ps;
ServerSocket ss;
Socket sk;
String rt,uid,pwd,unm,gen,ln;

try
{
DB.open();
LoggedUserList.create();
22

ss=new ServerSocket(2500);
while(true)
{
sk=ss.accept();
is=sk.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
os=sk.getOutputStream();
ps=new PrintStream(os);
rt=br.readLine();
if(rt.equals("Log In"))
{
uid=br.readLine();
pwd=br.readLine();
ln=br.readLine();
if(DB.checkuser(uid,pwd,ln)==1)
{
ps.println("valid");
ps.println(pno);
LoggedUserList.sendMessage("Login",uid);
LoggedUser lu=new LoggedUser(uid,sk,pno);
LoggedUserList.add(lu);
pno++;
}
else
23

ps.println("Invalid");

}else
if(rt.equals("User List"))
{
ResultSet rs=DB.getUserList();
while(rs.next())
{
ps.println(rs.getString(1));
ps.println(rs.getString(2));
ps.println(rs.getString(3));
ps.println(rs.getString(4));
}
ps.println("End");
rs.close();
}
else
if(rt.equals("saveUser"))
{
uid=br.readLine();
pwd=br.readLine();
unm=br.readLine();
gen=br.readLine();
int r=DB.saveUser(uid,pwd,unm,gen);
if(r==1)
24

ps.println("Record Saved");
else
ps.println("Record not saved");
}
else
if(rt.equals("LoggedUserList"))
{
ArrayList al=LoggedUserList.al;
int i;
for(i=0;i<al.size();i++)
{
LoggedUser lu=LoggedUserList.getLoggedUser(i);
ps.println(lu.uid);
}
ps.println("End");
}
else
if(rt.equals("Logout"))
{
uid=br.readLine();
LoggedUserList.sendMessage("Logout",uid);
LoggedUserList.remove(uid);
}
else
if(rt.equals("Send Message"))
25

{
String fuid=br.readLine();
String tuid=br.readLine();
String msg=br.readLine();
LoggedUserList.sendMessage(fuid,tuid,msg);
}
else
if(rt.equals("DeleteUser"))
{
uid=br.readLine();
if(DB.checkuser(uid)==0)
ps.println("Invalid UserId");
else
{
DB.delUser(uid);
ps.println("User Deleted");
}
}
}
}catch(Exception e)
{
System.out.println(e);
}
}

26

DB.java
import java.util.*;
import java.sql.*;
class DB
{
static Connection cn;
static void open() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("Jdbc:Odbc:ChatDSN");
}
static int checkuser(String uid,String pwd,String ln) throws Exception
{
PreparedStatement pstm;
ResultSet rs;
if(ln.equals("Admin"))
pstm=cn.prepareStatement("Select * from AdminUsers Where UserId=?
and Password=?");
else
pstm=cn.prepareStatement("Select

from

User

Where

UserId=?

and

Password=?");
pstm.setString(1,uid);
pstm.setString(2,pwd);
rs=pstm.executeQuery();
if(rs.next())
return(1);
27

else
return(0);
}
static int saveUser(String uid,String pwd,String unm,String gen)
{
try
{
PreparedStatement pstm;
pstm=cn.prepareStatement("Insert into User values(?,?,?,?)");
pstm.setString(1,uid);
pstm.setString(2,pwd);
pstm.setString(3,unm);
pstm.setString(4,gen);
pstm.executeUpdate();
return(1);
}catch(Exception e)
{
return(0);
}
}
static ResultSet getUserList() throws Exception
{
PreparedStatement pstm;
ResultSet rs;
pstm=cn.prepareStatement("Select * from user");
28

rs=pstm.executeQuery();
return(rs);
}
static void delUser(String uid)throws Exception
{
PreparedStatement pstm;
pstm=cn.prepareStatement("Delete from User where UserId=?");
pstm.setString(1,uid);
pstm.executeUpdate();
}
static int checkuser(String uid) throws Exception
{
PreparedStatement pstm;
ResultSet rs;
pstm=cn.prepareStatement("Select * from User Where UserId=?");
pstm.setString(1,uid);
rs=pstm.executeQuery();
if(rs.next())
return(1);
else
return(0);
}
}

29

LoggedUser.java
import java.net.*;
class LoggedUser
{
String uid;
Socket sk;
int pno;
LoggedUser(String u,Socket s,int pn)
{
uid=u;
sk=s;
pno=pn;
}
}

30

LoggedUserList.java
import java.net.*;
import java.util.*;
import java.io.*;
class LoggedUserList
{
static ArrayList al;
static void create()
{
al=new ArrayList();
}
static void add(LoggedUser lu)
{
al.add(lu);
}
static int size()
{
return(al.size());
}
static LoggedUser getLoggedUser(int i)
{
return((LoggedUser)al.get(i));
}
static void sendMessage(String msg,String uid) throws Exception

31

{
LoggedUser lu;
int l,i;
l=al.size();
for(i=0;i<l;i++)
{
lu=(LoggedUser)al.get(i);
String ip=lu.sk.getInetAddress().getHostAddress();
Socket sk=new Socket(ip,lu.pno);
OutputStream os=sk.getOutputStream();
PrintStream ps=new PrintStream(os);
ps.println(msg);
ps.println(uid);
}

}
static void sendMessage(String fuid,String tuid,String msg) throws Exception
{
LoggedUser lu=null;
int l,i;
l=al.size();
String ip="";
for(i=0;i<l;i++)
{
lu=(LoggedUser)al.get(i);
32

if(lu.uid.equals(tuid))
break;
}
ip=lu.sk.getInetAddress().getHostAddress();
Socket sk=new Socket(ip,lu.pno);
OutputStream os=sk.getOutputStream();
PrintStream ps=new PrintStream(os);
ps.println("Chat Message");
ps.println(fuid);
ps.println(msg);
}
static void remove(String uid)
{
int i,l;
LoggedUser lu;
l=al.size();
for(i=0;i<l;i++)
{
lu=((LoggedUser)al.get(i));
if(lu.uid.equals(uid))
al.remove(i);
}
}
}

33

LoginFrame.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class LoginFrame extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4,pic;
JTextField tf;
JPasswordField pw;
JButton b1,b2,b3;
JPanel p,p1,p2,p3;
JComboBox cb;
LoginFrame()
{
ImageIcon ic=new ImageIcon("a3.jpg");
pic=new JLabel(ic);
setTitle("LogIN Frame");
l1=new JLabel("Log In To Chat Server",SwingConstants.CENTER);
p3=new JPanel();
p3.setLayout(new GridLayout(1,2,20,20));
p3.add(l1);p3.add(pic);
Font f=new Font("Arial",Font.BOLD,34);
l1.setFont(f);

34

l2=new JLabel("User Id");


l3=new JLabel("Password");
l4=new JLabel("LogIn Mode");
cb=new JComboBox();
cb.addItem("User");
cb.addItem("Admin");
tf=new JTextField(20);
pw=new JPasswordField();
b1=new JButton("Log In");
b2=new JButton("Exit");
b3=new JButton("Register");
p=new JPanel();
p.setLayout(new GridLayout(3,2,20,20));
p.add(l4);p.add(cb);
p.add(l2);p.add(tf);
p.add(l3);p.add(pw);
Container c;
c=getContentPane();
c.setBackground(Color.pink);
p2=new JPanel();
p2.setLayout(new GridLayout(1,3,20,20));
p2.add(b1);p2.add(b3);p2.add(b2);
p1=new JPanel();
p1.setLayout(new BorderLayout());
p1.add(p,"Center");
35

p1.add(p2,"South");
p1.add(p3,"North");

setLayout(new FlowLayout());
add(p1);
setSize(600,400);
setVisible(true);
p.setBackground(Color.pink);
p3.setBackground(Color.pink);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.equals("Exit"))
System.exit(0);
else
if(s.equals("Register"))
{
RegisterFrame rf=new RegisterFrame();
rf.setSize(600,400);
rf.setVisible(true);
}
36

else
{
Socket sk;
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
PrintStream ps;
String uid,pwd,res,ln;
try
{
uid=tf.getText();
pwd=pw.getText();
ln=cb.getSelectedItem()+"";
sk=new Socket(IP.getIP(),2500);
is=sk.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
os=sk.getOutputStream();
ps=new PrintStream(os);
ps.println("Log In");
ps.println(uid);
ps.println(pwd);
ps.println(ln);
res=br.readLine();
37

if(res.equals("valid"))
{
String pno=br.readLine();
if(ln.equals("Admin"))
{
AdminMainFrame amf=new AdminMainFrame();
amf.setVisible(true);
amf.setSize(600,400);
}
else
{
UserMainFrame

mf=new

UserMainFrame(uid,pno);
mf.setSize(600,400);
mf.setVisible(true);
}
}
else
JOptionPane.showMessageDialog(this,"invalid

uid or

pwd");
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error"+e);
}
}
}
38

public static void main(String arg[])


{
LoginFrame lf=new LoginFrame();
}
}

RegisterFrame.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.sql.*;
class RegisterFrame extends JFrame implements ActionListener
{

JLabel ttitle,l1,l2,l3,l4,l5,pic;
JTextField tf1,tf2;
JPasswordField pw1,pw2;
JComboBox cb;
JButton b1,b2;
JPanel p1,p2;
RegisterFrame()
{
ImageIcon ic =new ImageIcon("a2.jpg");

39

pic=new JLabel(ic);
setTitle("Register Frame");
ttitle=new JLabel("Register New User");
Font f=new Font("Arial",Font.BOLD,34);
ttitle.setFont(f);
l1=new JLabel("User Id");
l2=new JLabel("Password");
l3=new JLabel("Confirm Password");
l4=new JLabel("User Name");
l5=new JLabel("Gender");
tf1=new JTextField();
tf2=new JTextField();
pw1=new JPasswordField();
pw2=new JPasswordField();
cb=new JComboBox();
cb.addItem("Male");
cb.addItem("Female");
b1=new JButton("Register");
b2=new JButton("Close");
p1=new JPanel();
p1.setLayout(new GridLayout(6,2,20,20));
p1.add(l1);p1.add(tf1);
p1.add(l2);p1.add(pw1);
p1.add(l3);p1.add(pw2);
p1.add(l4);p1.add(tf2);
40

p1.add(l5);p1.add(cb);
p1.add(b1);p1.add(b2);
p2=new JPanel();
p2.setLayout(new BorderLayout());
p2.add(ttitle,"North");
p2.add(p1,"Center");
p2.add(pic,"West");
setLayout(new FlowLayout());
add(p2);
b1.addActionListener(this);
b2.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
PrintStream ps;
String s=ae.getActionCommand();
if(s.equals("Close"))
{
dispose();
return;
41

}
String uid,pwd,unm,cpwd,gen;
uid=tf1.getText();
pwd=pw1.getText();
cpwd=pw2.getText();
if(!pwd.equals(cpwd))
{
JOptionPane.showMessageDialog(this,"Password and conformpassword
not match");
return;
}
unm=tf2.getText();
gen=cb.getSelectedItem()+"";
try
{
Socket sk=new Socket(IP.getIP(),2500);
is=sk.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
os=sk.getOutputStream();
ps=new PrintStream(os);
ps.println("saveUser");
ps.println(uid);
ps.println(pwd);
ps.println(unm);

42

ps.println(gen);
String res=br.readLine();
JOptionPane.showMessageDialog(this,res);
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error");
}

}
}

AdminMainFrame.java
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
class AdminMainFrame extends JFrame implements ActionListener
{
JMenuBar mb;
JMenu op,ex;
JMenuItem ul,du,exi;
JTextArea ta;
JDesktopPane jdp;
JLabel pic;

43

AdminMainFrame()
{
setTitle("ChatServer:Admin Section");
jdp=new JDesktopPane();
ImageIcon ic=new ImageIcon("2.jpg");
pic=new JLabel(ic);
jdp.setLayout(new BorderLayout());
jdp.add(pic,"Center");
setLayout(new BorderLayout());
add(jdp,"Center");
mb=new JMenuBar();
op=new JMenu("Options");
ex=new JMenu("Exit");
ul=new JMenuItem("Users List");
du=new JMenuItem("Delete User");
exi=new JMenuItem("Exit");
mb.add(op);mb.add(ex);
op.add(ul);op.add(du);
ex.add(exi);
setJMenuBar(mb);
ul.addActionListener(this);
du.addActionListener(this);
exi.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
44

{
String s=ae.getActionCommand();
if(s.equals("Exit"))
System.exit(0);
else
if(s.equals("Users List"))
{
UserListFrame ulf=new UserListFrame();
ulf.setSize(600,400);
ulf.setClosable(true);
ulf.setMaximizable(true);
jdp.add(ulf);
ulf.setVisible(true);
}
else
if(s.equals("Delete User"))
{
DeleteUserFrame duf= new DeleteUserFrame();
duf.setSize(600,400);
duf.setClosable(true);
duf.setMaximizable(true);
jdp.add(duf);
duf.setVisible(true);
}
}
45

public static void main(String arg[])


{
AdminMainFrame amf=new AdminMainFrame();
amf.setVisible(true);
amf.setSize(600,400);
}
}

DeleteUserFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
class DeleteUserFrame extends JInternalFrame implements ActionListener
{
JLabel l1,l2;
JTextField tf;
JButton b1,b2;
JDesktopPane dsp;
ImageIcon ic;
JLabel pic;
JPanel p;
DeleteUserFrame()
{
l1=new JLabel("Enter UserId");

46

tf=new JTextField();
b1=new JButton("Delete");
b2=new JButton("Close");
p=new JPanel();
p.setLayout(new GridLayout(1,1,10,10));
p.add(l1);
p.add(tf);
p.add(b1);
p.add(b2);
setLayout(new FlowLayout());
add(p,"Center");
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.equals("Close"))
dispose();
else
{
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
47

PrintStream ps;
try
{
Socket sk=new Socket(IP.getIP(),2500);
os=sk.getOutputStream();
is=sk.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
ps=new PrintStream(os);
ps.println("DeleteUser");
String uid=tf.getText();
ps.println(uid);
String msg1=br.readLine();
System.out.println(msg1+uid);
//JOptionPane.showMessageDialog(this,msg1,""+uid);
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error"+e);
}
}
}

48

IP.java
class IP
{
static String IP;
static String getIP()
{
return(IP);
}
static void setIP(String i)
{
IP=i;
}
static
{
IP="127.0.0.1";
}
}

ChatWindow.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class ChatWindow extends JInternalFrame implements ActionListener
{
JTextField tf;
49

JButton b;
JTextArea ta;
JPanel p;
String uid1,uid2;
ChatWindow(String u1,String u2)
{
setSize(300,200);
uid1=u1;
uid2=u2;
setTitle("Chat Window for: "+uid2);
ta=new JTextArea();
setLayout(new BorderLayout());
add(ta,"Center");
b=new JButton("Send");
tf=new JTextField(15);
p=new JPanel();
p.setLayout(new GridLayout(1,2,5,5));
p.add(tf);p.add(b);
add(p,"South");
b.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
50

if(s.equals("Send"))
{
try
{
Socket sk;
OutputStream os;
PrintStream ps;
sk=new Socket(IP.getIP(),2500);
os=sk.getOutputStream();
ps=new PrintStream(os);
String msg=tf.getText();
ps.println("Send Message");
ps.println(uid1);
ps.println(uid2);
ps.println(msg);
ta.setText(ta.getText()+"\nSent:"+msg);
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error"+e);
}

}
}
}

51

TitleLabel.java
import java.awt.*;
import javax.swing.*;
class TitleLabel extends JLabel
{
TitleLabel(String t)
{
super(t,SwingConstants.CENTER);
setFont(new Font("Arial",Font.BOLD,36));
setBackground(Color.red);
setOpaque(true);
setForeground(Color.yellow);
}
}

UserListFrame.java
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import javax.swing.JTable.*;
import java.awt.event.*;
class UserListFrame extends JInternalFrame implements ActionListener
{
JTable t;
JLabel pic;
52

UserListFrame()
{
TitleLabel ttitle;
JButton b;
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
PrintStream ps;
Socket s;
JPanel p;
ArrayList al;
try
{
setTitle("List Of Users");
ImageIcon ic=new ImageIcon("2.jpg");
pic=new JLabel(ic);
ttitle=new TitleLabel("List of Users");
b=new JButton("Exit");
s=new Socket("127.0.0.1",2500);
is=s.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
os=s.getOutputStream();
ps=new PrintStream(os);
53

al=new ArrayList();
p=new JPanel();
String rec[],item;
while(true)
{

ps.println("User List");
item=br.readLine();
if(item.equals("End"))
break;
rec=new String[4];
rec[0]=item;
rec[1]=br.readLine();
rec[2]=br.readLine();
rec[3]=br.readLine();
al.add(rec);

}
int l=al.size();
String data[][]=new String[l][4];
for(int i=0;i<l;i++)
{
rec=(String[])al.get(i);
data[i][0]=rec[0];
data[i][1]=rec[1];
data[i][2]=rec[2];
data[i][3]=rec[3];
}
54

String head[]={"Userid","Password","Gender","User Name"};


t=new JTable(data,head);
JScrollPane jsp=new JScrollPane(t);
add(jsp);
p.setLayout(new BorderLayout());
p.add(ttitle,"North");
p.add(jsp,"Center");
p.add(b,"South");
p.add(pic,"East");
setLayout(new FlowLayout());
add(p);
b.addActionListener(this);
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error"+e);
}

}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.equals("Exit"))
{
dispose();
}
55

}
}

UserMainFrame.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
class UserThread extends Thread
{
UserMainFrame umf;
String uid;
int pno;
ServerSocket ss;
UserThread(UserMainFrame um,String u,int pn)
{
umf=um;
uid=u;
pno=pn;
}
public void run()
{
Socket sk;
InputStream is;

56

InputStreamReader isr;
BufferedReader br;
try
{
ss=new ServerSocket(pno);
while(true)
{
sk=ss.accept();
is=sk.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
String rt=br.readLine();
if(rt.equals("Login"))
{
String u=br.readLine();
umf.l.addItem(u);
ChatWindow cw=new ChatWindow(uid,u);
umf.cwList.add(cw);
umf.dsp.add(cw);
}
else
if(rt.equals("Logout"))
{
String u=br.readLine();
umf.l.remove(u);
57

}
else
if(rt.equals("Chat Message"))
{
String fuid,msg;
fuid=br.readLine();
msg=br.readLine();
//JOptionPane.showMessageDialog(umf,fuid+" "+msg);
ChatWindow cw=null;
int l,i;
l=umf.cwList.size();
for(i=0;i<l;i++)
{
cw=(ChatWindow)umf.cwList.get(i);
if(cw.uid2.equals(fuid))
break;
}
cw.setVisible(true);
cw.ta.setText(cw.ta.getText()+"\nRcvd:"+msg);
}
}

}catch(Exception e)
{
System.out.println(e);
58

}
}
}

class UserMainFrame extends JFrame implements ActionListener


{
JLabel lb;
JButton b;
JPanel p;
java.awt.List l;
String uid;
Socket sk;
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
PrintStream ps;
int pno;
UserThread ut;
JDesktopPane dsp;
ArrayList cwList;
UserMainFrame(String u,String pn)
{

59

uid=u;
pno=Integer.parseInt(pn);
ut=new UserThread(this,uid,pno);
ut.start();
cwList=new ArrayList();
setTitle("Chat Application Logged By:"+uid);
b=new JButton("Logout");
lb=new JLabel("Welcome:"+uid);
p=new JPanel();
p.setLayout(new GridLayout(1,2,10,10));
p.add(lb);p.add(b);
b.addActionListener(this);
l=new java.awt.List();
setLayout(new BorderLayout());
add(p,"South");
add(l,"East");
dsp=new JDesktopPane();
add(dsp,"Center");
try
{
sk=new Socket(IP.getIP(),2500);
is=sk.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
os=sk.getOutputStream();
60

ps=new PrintStream(os);
ps.println("LoggedUserList");
while(true)
{
u=br.readLine();
if(u.equals("End"))
break;
else
if(u.equals(uid))
continue;
ChatWindow cw=new ChatWindow(uid,u);
cwList.add(cw);
dsp.add(cw);
l.addItem(u);
}
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error"+e);
}
l.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.equals("Logout"))
61

{
try
{
sk=new Socket(IP.getIP(),2500);
os=sk.getOutputStream();
ps=new PrintStream(os);
ps.println("Logout");
ps.println(uid);
dispose();
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error "+e);
}
}
else
{
ChatWindow cw=null;
int l=cwList.size();
for(int i=0;i<l;i++)
{
cw=(ChatWindow)cwList.get(i);
if(cw.uid2.equals(s))
break;
}
cw.setVisible(true);
62

cw.setClosable(true);
}

}
}

63

Chapter-8

Snapshot.docx

64

Snapshot.docx

65

Running Server Program:

66

67

Login

Frame:

68

Register Frame:

69

User Main Frame:

70

Chat Window:

71

Admin Section:

72

Users List

73

Delete User Frame:

74

75

Database docx.
Column Name
UserId
Password

Type
Text
Text

Table: User
UserId
A
B
123
S
Q
W

Password
A
B
123456
S
Q
W

Table: Admin Users


UserId
A
B
Aa
123
34
14

Password
1
2
1234
123456
67
47

76

CONCLUSION

Java is a very easy to learn and powerful language. Many difficult projects can be made easily due to its
property of modularity. Various packages are pre-defined in it, which makes it a very user-friendly tool to
develop powerful software. It is very easy to upgrade any part of the software. The whole softwares
source code needs not to be changed but only that part of module.

Java offers the real possibility that most programs can be written in a type-safe language.
However, for Java to be broadly useful, it needs to have more expressive power than it does at
present.
This paper addresses one of the areas where more power is needed. It extends Java with a
mechanism for parametric polymorphism, which allows the definition and implementation of
generic abstractions. The paper gives a complete design for the extended language. The proposed
extension is small and conservative and the paper discusses the rationale for many of our
decisions. The extension does have some impact on other parts of Java, especially Java arrays,
and the Java class library.

77

REFERENCES

Following books are used as reference:


1)Java The Complete Reference, 8th Edition.
2)Programming with Java By E.Balagurusamy
Following websites are also used as reference:
1)www.javatpoint.com
2)www.javaworld.com
3)www.tutorialspoint.com
4)www.oracle.com/java/

78

79

You might also like