英文:
how to make unix socket listener
问题
这是一个比较基础的Go语言习惯用法问题,但这个例子很好。 (顺便说一句,我是完全的Go新手)
我试图监听Unix套接字并处理消息。我从各个地方抄了一些代码,但我无法正确地“转换”东西。
package main
import "fmt"
import "net"
func main(){
ln,err := net.Listen("unix", "/var/service/daemon2")
if err!= nil {
fmt.Println(err)
return
}
for {
c, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
// 处理连接
go handleServerConnection(c)
}
}
func handleServerConnection(c net.UnixConn) {
// 接收消息
buff := make([]byte, 1024)
oob := make([]byte, 1024)
_,_,_,_,err:=c.ReadMsgUnix(buff,oob);
if err != nil {
fmt.Println(err)
}
}
我需要在handleServerConnection函数中的c
变量的类型为UnixConn,以便我可以调用ReadMsgUnix函数。但是通用的Listen代码生成了一个通用的Conn对象。所以这段代码无法编译。
我尝试了各种类型转换,比如UnixConn(c),但都没有成功。
英文:
this is really a more basic go idiom question but this serves as a good example. (BTW 100% go newb)
Trying to listen to unix socket and process messages. Stolen code from various places but I cant 'cast' things right
package main
import "fmt"
import "net"
func main(){
ln,err := net.Listen("unix", "/var/service/daemon2")
if err!= nil {
fmt.Println(err)
return
}
for {
c, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
// handle the connection
go handleServerConnection(c)
}
}
func handleServerConnection(c net.UnixConn) {
// receive the message
buff := make([]byte, 1024)
oob := make([]byte, 1024)
_,_,_,_,err:=c.ReadMsgUnix(buff,oob);
if err != nil {
fmt.Println(err)
}
}
I need 'c' inside handleServerConnection to be of type UNixConn so that I can call ReadUNixMsg. But the generic Listen code makes a generic Conn object. So this code doesnt compile.
I tried various convert / cast type things UnixConn(c) for example but all to no avail.
答案1
得分: 7
将连接转换为以下形式:
go handleServerConnection(c.(*net.UnixConn))
并将函数的签名更改为:
func handleServerConnection(c *net.UnixConn) {
这里发生的情况是,net.Listen
返回一个 Listener
接口,所有监听套接字都实现该接口。实际对象是指向 net.UnixConn
的指针,它实现了 Listener
接口。这允许你进行类型断言/转换。当然,如果对象实际上不是 Unix 套接字,这将失败,所以最好先验证断言。
这是关于这个内容你需要知道的信息:http://golang.org/doc/effective_go.html#interface_conversions
英文:
Cast the connection like this:
go handleServerConnection(c.(*net.UnixConn))
and change the function's signature to:
func handleServerConnection(c *net.UnixConn) {
What happens here is that net.Listen
returns a Listener
interface, which all listener sockets implement. The actual object is a pointer to net.UnixConn
which implements the Listener
interface. This allows you to do type assertion/conversion. This will fail of course if the object is not really a unix socket, so you'd better validate the assertion first.
Here's what you need to know about this stuff: http://golang.org/doc/effective_go.html#interface_conversions
答案2
得分: 2
你要做的是将net.Listen
替换为net.ListenUnixgram("unix", net.ResolveUnixAddr("unix","/path/to/socket")
,这将返回你所需的net.UnixConn
对象。
英文:
What you are looking for is to replace your net.Listen
with net.ListenUnixgram("unix", net.ResolveUnixAddr("unix","/path/to/socket")
which will return the net.UnixConn
object that you want.
答案3
得分: 0
你可以使用net.ListenUnix()函数,它会返回一个UnixListener对象,你可以调用它的AcceptUnix方法来接受连接,该方法会返回一个*net.UnixConn对象。
但是Not_a_Golfer的解决方案也很好
英文:
Or you can use net.ListenUnix() which return a UnixListener on which you can call AcceptUnix which returns a *net.UnixConn.
But Not_a_Golfer solution works fine
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论