伙计们!我编写了简单的服务器客户端 (udp) 应用程序。现在我正在尝试制作可以接受许多客户端的服务器。据我了解,我需要创建接受和处理客户端的函数,但我对应用程序结构感到困惑。你能检查我的应用程序是否有正确的骨架吗?也许你可以给我一些提示或例子。所有建议表示赞赏!:)
class MultiServer {
private DatagramSocket serversocket;
public MultiServer() {
try {
this.serversocket = new DatagramSocket(6789);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void start() throws IOException {
while(true) {
DatagramSocket serversock = serversocket.accept();
new Thread(new ClientHandler(serversock)).start();
}
}
public static void main(String[] args) {
Server1 server = new Server1();
try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ClientHandler implements Runnable {
private final DatagramSocket clientsocket;
ClientHandler(DatagramSocket sock) {
this.clientsocket = sock;
}
@Override
public void run() {
//receive packet, send msg, get ip, get portnumber ?
}
}
}