static Socket socket=null; //用来存储一个套接字连接
//chatServer main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
chatServer chatServer1=new chatServer(); //实例化一个chatServer类
chatServer1.show();
System.out.println("Server starting ...");
try
{
server=new ServerSocket(2525); //使用端口2525初始化服务器套接字
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
while(true)
{
if(clients.size()<10) //当客户数小于10个时开始连接
{
try
{
socket=server.accept(); //用来存储连接上的客户socket
if(socket!=null)
{
System.out.println(socket+"连接"); //在控制台打印客户连接信息
}
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
int i=0;
do{
Client c=new Client(socket); //定义并实例化一个Client线程类,一个就对应一个客户连接
clients.addElement(c); //加入clients数组中
if(checkName(c)) //调用checkName方法验证c的合法性
{
int connum=++chatServer1.active_connects; //定义connum来存储活动连接数
String constr="目前有"+connum+"客户相连"; //在状态栏里显示连接数
chatServer1.statusBar.setText(constr);
Client listdata=(Client)clients.elementAt(i); //将连接客户的socket信息存储进listdata数组
chatServer1.jList1.addItem(listdata.ip+"连接",i); //将客户socket信息写入list框
c.start(); //启动线程
notifyRoom(); //用notifyRoom方法来监视聊天室连接变化
//不断改变clients数组并刷新客户端信息
}
else
{
//如果名字不合法
c.ps.println("TAKEN");
disconnect(c);
}
i++;
break;
}
while(i
}
else //如果clients数组超过了10个
{
try{Thread.sleep(200);}
catch(InterruptedException e)
{
}
}
}//end of while
}// end of main method
/**Construct the frame*/
public chatServer() //chatServer类的构造器用来初始化一些UI信息
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("简易聊天服务器端");
statusBar.setText("目前的连接数为:");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuFileExit_actionPerformed(e);
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuHelpAbout_actionPerformed(e);
}
});
jPanel1.setLayout(borderLayout2);
jLabel1.setText("服务器端连接客户");
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, BorderLayout.NORTH);
jPanel1.add(scrollpane, BorderLayout.CENTER);
}//end of jbinit
/**File | Exit action performed*/
public void jMenuFileExit_actionPerformed(ActionEvent e) //实现退出菜单方法
{
sendClients(new StringBuffer("QUIT")); //向客户端发送断开连接信息
closeAll(); //调用closeAll方法关闭所有连接
System.exit(0);
}
/**Help | About action performed*/
public void jMenuHelpAbout_actionPerformed(ActionEvent e) //实现about对话框,意义不大,可以去掉
{
chatServer_AboutBox dlg = new chatServer_AboutBox(this
上一篇:电话号码查询系统设计(数据库管理)毕业论文——基于JAVA(word文档)
下一篇:缅怀先烈 铭记历史