lua-socket:Unix域套接字? [英] lua-socket: unix domain sockets?

查看:162
本文介绍了lua-socket:Unix域套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用lua-socket 3.0rc1.3(Ubuntu Trusty随附)和lua 5.1.我正在尝试在Unix域套接字上进行侦听,我可以找到的唯一示例代码是

I'm using lua-socket 3.0rc1.3 (that comes with Ubuntu Trusty) and lua 5.1. I'm trying to listen on a unix domain socket, and the only example code I can find is this

-- send stdin through unix socket
socket = require"socket"
socket.unix = require"socket.unix"
c = assert(socket.unix())
assert(c:connect("/tmp/foo"))
while 1 do
    local l = io.read()
    assert(c:send(l .. "\n"))
end

问题是,当我尝试使用connect()时,我得到没有这样的文件或目录"-首先如何创建该套接字?有人向我推荐的mkfifo /tmp/foo却给我一个连接被拒绝"错误(我不认为fifo与域套接字是一回事吗?).

Problem is, when I try and connect() I get "no such file or directory" - how do I create that socket in the first place? mkfifo /tmp/foo which someone recommended me gets me a "connection refused" error instead (I don't think a fifo is the same thing as a domain socket?).

在unix域套接字上使用luasocket是否有任何最小的可行示例?

Is there any minimal working example out there of using luasocket on a unix domain socket?

根据Paul的解决方案,如果有人感兴趣,这里是MWE

libsocket = require "socket"
libunix = require "socket.unix"
socket = assert(libunix())
SOCKET="/tmp/socket"
assert(socket:bind(SOCKET))
assert(socket:listen())
conn = assert(socket:accept())
while 1 do
    data=assert(conn:receive())
    print("Got line: " .. data)
    conn:send("echo: " .. data .. "\n")
    if data == "." then conn:close() return end
end

推荐答案

据我了解,您不能使用mkfifo(或任何命令)创建该套接字,因为它将由(侦听)服务器创建.在您引用的同一页上列出了一个示例,但是可能很难找到:

As far as I understand, you can't create that socket using mkfifo (or any command) as it will be created by the (listening) server. There is an example listed on the same page you referenced, but it may be difficult to find:

sock, err = s:listen([backlog|_32_])
sock, err = s:bind(path)
client_conection, err = s:accept()

基本上,创建服务器的方式与为TCP创建服务器的方式相同,只是绑定而不是绑定到地址/端口,而是绑定到路径,然后开始在该路径上接受新的连接.

Basically, you create the server the same way you'd do it for TCP, only instead of binding to an address/port, you bind to a path and then start accepting new connections on it.

这篇关于lua-socket:Unix域套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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