Go语言中的Unix域套接字名称

huangapple go评论85阅读模式
英文:

Unix domain socket name in go language

问题

在Go语言的net包中,提供了以下函数:

func ResolveUnixAddr(net, addr string) (*UnixAddr, error)

其中,字符串参数net表示网络名称,可以是"unix"、"unixgram"或"unixpacket"。

我猜测网络名称的含义如下:

  • unixgram:在socket()函数中作为SOCK_DGRAM类型,由net包中的ListenPacket()函数使用。
  • unixpacket:在socket()函数中作为SOCK_STREAM类型,由net包中的Listen()函数使用。
  • unix:可能是其他类型。

我猜的对吗?

英文:

The net package in go provides this function:

func ResolveUnixAddr(net, addr string) (*UnixAddr, error)

The string parameter net gives the network name, "unix", "unixgram" or "unixpacket".

I guess the the network name's meaning as:

  • unixgram: as type SOCK_DGRAM in socket() function, used by ListenPacket() in net package.

  • unixpacket: as type SOCK_STREAM in socket() function, used by Listen() in net package.

  • unix: either

Am I right?

答案1

得分: 9

net/unixsock_posix.go中查看unixSocket函数,我们有:

var sotype int
switch net {
case "unix":
        sotype = syscall.SOCK_STREAM
case "unixgram":
        sotype = syscall.SOCK_DGRAM
case "unixpacket":
        sotype = syscall.SOCK_SEQPACKET
default:
        return nil, UnknownNetworkError(net)
}

所以你对于unixgram是数据报套接字是正确的,但是unix是指流类型套接字,而unixpacket是指顺序数据包套接字(即数据以数据报套接字的方式发送,但按顺序传递)。

英文:

Looking at the unixSocket function in net/unixsock_posix.go, we have:

var sotype int
switch net {
case "unix":
        sotype = syscall.SOCK_STREAM
case "unixgram":
        sotype = syscall.SOCK_DGRAM
case "unixpacket":
        sotype = syscall.SOCK_SEQPACKET
default:
        return nil, UnknownNetworkError(net)
}

So you're right about unixgram being a datagram socket, but it is unix that refers to the the stream type socket and unixpacket refers to a sequential packet socket (i.e. data is sent in packets as with datagram sockets, but they are delivered in order).

huangapple
  • 本文由 发表于 2014年4月9日 15:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/22955226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定