import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class ChatServer extends JFrame
{
ServerSocket sSocket;
JPanel panel1, pnlInfo, pnlLeft;
JLabel lblSts, lblUserOnLine, lblPort, lblLog, lblUser;
JTextField tfSts, tfUserOnLine, tfPort;
JButton btnStart, btnStop;
TextArea taLog;
JList listUser;
JScrollPane spUser;
boolean bStart = false;
Thread sThread;
static Vector u = new Vector(1, 1);
static Vector v = new Vector(1, 1);
static Vector vList = new Vector();
final static int MAX_CLIENT = 3;
public ChatServer()
{
super("Server");
setSize(550, 490);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
panel1 = new JPanel();
panel1.setLayout(null);
pnlInfo = new JPanel(new GridLayout(7, 1));
pnlInfo.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder(""), BorderFactory.createEmptyBorder(1, 1,
1, 1)));
lblSts = new JLabel("Status:");
tfSts = new JTextField(10);
tfSts.setEnabled(false);
lblUserOnLine = new JLabel("Online users:");
tfUserOnLine = new JTextField("0人", 10);
tfUserOnLine.setEditable(false);
lblPort = new JLabel("Server port:");
tfPort = new JTextField("8000", 10);
lblUser = new JLabel("Online lists:");
listUser = new JList();
listUser.setVisibleRowCount(17);
spUser = new JScrollPane();
int i = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
spUser.setVerticalScrollBarPolicy(i);
spUser.getViewport().setView(listUser);
btnStart = new JButton("START:");
btnStop = new JButton("STOP:");
btnStop.setEnabled(false);
lblLog = new JLabel("Log");
lblLog.setForeground(Color.PINK);
taLog = new TextArea(30, 30);
taLog.setEditable(false);
pnlInfo.add(lblSts);
pnlInfo.add(tfSts);
pnlInfo.add(lblUserOnLine);
pnlInfo.add(tfUserOnLine);
pnlInfo.add(lblPort);
pnlInfo.add(tfPort);
pnlInfo.add(lblUser);
pnlLeft = new JPanel(new GridLayout(2, 1));
pnlLeft.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder(""), BorderFactory.createEmptyBorder(1, 1,
1, 1)));
pnlLeft.add(pnlInfo);
pnlLeft.add(spUser);
pnlLeft.setBounds(5, 5, 130, 400);
lblLog.setBounds(140, 5, 100, 30);
taLog.setBounds(140, 35, 400, 370);
btnStart.setBounds(150, 420, 90, 25);
btnStop.setBounds(270, 420, 90, 25);
panel1.add(pnlLeft);
panel1.add(lblLog);
panel1.add(taLog);
panel1.add(btnStart);
panel1.add(btnStop);
this.getContentPane().add(panel1);
setVisible(true);
BtnAction act = new BtnAction();
btnStart.addActionListener(act);
btnStop.addActionListener(act);
}
private class BtnAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
if (source.equals(btnStart))
{
try
{
int port = Integer.valueOf(tfPort.getText());
if (port <= 1024)
{
fail("请使用大于1024的端口号");
return;
}
sSocket = new ServerSocket(port);
btnStop.setEnabled(true);
btnStart.setEnabled(false);
bStart = true;
tfPort.setEditable(false);
sThread = new mThread();
sThread.start();
tfSts.setText("服务已启动");
taLog.append("服务启动时间:" + (new Date() + "\n"));
}
catch (IOException e)
{
fail("不能启动服务,可能端口被占用!");
}
catch (NumberFormatException e)
{
fail("输入的端口号不是整数");
}
catch (Exception e)
{
fail("创建线程失败");
}
}
if (source.equals(btnStop))
{
try
{
sSocket.close();
btnStop.setEnabled(false);
btnStart.setEnabled(true);
bStart = false;
tfPort.setEditable(true);
sThread.stop();
tfSts.setText("服务已停止");
taLog.append("服务停止时间:" + (new Date() + "\n"));
}
catch (IOException e)
{
fail("不能启动服务!");
}
catch (SecurityException e)
{
fail("终止服务失败!");
}
}
}
}
public static void fail(String str)
{
JOptionPane.showMessageDialog(null, str, "错误",
JOptionPane.ERROR_MESSAGE);
}
public static void main(String afgs[])
{
new ChatServer();
}
@SuppressWarnings("unused")
private class mThread extends Thread
{
public void run()
{
try
{
while (true)
{
if (bStart)
{
Socket client = sSocket.accept();
Connection con = new Connection(client, u, v);
}
else
Thread.sleep(100);
}
}
catch (IOException e)
{
fail("不能监听!");
}
catch (InterruptedException e)
{
}
}
}
class Connection extends Thread
{
protected Socket client;
Vector userOnline;
Vector userChat;
protected ObjectInputStream fromClient;
protected PrintStream toClient;
Object obj;
public Connection(Socket sock, Vector u, Vector c)
{
client = sock;//客户端的socket
userOnline = u; //在线用户列表
userChat = c;//聊天信息
try
{
fromClient = new ObjectInputStream(client.getInputStream());
toClient = new PrintStream(client.getOutputStream());
}
catch (IOException e)
{
try
{
client.close();
}
catch (IOException e1)
{
fail("不能建立流" + e1);
return;
}
}
this.start();
}
public void run()
{
try
{
obj = (Object) fromClient.readObject();
if (obj.getClass().getName().equals("User"))
{
if (userOnline.size() < MAX_CLIENT)
{
doLogin();
}
else
{
toClient.println("用户数达到上限,请稍候!");
}
}
else if (obj.getClass().getName().equals("NewUser"))
{
doRegiste();
}
else if (obj.getClass().getName().equals("Info"))
{
doInfo();
}
else if (obj.getClass().getName().equals("Chat"))
{
doChat();
}
else if (obj.getClass().getName().equals("ExitUser"))
{
doExit();
}
}
catch (IOException e)
{
fail(e.toString());
}
catch (ClassNotFoundException e1)
{
fail("读对象发生错误!" + e1);
}
finally
{
try
{
client.close();
}
catch (IOException e)
{
fail(e.toString());
}
}
}
public void doLogin()
{
try
{
User u1 = (User) obj;//创建对象,从文件中查找是否存在
FileInputStream fin = new FileInputStream("user.dat");
ObjectInputStream objInput1 = new ObjectInputStream(fin);
vList = (Vector) objInput1.readObject();
boolean bHave = false;//查找判断标志用户是否存在
for (int i = 0; i < vList.size(); i++)
{
NewUser reg = (NewUser) vList.elementAt(i);
if (reg.name.equals(u1.name))
{
bHave = true;
if (!reg.password.equals(u1.password))
{
toClient.println("用户密码错误");
break;
}
else//判断是否已经登录
{
boolean bHaveLogin = false;
for (int a = 0; a < userOnline.size(); a++)
{
if (u1.name.equals(userOnline.elementAt(a)))
{
bHaveLogin = true;
break;
}
}
if (!bHaveLogin)
{
userOnline.addElement(u1.name);
toClient.println("登录成功");
taLog.append("用户" + u1.name + "登录成功" + "登录时间:"
+ (new Date()).toLocaleString() + "\n");
listUser.setListData(userOnline);
tfUserOnLine.setText(userOnline.size() + "人");
break;
}
else
toClient.println("该用户已登录");
}
}
}
if (!bHave)
toClient.println("用户名不存在,请先注册");
fin.close();
objInput1.close();
fromClient.close();
}
catch (ClassNotFoundException e)
{
fail(e.toString());
}
catch (IOExcepti