import java.net.*;
import java.awt.BorderLayout;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class ChatServer {
static int port=5652; //port
static Vector<Client> clients=new Vector<Client>(100); //store a connection
static ServerSocket server=null; //creat server socket
static Socket socket=null;
//frame
JFrame frame;
JPanel panel;
static JTextArea infoArea;
JLabel infoLabel;
/**the creation function*/
public ChatServer() {
//frame
frame = new JFrame("ChatServer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set exit when frame close
panel = new JPanel(new BorderLayout());
infoArea = new JTextArea(2,2);
infoLabel = new JLabel("Connect information:");
frame.add(panel);
panel.add(infoArea, BorderLayout.CENTER);
panel.add(infoLabel, BorderLayout.NORTH);
//set object
infoArea.setEditable(false);
frame.setSize(400,500);
frame.setVisible(true);
try {
infoArea.append("Server start...\n");
server=new ServerSocket(port); //first set
while(true){
socket=server.accept(); //wait for connect
infoArea.append(socket.getInetAddress()+" connected\n");
Client client=new Client(socket); //new a client
clients.addElement(client); //add to the thread
client.start(); //start the thread
notifyChatRoom(); //change the members connected
}
}
catch(Exception ex) {
ex.printStackTrace(); //output the error
}
}
/**change the member of the chatroom*/
public static void notifyChatRoom() {
StringBuffer newUser=new StringBuffer("newUser");
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
newUser.append(":"+c.name); //user name
}
sendClients(new ImageInfo(newUser)); //send message
}
/**send information to all the clients*/
public static void sendClients(ImageInfo o){
for(int i=0;i<clients.size();i++){
Client client=(Client)clients.elementAt(i); //get each connection
client.send(o); //send
}
}
/**close all connections*/
public void closeAll() {
while(clients.size()>0) { //overall
Client client=(Client)clients.firstElement(); //get a client
clients.removeElement(client); //remove
}
}
public static void disconnect(Client c){ //disconnect the client
try{
infoArea.append(c.ip+" disconnected\n");
c.socket=null; //close the port
}
catch(Exception ex){
ex.printStackTrace();
}
clients.removeElement(c); //remove the client
}
/**the thread to use*/
class Client extends Thread {
Socket socket;
String name;
String ip;
ObjectInputStream iss; //input
ObjectOutputStream ps; //output
ImageInfo temp;
public Client(Socket s){
socket=s;
try{
ps=new ObjectOutputStream(socket.getOutputStream());
iss = new ObjectInputStream(socket.getInputStream());
}catch(IOException ex){
ex.printStackTrace();
}
try{
temp = (ImageInfo)iss.readObject();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
if(temp.flag == 0)
{
String info=temp.chat; //read the information
StringTokenizer stinfo=new StringTokenizer(info,":"); //analysis the string
String head=stinfo.nextToken(); //get the keywords
if(head.equals("INFO"))
{
if(stinfo.hasMoreTokens())
name=stinfo.nextToken(); //get the user name
if(stinfo.hasMoreTokens())
ip=stinfo.nextToken(); //get the ip address
}
}
}
/**send the image to the client*/
public void send(ImageInfo o) {
try{
ps.writeObject(o); //output
ps.flush();
}catch(IOException ex){
ex.printStackTrace(); //output error
}
}
/**the run function of the thread*/
public void run(){
while(true){
try{
temp = (ImageInfo)iss.readObject();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
ChatServer.disconnect(this); //disconnect
ChatServer.notifyChatRoom(); //update the information of the chat room
return;
}
if(temp.flag == 0 && temp.chat.equals("QUIT"))
{
ChatServer.disconnect(this); //disconnect
ChatServer.notifyChatRoom(); //update
}else{
ChatServer.sendClients(temp);
}
}
}
}
public static void main(String args[])
{
//set the GUI same as my operating system
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&F: " + exc);
}
new ChatServer();
}
}