如何管理服务器中与不同计算机上的多个客户端连接的应用程序的多个实例
我有一个应用程序需要与远程计算机上的客户端配对。服务器上可以运行该应用程序的多个实例,每个实例为不同的客户端提供服务,可能多达 25-30 对。
每对服务器-客户端都需要有两个 tcp 连接。服务器应用程序有 2 个进程,每个进程都有自己的与客户端的 tcp 连接。
- 进程A与客户端交换命令
- 进程B从客户端接收恒定的视频流。
此外,每对进程 A/进程 B 之间还有另一个 tcp 连接,
命令/数据将如下所示流动:
客户端 <-->过程A<--->处理 B
,同时视频将流向此方向:
客户端 -->过程 B
我需要帮助来确定建立所有 tcp 连接的最佳方法。谁设置监听器?如何确定要使用的端口? 我目前有一个进程 A 的实例与进程 B 和客户端进行通信,但它都在一台
机器上运行,所以我现在只使用“localhost”作为我的主机名。我还对我正在使用的 2 个端口进行了硬编码,因此当我有多个实例时,我也需要更改它,以便每个实例使用不同的端口。
一旦进程A与客户端建立连接,它需要向客户端请求端口,以便进程B可以直接与客户端建立另一个连接。这是一个好方法,还是更好的方法?客户端如何确定哪个端口可供使用?
此外,有关如何分配端口以便可以与多个实例一起使用的任何帮助或指示将不胜感激。
谢谢,有什么不明白的地方请追问。
编辑:实际上进程A和进程B是两个不同的应用程序。
I have an application that needs to be paired with a client on a remote machine. There can be several instances of this application running on the server, each servicing a different client, probably as many as 25-30 pairs.
Each pair of server-client will need to have two tcp connections. The server app has 2 processes, and each process has its own tcp connection to the client.
- Process A exchanges commands with client
- Process B receives constant video stream from the client.
In addition, there's another tcp connection between each pair Process A/Process B
Commands/Data will flow from as follows:
Client <--> Process A <---> Process B
while video will flow in this direction:
client --> Process B
I need help to determine what is the best way to establish all the tcp connections. Who sets up listeners? how to determine the ports to be used? etc.
I currently have a single instance of Process A communicating with both Process B and client, but it is all running in one machine, so I am just using "localhost" as my hostname for now. I am also hardcoding the 2 ports that I am using, so I need to change that as well when I have multiple instances so that each instance uses different ports.
Once Process A makes a connection with Client, it needs to ask the Client for a port so that Process B can make the other connection directly with Client. Is this a good way to do it, or what is a better way? How can the client determine what port is available to be used?
Also any help or pointers on how to assign the ports so this can work with multiple instances would be appreciated.
Thanks, please ask any questions about anything that is not clear.
EDIT: Actually process A and process B are two different applications.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对应用程序了解不够,因此可能可以进一步简化,但我会执行以下操作:
创建一个或两个监听已知端口的服务器进程(您可能可以将其减少到一个,但是如果您需要两个,好的,我会解释如何做到这一点)。
如果您可以将其简化为单个服务器进程,则它本身可以侦听两个端口。使用
select
调用,您可以让它监视每个打开的文件描述符上的活动。事实上,这就是为什么我认为您可以使用单个进程或一小部分进程来完成整个任务。让客户端连接到服务器上的已知端口。当您进入远程网络时,让服务器连接到客户端会给您带来麻烦。 放在具有端口转发功能的某个位置或位于 DMZ 中,但您不能坚持要求所有客户端更改其网络以删除 NAT、禁用防火墙或为您的应用程序转发端口。
当客户端连接时,如果您需要多个进程,请在分叉之前让服务器相互建立开放连接,以便您可以使用已知端口连接它们。
如果需要单独的进程,请使用
fork
来拆分进程。在子进程中,您关闭正在侦听已知端口的文件描述符。在父进程中,除了连接到其他服务器进程的文件描述符之外,还可以关闭连接到客户端的文件描述符。注意:这是来自 Linux 背景,但我很确定所有这些函数和方法都可以在 Windows 上运行。我确信如果我错了,会有人纠正我。我强烈建议尝试使用
select
将其简化为单个进程,这将大大简化您的编码。如果您需要更好地利用多处理器系统,请使用多线程或拥有一个权衡连接处理的进程池。如果您需要创建多个进程,了解文件描述符在fork
之后如何工作将简化您的编码。I don't know enough about the application, so it's possible this can be further simplified, but I would do the following:
Create one or two server processes listening on known ports (you could probably get this down to one, but if you need two, ok I'll explain how to do that).
If you can get it down to a single server process, it can listen on both ports itself. Using the
select
call, you can have it monitor for activity on each open file descriptor. In fact, this is why I think you could do the entire thing with a single process, or a small pool of them.Have the client connect to the known port on the server. Having the server connect to the client will give you trouble when you get into remote networks. You can always get the server somewhere with port forwarding or in a DMZ, but you can't insist that all of your clients change their network to remove NAT, disable firewalls, or forward ports for your application.
When the client connects, if you need multiple processes, have the servers establish as open connection to each other before forking so that you can connect them using the known port.
If you need a separate process, use
fork
to split the processes. In the child process, you close the file descriptors that are listening on the known ports. And in the parent process, you close the file descriptors that are connected to the clients in addition to the file descriptors that are connected to the other server process.Note: this is from a linux background, but I'm pretty sure all of these functions and methods will work on windows. I'm sure someone will correct me if I'm wrong. I strongly suggest trying to use
select
to get this down to a single process which will greatly simply your coding. If you need to make better use of a multi-processor system, then go multi-threaded or have a pool of processes that trade-off the connection handling. Learning about how file descriptors work after afork
will simplify your coding should you need to create multiple processes.