如何创建将侦听特定端口并接受 TCP 连接(非 https)的 servlet
我需要创建将侦听某些特定端口(如 serverSocket.accept())的 servlet,生成新线程(如普通 servlet)并将套接字(或只是输入流和输出流)传递给我的代码。 这个连接与http无关。我需要读取和写入二进制数据以打开连接。
用Tomcat可以吗?从哪里开始? 我搜索了 GenericSocket 示例,但我发现从 GenericSocket 继承的 servlet 仍然需要在 web.xml 中使用一些 url 模式进行配置 - 但这是 http 特定的。
I need to create servlet that will listen on some specific port (like serverSocket.accept()), spawn new thread (like normal servlet) and pass socket (or just inputstream and outputstream) to my code.
this connection has nothing to do with http. I need to read and write binary data to opened connection.
Is it possible with Tomcat? Where to begin ?
I've searched for GenericSocket examples, but I see that servlet inheriting from GenericSocket still has to be configured in web.xml with some url-pattern - but this is http specific.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
许多年前,当 Servlet 首次在 Java 中引入时,Jason Hunter 的《Java Servlet 编程》(由 OReilly 出版)是有关 Servlet 编程的最好的书籍之一。这本书附带了可下载的源代码示例,其中包括所谓的 DaemonHttpServlet 类,您可以对其进行子类化以准确执行您所要求的操作。
请参阅此链接以获取源代码: http://java .codefetch.com/example/qg/src/com/oreilly/servlet/DaemonHttpServlet.java
还有一个 servlet 也充当 RMI 服务器。
我明白为什么有人会看到以这种方式使用 servlet 的实用性;如果您的主应用程序是基于 HTTP 的 Web 应用程序,但应用程序的某些较小部分要求您通过原始套接字公开 RMI 接口或某些自定义接口,那么将它们实现为 servlet 将使您更容易将应用程序干净地部署为只是一个单个 .war 或 .ear 文件。
希望有帮助。
更新:一些快速谷歌搜索显示以下内容:
因此,对于 HTML5 WebSockets,使用 Servlet 进行非 http(s) 原始套接字工作似乎并不是一个非标准用例。
When servlets were first introduced in Java many many years ago, one of the best books on servlet programming was Jason Hunter's Java Servlet Programming (published by OReilly). The book came with downloadable source code examples that included what is called the DaemonHttpServlet class which you can subclass to do exactly what you are asking about.
See this link for the source code: http://java.codefetch.com/example/qg/src/com/oreilly/servlet/DaemonHttpServlet.java
There is also a servlet that acts as an RMI server as well.
I can see why someone would see the utility of using servlets in this way; if your main app is a HTTP-based web app but some smaller portion of your app requires you to expose an RMI interface or some custom interface through raw sockets, then implementing them as servlets would make it easier to cleanly deploy your application as just a single .war or .ear file.
Hope that helps.
Update: some quick googling reveals the following:
So with the HTML5 WebSockets, it would seem that using Servlets for non-http(s) raw socket work does not seem to be such a non-standard use-case after all.
您可以做到这一切,只需编写代码即可,但是为什么需要 servlet 部分呢?还是雄猫?
You can do all that, just write the code, but why do you need the servlet part? or Tomcat?
一定要查看用于实现套接字连接的servlet。您尝试做的事情是非标准的。
Definitely check out servlet for implementing socket connection. What you are trying to do is non-standard.
我想使用一些 servlet 容器(如 Tomcat),因为我也会使用 WebServices(作为客户端和服务器)。
I wanted to use some servlet container (like Tomcat), because i will use WebServices (as a client and server) too.