服务器在多个端口上侦听[Java] [英] Server Listening on Multiple Ports [Java]

查看:162
本文介绍了服务器在多个端口上侦听[Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚如何创建一个可以监听多个端口并执行不同操作的java程序,具体取决于客户端所说的端口。

I am trying to figure out how to create a java program that can listen to multiple ports and perform different actions depending on which port the client speaks to.

I已经看到并理解了基本的客户端 - 服务器程序:
http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/

I've seen and understand the basic client-server program: http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/

重申一下,我想创建同样的关系,但是服务器只接听一个端口并在接收输入时执行一个操作,而不是服务器,而是希望它在多个端口上侦听,并根据客户端连接和发送数据的端口,执行不同的操作。

Just to reiterate, I want to create this same relationship, but instead of the server only listening on one port and performing one action when it receives input, I want it to listen on multiple ports and depending which port the client connects and sends data to, perform a different action.

我希望将来每个端口都接受GET和PUT命令,但是现在我只想弄清楚如何设置服务器的基本结构,可以监听多个端口。我试过谷歌搜索,但我似乎找不到多少,所以任何帮助都表示赞赏。

I'm hoping to make each port accept a GET and PUT command in the future, but for now I'm just trying to figure out how to set up the basic structure of the server which will be able to listen to multiple ports. I've tried googling, but I can't seem to find much, so any help is appreciated.

提前致谢。
-Anthony

Thanks in advance. -Anthony

推荐答案

套接字只能对特定端口开放,因此需要多个服务器套接字(例如每个端口1个插槽)。我你还需要每个插槽一个线程,这样一个套接字上的网络活动不会干扰其他套接字上的活动。

A socket can only be open to a particular port, so you need multiple server sockets (e.g. 1 socket per port). I think you also need one thread per socket so that network activity on one socket doesn't interfere with activity on the others.

你是实施服务器作为学术练习?如果没有,我真的确实 真的强烈建议使用预先存在的服务器,例如 Tomcat

Are you implementing a server as an academic exercise? If not, I'd really really really really strongly recommend using a preexisting server, like Tomcat.

这篇关于服务器在多个端口上侦听[Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆