专业版尚未开始(04-12-17)
三峡大学内的同学可以到http://ftp.ctgu.edu.cn/soft/down.php?act=up&id=2800下载
/*
* date: 2004-11-29 author:zhangyu6050;
* email: dazuibazhangyu@eyou.com
* ctgu (china three groges university)
* todo: 服务器主程序
* bugs:
*/
import util.DateUtil;
import java.net.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
import java.applet.*;
import java.util.*;
public class ChatServer {
private int port = 1775; //端口号
private ServerSocket serverS; //服务器套节字
public static Broadcast broad;//通讯专用类,单态模式设计
private JTextArea display;
/**
*初始化
*/
public ChatServer() {
setupGui();
runServer();
}
public void runServer(){
try {
serverS=new ServerSocket(port);
} catch (IOException e2) {
showMSg("在指定端口无法监听"+e2.getMessage());
}
try {
broad= Broadcast.getInstence();//做好准备
while(true)
{
showMSg("等待接入......");
Socket socket = serverS.accept();
//记录登录时间
String s = DateUtil.format(Calendar.getInstance(),DateUtil.ymdhms)+"/r";
showMSg("接入时间:"+s);
//实例化一个代理
Angent angent = new Angent(socket);
//实例化一个服务线程
ServerThread serverthread = new ServerThread(angent);
serverthread.start();
serverS.close();
}
}
catch(IOException e)
{
showMSg(e.getMessage()+"服务器异常在要关闭");
}
}
public void closeServer(){
System.out.print("服务器正在关闭!");
try {
serverS.close();
} catch (IOException e) {
System.err.println(e.getMessage()+"/n服务器无法关闭");
e.printStackTrace();
}
}
//set up a Frame and return it
private JFrame setupGui(){
//setup a Frame and set title
JFrame jf=new JFrame();
port=Integer.parseInt(JOptionPane.showInputDialog("input port please:"));
jf.setTitle("server");
//get the pane of the Frame
Container pane=jf.getContentPane();
//set up modules
display=new JTextArea();//显示区
JLabel sendLable=new JLabel("enter msg");
JTextField sysBroadcast=new JTextField(20);//系统广播发送
JPanel sendPane=new JPanel();
sendPane.add(sendLable);sendPane.add(sysBroadcast);
pane.add(sendPane, BorderLayout.NORTH);
pane.add("Center",new JScrollPane(display));
jf.setSize(400,300);
jf.setVisible(true);
jf.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
return jf;
}
void showMSg(String s){
display.append(s+"/n");
}
public static void main(String args[]){
new ChatServer();
}
}