如何将本地unix套接字映射到inet套接字? [英] How can I map a local unix socket to an inet socket?

查看:192
本文介绍了如何将本地unix套接字映射到inet套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇是否可以将UNIX套接字映射到INET套接字.情况很简单,我想连接到MySQL服务器.不幸的是,它禁用了INET套接字,因此我只能使用UNIX套接字进行连接.我正在使用/编写的工具必须连接到INET套接字上,因此我试图查看是否可以将一个映射到另一个.

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.

进行了大量搜索,但我确实找到了 socat ,据说我在寻找什么.我想知道是否有人对如何实现这一目标有任何建议.我一直使用的命令行(部分成功)是:

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

现在,我可以建立连接并与服务器对话.不幸的是,由于我需要使用fork选项,因此建立多个连接的任何尝试都会失败,但是此选项似乎使连接无法正常工作.

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.

我知道我可以使用Perl(我的首选语言)解决该问题,但是我宁愿避免自己编写整个实现.我熟悉IO :: Socket库,我只是希望任何人都有做这种事情的经验.打开建议/想法.

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.

谢谢.

推荐答案

将参数的顺序反转为socat,它可以工作.

Reverse the order of your arguments to socat, and it works.

socat -v tcp-l:6666,reuseaddr,fork unix:/var/lib/mysql/mysql.sock

这指示socat

  1. 在TCP端口6666上侦听(使用SO_REUSEADDR)
  2. 等待接受连接
  3. 建立连接后,进行分叉.在孩子中,继续以下步骤.在父级中,转到2.
  4. 打开到/var/lib/mysql/mysql.sock套接字的UNIX域连接.
  5. 在两个端点之间传输数据,然后退出.
  1. Listen on TCP port 6666 (with SO_REUSEADDR)
  2. Wait to accept a connection
  3. When a connection is made, fork. In the child, continue the steps below. In the parent, go to 2.
  4. Open a UNIX domain connection to the /var/lib/mysql/mysql.sock socket.
  5. Transfer data between the two endpoints, then exit.


反之亦然


Writing it the other way around

socat -v unix:/var/lib/mysql/mysql.sock tcp-l:6666,reuseaddr,fork

不起作用,因为这指示socat

doesn't work, because this instructs socat to

  1. 打开到/var/lib/mysql/mysql.sock套接字的UNIX域连接.
  2. 在TCP端口6666上侦听(使用SO_REUSEADDR)
  3. 等待接受连接
  4. 建立连接后,产生一个工作子进程以在两个地址之间传输数据.
  5. 父级继续接受第二个地址上的连接,但不再有第一个地址可用:它已分配给第一个孩子.因此,从现在开始,没有什么可以做的.
  1. Open a UNIX domain connection to the /var/lib/mysql/mysql.sock socket.
  2. Listen on TCP port 6666 (with SO_REUSEADDR)
  3. Wait to accept a connection
  4. When a connection is made, spawn a worker child to transfer data between the two addresses.
  5. The parent continues to accept connections on the second address, but no longer has the first address available: it was given to the first child. So nothing useful can be done from this point on.

这篇关于如何将本地unix套接字映射到inet套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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