java 实现socket连接的servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
servlet for implementing socket connection
提问by Kevin Boyd
How do I implement a web application with a Servlet that is listening on a port for socket connections?
如何使用侦听套接字连接端口的 Servlet 实现 Web 应用程序?
采纳答案by G__
I assume you don't mean the front-door HTTP connection, which you get for free with the servlet container... But if you want to add, say, an admin service you could create a listener thread that sets some global state in the servlet. Note that this is not considered kosher (and I believe may even violate the servlet standard).
我假设你不是指前门 HTTP 连接,你可以通过 servlet 容器免费获得它......但是如果你想添加一个管理服务,你可以创建一个侦听器线程来设置一些全局状态小服务程序。请注意,这不被视为 kosher(我认为甚至可能违反 servlet 标准)。
回答by Vineet Reynolds
Having the servlet open ServerSockets is a bad code smell. This is primarily because it is the container's responsibility to manage sockets (among other resources like worker threads, sessions etc).
让 servlet 打开 ServerSockets 是一种糟糕的代码味道。这主要是因为容器有责任管理套接字(以及其他资源,如工作线程、会话等)。
That said, I do not think you need a servlet in the first place. Unless you want to access some of the container's services, it would be better if you use a J2SE application to manage ServerSockets.
也就是说,我认为您首先不需要 servlet。除非您想访问容器的某些服务,否则最好使用 J2SE 应用程序来管理 ServerSockets。
回答by Helen Neely
Not totally sure what you want to achieve, but you can have a look at client/server programmingif that's what you need. Other than that, you could implement your web application as normal but change the default port to whatever suits your need.
不完全确定您想要实现什么,但是如果您需要,您可以查看客户端/服务器编程。除此之外,您可以照常实施您的 Web 应用程序,但将默认端口更改为适合您需要的任何端口。

