如何将本地 unix 套接字映射到 inet 套接字?
我很好奇是否可以将 UNIX 套接字映射到 INET 套接字。 情况很简单,我想连接到 MySQL 服务器。 不幸的是它禁用了 INET 套接字,因此我只能使用 UNIX 套接字进行连接。 我正在使用/编写的工具必须连接到 INET 套接字,因此我试图看看是否可以将一个工具映射到另一个工具。
我花了相当多的搜索时间,但我确实找到了 socat ,据说它做了我的事情我在寻找。 我想知道是否有人对如何实现这一目标有任何建议。 我一直在使用的命令行(部分成功)是:
socat -v UNIX-CONNECT:/var/lib/mysql/mysql.sock TCP-LISTEN:6666,reuseaddr
现在我可以建立连接并与服务器交谈。 不幸的是,任何建立多个连接的尝试都会失败,因为我需要使用 fork 选项,但此选项似乎会使连接不起作用。
我知道我可以使用 Perl(我的首选语言)解决这个问题,但我宁愿避免自己编写整个实现。 我熟悉 IO::Socket 库,我只是希望任何人都有做这类事情的经验。 接受建议/想法。
谢谢。
I'm curious if it is possible to map a UNIX socket on to an INET socket. The situation is simply that I'd like to connect to a MySQL server. Unfortunately it has INET sockets disabled and therefore I can only connect with UNIX sockets. The tools I'm using/writing have to connect on an INET socket, so I'm trying to see if I can map one on to the other.
It took a fair amount of searching but I did find socat, which purportedly does what I'm looking for. I was wondering if anyone has any suggestions on how to accomplish this. The command-line I've been using (with partial success) is:
socat -v UNIX-CONNECT:/var/lib/mysql/mysql.sock TCP-LISTEN:6666,reuseaddr
Now I can make connections and talk to the server. Unfortunately any attempts at making multiple connections fail as I need to use the fork option but this option seems to render the connections nonfunctional.
I know I can tackle the issue with Perl (my preferred language), but I'd rather avoid writing the entire implementation myself. I familiar with the IO::Socket libraries, I am simply hoping anyone has experience doing this sort of thing. Open to suggestions/ideas.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将参数的顺序颠倒到
socat
即可,这样就可以了。这指示
socat
侦听SO_REUSEADDR
)/var/lib/mysql/mysql.sock
套接字的 UNIX 域连接。以相反的方式编写它
是行不通的,因为这指示socat
/var/lib/mysql/mysql.sock
套接字的UNIX域连接。Reverse the order of your arguments to
socat
, and it works.This instructs
socat
toSO_REUSEADDR
)/var/lib/mysql/mysql.sock
socket.Writing it the other way around
doesn't work, because this instructs
socat
to/var/lib/mysql/mysql.sock
socket.SO_REUSEADDR
)是的,您可以在 Perl 中做到这一点。
看看perlipc,IO::Select, IO ::Socket 和 Beej 网络编程指南。
Yes, you can do this in Perl.
Look at perlipc, IO::Select, IO::Socket and Beej's Guide to Network Programming.
您可能需要考虑在 POE 中执行此操作 - 它是用于处理事件的异步库,因此它看起来非常适合该任务。
它不是 100% 相关,但我使用 POE 在无状态协议(HTTP)和有状态协议(telnet 会话,更具体地说 - MUD 会话)之间编写代理,并且它相当简单 - 您可以在此处查看代码:< a href="http://www.depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/" rel="nofollow noreferrer">http://www.depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/" depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/。
在评论中,有人还建议了 Coro/AnyEvent - 我还没有玩过它,但你可能想检查一下。
You might want to consider doing it in POE - it's asynchronous library for dealing with events, so it looks like great for the task.
It is not 100% relevant, but I use POE to write proxy between stateless protocol (HTTP) and statefull protocol (telnet session, and more specifically - MUD session), and it was rather simple - You can check the code in here: http://www.depesz.com/index.php/2009/04/08/learning-poe-http-2-mud-proxy/.
In the comments somebody also suggested Coro/AnyEvent - I haven't played with it yet, but you might want to check it.