更新时间:2022-06-18 22:35:29
多线程服务器的准系统示例:
A barebones example of a multi-threaded server:
//MyServer.java
public class MyServer {
private static int PORT = 12345;
public static void main(String args[]) {
ServerSocket s = new ServerSocket(PORT);
while(true) new MyServerThread(s.accept());
}
您的服务器主题:
//MyServerThread.java
public class MyServerThread implements Runnable {
private InputStream in = null;
private OutputStream out = null;
public MyServerThread(Socket s) {
in = s.getInputStream();
out = s.getOutputStream();
(new Thread(this)).start();
}
public void run() {
//do stuff with **in** and **out** to interact with client
}
}
此示例中缺少:
关闭()
输入套接字/流close()
ing the sockets/streams希望这会给你一些想法通常看起来如何。
Hopefully that gives you some idea of how it usually looks.