📄 server.java
字号:
import java.io.*;
import java.net.*;
import java.util.Vector;
public class Server extends Thread {
private ServerSocket serverSocket;
private Administration admin;
private static Vector userOnline = new Vector(1, 1);
private static Vector v = new Vector(1, 1);
//创建服务器 启动服务监听30002端口
public Server() {
admin = new Administration();
try {
serverSocket = new ServerSocket(30002);
InetAddress address = InetAddress.getLocalHost();
admin.txtServerName.setText(address.getHostName());
admin.txtIP.setText(address.getHostAddress());
admin.txtPort.setText("30002");
} catch (IOException e) {
fail(e, "不能启动服务!");
}
admin.txtStatus.setText("已启动。。。");
this.start(); // 启动线程
}
// 退出服务器
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_net(admin, client ,userOnline,v); // 支持多线程
}
} catch (IOException e) {
fail(e, "不能监听!");
} catch (NullPointerException e) {
System.out.println(e);
}
}
// 启动服务器
public static void main(String args[]) {
new Server();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -