简体   繁体   English

将套接字路由到另一个端口

[英]Routing sockets to another port

I have a system where I want to listen to a socket and wait to client connect and then pass the connection to another application that I'll start as soon as the connection is established. 我有一个系统,我想听一个套接字并等待客户端连接,然后将连接传递给另一个应用程序,我将在建立连接后立即启动。

I do not have control on this other application and can only set the port where it will listen, but I want to have one process for each new client. 我没有对这个其他应用程序的控制,只能设置它将侦听的端口,但我希望每个新客户端都有一个进程。

This is what I'm trying to do: 这就是我想要做的:

路由

I've been searching for a solution, but I thing I don't have the right terminology, but I managed to find on Richard Stevens' "Unix Network Programming" something about the AF_ROUTE family of sockets that may be combined with a SOCK_RAW to route a connection to another IP and port. 我一直在寻找一个解决方案,但是我没有正确的术语,但我设法在Richard Stevens的“Unix网络编程”中找到了一些关于AF_ROUTE系列套接字的信息,这些套接字可以与SOCK_RAW结合使用。路由到另一个IP和端口的连接。 But there's too little documentation about how to use this flag and seems to require superuser privileges (that I want to avoid). 但是关于如何使用这个标志的文档太少,似乎需要超级用户权限(我想避免)。

Maybe there's an easier solution but I'm probably using the wrong terms. 也许有一个更简单的解决方案,但我可能使用了错误的术语。 Is it clear what I want to do? 我明白要做什么?

I don't think you'll be able to just "pass" the socket like you want to, especially if you can't change and recompile "APP". 我不认为你能够像你想要的那样“传递”套接字,特别是如果你不能改变并重新编译“APP”。 Sockets include various administrative overhead (resource management, etc) that are linked to the process they are owned by. 套接字包括各种管理开销(资源管理等),这些开销链接到它们所拥有的进程。 In addition, if you can't recompile APP, there is no way to make it bypass the steps involved with accepting a connection and simple have an already open connected "handed" to it by your router. 此外,如果您无法重新编译APP,则无法绕过接受连接所涉及的步骤,并且简单地已经通过您的路由器“已经打开”连接到它。

However, have you considered simply using router as a pass-through? 但是,您是否考虑过简单地使用路由器作为传递? Basically, have your "Router" process connect via sockets to the each "APP" process it spawns, and simply echo whatever it recieves from the appropriate client to the appropriate APP, and visa versa for APP to client? 基本上,让你的“路由器”进程通过套接字连接到它产生的每个“APP”进程,并简单地回应它从适当的客户端收到的相应APP,反之亦然APP对客户端?

This does add overhead, and you will have to manage a small mapping to keep track of which clients go to which apps, but it might work (assuming the APP or client aren't basing any behavior off of the IP address they are connected to, etc). 这确实增加了开销,你必须管理一个小的映射来跟踪哪些客户端去哪些应用程序,但它可能会起作用(假设APP或客户端没有基于他们所连接的IP地址的任何行为)等)。 Assuming you can't recompile APP, there might not be too many other options. 假设您无法重新编译APP,可能没有太多其他选项。

The code for this is relatively simple. 这个代码相对简单。 Your handler for data recieved from APP just looks up the socket for the appropriate app from your mapping, and then does a non blocking send of this data out on it. 从APP收到的数据处理程序只是从映射中查找适当应用程序的套接字,然后在其上执行非阻塞的数据发送。 Likewise the handler for data recieved from client. 同样,从客户端收到数据的处理程序。 Depending on how exactly the clients and app behave, you may have to handle a bit of synchronization (if you recieve from both simultaneously). 根据客户端和应用程序的行为方式,您可能需要处理一些同步(如果您同时接收它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM