SERVER
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server extends JFrame
{
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("File");
JMenuItem mi1 = new JMenuItem("Connect");
JMenuItem mi2 = new JMenuItem("Exit");
JMenu m2 = new JMenu("Help");
JOptionPane mi3 = new JOptionPane("If you want to use this server chatroom please click at 'File' and choose Connect first for client connect. If you want to stop server chatroom please shoose 'Exit.'");
JLabel lab1 = new JLabel("Online User");
JLabel lab2 = new JLabel("Message:");
JTextArea ta = new JTextArea(9,30);
java.awt.List list = new java.awt.List(9);
JTextField tf = new JTextField(22);
JButton b = new JButton("Sent");
JButton e = new JButton("Emoticon");
JScrollPane sp = new JScrollPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
ServerSocket serverSocket;
Socket socket;
ArrayList array = new ArrayList();
int connect;
public Server()
{
super("Server Chatroom");
setJMenuBar(mb);
mb.add(m1);
mb.add(m2);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m2.add(mi3);
sp.getViewport().add(ta);
p1.setLayout(new BorderLayout());
p3.add(lab1);
p1.add(p3,BorderLayout.NORTH);
p1.add(list,BorderLayout.SOUTH);
p2.add(e);
p2.add(lab2);
p2.add(tf);
p2.add(b);
getContentPane().add(sp,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
setSize(500,430);
setVisible(true);
setResizable(false);
mi1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(serverSocket == null)
{
try
{
serverSocket = new ServerSocket(7000);
ta.append("System imformation:"+" Server Chatroom"
+" Now Starting...... ");
}
catch(IOException ex)
{
ta.append("System imformation:"+" Server Chatroom"
+" Starting fail! ");
}
}
else
{
ta.append("System imformation:"+" Server Chatroom"
+" Starting copleted...... ");
}
Thread t = new Watch();
t.start();
}
});
mi2.addActionListener(new ServerListener());
b.addActionListener(new ServerListener());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeAll();
System.exit(0);
}
});
}
class Watch extends Thread
{
public void run()
{
while(true)
{
if(array.size() <= 10)
{
try
{
socket = serverSocket.accept();
}
catch(IOException ex)
{
ta.append("System imformation: "+ex+" ");
}
Client c = new Client(socket);
array.add(c);
c.start();
}
else
{
ta.append("System imformation:"+" Server Chatroom"
+"
}
}
}
}
class Client extends Thread
{
String name;
String ip;
String ipAddress;
Socket socket;
BufferedReader br;
PrintStream ps;
public Client(Socket s)
{
try
{
socket = s;
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
}
catch (IOException ex)
{
ta.append("System imformation: "+ex+" ");
}
}
public void run()
{
String info;
try
{
while(true)
{
info = br.readLine();
if(info == null)
break;
StringTokenizer st = new StringTokenizer(info, ":");
String keyword = st.nextToken();
if (keyword.equals("CONNECT"))
{
name = st.nextToken();
ip = st.nextToken();
ipAddress = ip.substring(ip.indexOf("/")+1);
ta.append("System imformation: " + name + "/"
+ ipAddress +" Connected ");
list.add(name + " " + ip
+" User login completed");
notifyChatRoom();
}
else if (keyword.equals("MSG"))
{
String broadcast = st.nextToken();
StringBuffer message = new StringBuffer("MSG:" +broadcast);
message.append(st.nextToken(""));
if (broadcast.equals("BROAD"))
{
sendToClients(message);
}
else
{
sendToClient(message, broadcast);
}
}
else if (keyword.equals("QUIT"))
{
disConnect(this);
list.removeAll();
for (int i = 0; i < array.size(); i++)
{
Client c = (Client) array.get(i);
list.add(c.name + " " + c.ip
+" User login completed", i);
}
notifyChatRoom();
break;
}
}
}
catch (IOException ex)
{
ta.append("System imformation: "+ex+" ");
disConnect(this);
notifyChatRoom();
}
}
public void send(StringBuffer msg)
{
ps.println(msg);
ps.flush();
}
}
public synchronized void sendToClients(StringBuffer msg)
{
for(int i=0;i<array.size();i++)
{
Client c = (Client)array.get(i);
c.send(msg);
}
}
public synchronized void sendToClient(StringBuffer msg,String s)
{
for(int i=0;i<array.size();i++)
{
Client c = (Client)array.get(i);
if(c.name.equals(s))
{
c.send(msg);
break;
}
}
}
public void notifyChatRoom()
{
StringBuffer sb = new StringBuffer("PEOPLE");
for(int i=0;i<array.size();i++)
{
Client c =(Client)array.get(i);
sb.append(":"+c.name);
}
sendToClients(sb);
}
public synchronized void disConnect(Client c)
{
try
{
ta.append("System imformation: " + c.name+ "/"
+ c.ipAddress +" User was left ");
c.send(new StringBuffer("QUIT:"+c.name));
c.socket.close();
array.remove(c);
}
catch(IOException ex)
{
ta.append("System imformation: "+ex+" ");
}
}
public void closeAll()
{
sendToClients(new StringBuffer("QUIT:"+"Server"));
while(array.size()>0)
{
try
{
for(int i=0;i<array.size();i++)
{
Client c = (Client)array.get(i);
c.socket.close();
array.remove(c);
}
}
catch(IOException ex)
{
ta.append("System imformation: "+ex+" ");
}
}
}
class ServerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Sent"))
{
StringBuffer msg = new StringBuffer("MSG:"+"BROAD:");
msg.append("Server:"+tf.getText());
sendToClients(msg);
tf.setText("");
}
else if(e.getActionCommand().equals("Exit"))
{
closeAll();
System.exit(0);
}
}
}
public static void main(String[] args)
{
Server app = new Server();
}
}