登陆界面
注册界面
聊天室界面
3.2通信功能实现方法
列举部分实现通信功能的主要代码
AppServer.java文件
public class AppServer extends Thread {
private static Vector userOnline = new Vector(1, 1); private ServerFrame sFrame; private ServerSocket serverSocket;
private static Vector v = new Vector(1, 1); /**
* 创建服务器 启动服务监听1001端口 * */
public AppServer() { }
sFrame = new ServerFrame(); try {
serverSocket = new ServerSocket(1001); // 获取服务器的主机名和IP地址
InetAddress address = InetAddress.getLocalHost(); sFrame.txtServerName.setText(address.getHostName()); sFrame.txtIP.setText(address.getHostAddress()); sFrame.txtPort.setText(\);
} catch (IOException e) { }
sFrame.txtStatus.setText(\已启动...\); this.start(); // 启动线程
fail(e, \不能启动服务!\);
/**
* 退出服务器 *
* @param e * 异常 * @param str
* 退出信息 */
public static void fail(Exception e, String str) { }
System.out.println(str + \。\ + e);
/**
* 监听客户的请求,当有用户请求时创建 Connection线程 */
public void run() {
try {
while (true) {
// 监听并接受客户的请求
Socket client = serverSocket.accept();
new Connection(sFrame, client, userOnline, v); // 支持
多线程
}
// System.out.println(\
} catch (IOException e) {
System.out.println(\不能监听\);
}
}
}
/**
* 启动服务器 */
public static void main(String args[]) { }
new AppServer();
ServerFrame.java文件
public class ServerFrame extends JFrame implements ActionListener {
public JList list; /** * */
private static final long serialVersionUID =
-8936397327038098620L;
JLabel lblStatus, lblNumber, lblMax, lblServerName, lblProtocol, // 服务器信息面板
JPanel pnlServer, pnlServerInfo;
lblIP,