使用线程池请求服务器程序
我正在寻找使用线程池概念接受多个客户端连接的服务器程序的代码,这样每个客户端都可以向服务器发送一个文件,并且服务器应该通过发送相应的响应文件来响应。
我问这个问题是因为此时我能够收到多个客户端请求,但我无法生成包含相应响应文件的响应。
I am looking for code for a server program that accepts several client connections using the threadpool concept, such that each client can send a file to the server, and the server should respond by sending the corresponding response file.
I am asking this because at this point I am able to get several client requests, but I am not able to generate responses that include the corresponding response files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数现代 J2EE 应用服务器都支持这个概念。将您的应用程序转换为 WAR 并使用服务器线程池/请求超时设置。
更新:如果您不喜欢应用程序服务器,请使用Executors 来自 JDK 的实用程序类,通过将长时间运行的操作包装到 Runnable 接口中并为其创建一个 ThreadFactory 来创建一个新的 ExeutorService,然后使用 awaitTermination 方法。如果它返回 false,那么您的线程遇到超时,您可以向客户端发送一些不错的错误消息。
Most of modern J2EE application servers support that concept. Transform your application into a WAR and play with server thread pool/request time out settings.
UPDATE: If you don't like application servers, then use Executors utility class from JDK, create a new ExceutorService through that by wrapping your long running action into Runnable interface and creating a ThreadFactory for that, and then use awaitTermination method. If it returns false, then your thread encountered a time out and you can send some nice error message to your client.