主进程函数
public static void main(String[] args) {
System.out.println("hello world");
JFrame a=new JFrame("");
ExecutorService executorService = Executors.newFixedThreadPool(5);
Runnable socketThread=new Runnable(){
@Override
public void run() {
try {
// 创建服务端socket
ServerSocket serverSocket = new ServerSocket(8088);
logger.info("listen:8088");
Thread thread1=new Thread() {
public void run() {
Socket socket ;
//循环监听等待客户端的连接
while(true){
try {
socket = serverSocket.accept();
// 监听客户端
InetAddress address=socket.getInetAddress();
System.out.println("当前客户端的IP:"+address.getHostAddress());
ServerThread3 thread = new ServerThread3(socket,a);
// thread.start();
executorService.submit(thread);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// thread.run();
}
}
};
thread1.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
};
try {
SwingUtilities.invokeLater(socketThread);
} catch (Exception e1) {
e1.printStackTrace();
}
}
这是一个Java程序,定义了主进程函数,它创建了一个JFrame窗口,并初始化了一个固定大小为5的线程池。服务端通过ServerSocket在8088端口监听,当有客户端连接时,创建新的线程处理连接,并使用ExecutorService提交任务。异常处理确保程序稳定运行,打印客户端IP地址。
3717

被折叠的 条评论
为什么被折叠?



