处理多个java客户端的服务器
伙计们!我编写了简单服务器客户端(udp)应用程序。现在,我正试图使服务器接受许多客户端。据我所知,我需要创建接受和处理客户端的函数,但我对应用程序的结构感到困惑。你能检查一下我的应用程序是否有合适的框架吗?也许你能给我一些提示或例子。感谢您的建议!:)处理多个java客户端的服务器,java,multiserver,Java,Multiserver,伙计们!我编写了简单服务器客户端(udp)应用程序。现在,我正试图使服务器接受许多客户端。据我所知,我需要创建接受和处理客户端的函数,但我对应用程序的结构感到困惑。你能检查一下我的应用程序是否有合适的框架吗?也许你能给我一些提示或例子。感谢您的建议!:) 您正在寻找线程池服务器。你开始的方式很好。现在您只需实现一个Java执行服务来处理请求。线程池具有固定的线程数。它会接受您的请求并将它们放入队列中,如果一个请求完成,它会接受下一个请求。所以你通常不会丢失任何请求 下面是我举的一个小例子: pu
您正在寻找线程池服务器。你开始的方式很好。现在您只需实现一个Java执行服务来处理请求。线程池具有固定的线程数。它会接受您的请求并将它们放入队列中,如果一个请求完成,它会接受下一个请求。所以你通常不会丢失任何请求 下面是我举的一个小例子:
public class PoolServer implements Runnable {
private static final int DEFAULT_PORT = 8080;
private static final String CONFIG = "config.xml";
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread = null;
protected ExecutorService threadPool = Executors.newFixedThreadPool(100);
protected int serverPort;
public PoolServer() {
// getting the port from the XML
this.serverPort = getPortFromXML();
}
public void run() {
synchronized (this) {
this.runningThread = Thread.currentThread();
}
openServerSocket();
// accepting loop
while (!isStopped()) {
Socket clientSocket = null;
try {
// accept the client
clientSocket = this.serverSocket.accept();
clientSocket.setSoTimeout(2000);
} catch (IOException e) {
if (isStopped()) {
return;
}
throw new RuntimeException("Error accepting client connection",
e);
}
this.threadPool.execute(new ThreadHandler(clientSocket));
}
// loop end
// server stopped shut down the ThreadPool
this.threadPool.shutdown();
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
throw new RuntimeException("Cannot open port " + this.serverPort, e);
}
}
此时this.threadPool.execute(newthreadhandler(clientSocket))如果线程空闲,我会执行请求。否则它将进入线程池的队列。
您甚至可以将其从固定线程池更改为其他线程池!只要看看遗嘱执行人,拿你需要的
希望这有帮助 那么您希望您的服务器能够同时处理多个请求?很好,这就是大多数web服务器的工作方式。您必须理解多线程和并发的基本概念
一个简单的服务器一次只能处理一件事情。如果在服务器处理其他事务时收到另一个请求,会发生什么情况?什么都没有,所以应用程序效率不高,根本无法扩展
如果您还没有在应用程序中使用多线程,也不太了解并发性,那么现在正是尝试、阅读或在线查找教程的好时机,有很多
现在,一旦(或如果)您知道线程是如何工作的,请确保尽可能地分解函数,并查看哪些函数可以同时发生。我能想到的一个Web服务器示例是:
- 在端口上侦听请求的单独线程。收到请求后,将其放入“请求池”并将其排队等待处理
- 处理请求的单独线程(或多线程/线程池)
您的结构看起来好像在同一个Runnable中同时有receive和process。无论如何,这只是一个想法,你必须看看什么更适合你的应用程序。另外,看看较新的Java版本提供的并发工具,Java 6和7提供了许多非常有效的工具(但在我看来,这些工具很难理解和使用)
祝你好运
public class PoolServer implements Runnable {
private static final int DEFAULT_PORT = 8080;
private static final String CONFIG = "config.xml";
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread = null;
protected ExecutorService threadPool = Executors.newFixedThreadPool(100);
protected int serverPort;
public PoolServer() {
// getting the port from the XML
this.serverPort = getPortFromXML();
}
public void run() {
synchronized (this) {
this.runningThread = Thread.currentThread();
}
openServerSocket();
// accepting loop
while (!isStopped()) {
Socket clientSocket = null;
try {
// accept the client
clientSocket = this.serverSocket.accept();
clientSocket.setSoTimeout(2000);
} catch (IOException e) {
if (isStopped()) {
return;
}
throw new RuntimeException("Error accepting client connection",
e);
}
this.threadPool.execute(new ThreadHandler(clientSocket));
}
// loop end
// server stopped shut down the ThreadPool
this.threadPool.shutdown();
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
throw new RuntimeException("Cannot open port " + this.serverPort, e);
}
}