本文整理汇总了Golang中syscall.Ntohs函数的典型用法代码示例。如果您正苦于以下问题:Golang Ntohs函数的具体用法?Golang Ntohs怎么用?Golang Ntohs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Ntohs函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newLookupPort
func newLookupPort(network, service string) (port int, err error) {
acquireThread()
defer releaseThread()
var stype int32
switch network {
case "tcp4", "tcp6":
stype = syscall.SOCK_STREAM
case "udp4", "udp6":
stype = syscall.SOCK_DGRAM
}
hints := syscall.AddrinfoW{
Family: syscall.AF_UNSPEC,
Socktype: stype,
Protocol: syscall.IPPROTO_IP,
}
var result *syscall.AddrinfoW
e := syscall.GetAddrInfoW(nil, syscall.StringToUTF16Ptr(service), &hints, &result)
if e != nil {
return 0, os.NewSyscallError("GetAddrInfoW", e)
}
defer syscall.FreeAddrInfoW(result)
if result == nil {
return 0, os.NewSyscallError("LookupPort", syscall.EINVAL)
}
addr := unsafe.Pointer(result.Addr)
switch result.Family {
case syscall.AF_INET:
a := (*syscall.RawSockaddrInet4)(addr)
return int(syscall.Ntohs(a.Port)), nil
case syscall.AF_INET6:
a := (*syscall.RawSockaddrInet6)(addr)
return int(syscall.Ntohs(a.Port)), nil
}
return 0, os.NewSyscallError("LookupPort", syscall.EINVAL)
}
示例2: lookupPort
func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
if r.PreferGo {
return lookupPortMap(network, service)
}
// TODO(bradfitz): finish ctx plumbing. Nothing currently depends on this.
acquireThread()
defer releaseThread()
var stype int32
switch network {
case "tcp4", "tcp6":
stype = syscall.SOCK_STREAM
case "udp4", "udp6":
stype = syscall.SOCK_DGRAM
}
hints := syscall.AddrinfoW{
Family: syscall.AF_UNSPEC,
Socktype: stype,
Protocol: syscall.IPPROTO_IP,
}
var result *syscall.AddrinfoW
e := syscall.GetAddrInfoW(nil, syscall.StringToUTF16Ptr(service), &hints, &result)
if e != nil {
if port, err := lookupPortMap(network, service); err == nil {
return port, nil
}
return 0, &DNSError{Err: winError("getaddrinfow", e).Error(), Name: network + "/" + service}
}
defer syscall.FreeAddrInfoW(result)
if result == nil {
return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service}
}
addr := unsafe.Pointer(result.Addr)
switch result.Family {
case syscall.AF_INET:
a := (*syscall.RawSockaddrInet4)(addr)
return int(syscall.Ntohs(a.Port)), nil
case syscall.AF_INET6:
a := (*syscall.RawSockaddrInet6)(addr)
return int(syscall.Ntohs(a.Port)), nil
}
return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service}
}
示例3: getservbyname
func getservbyname(network, service string) (port int, err error) {
switch network {
case "tcp4", "tcp6":
network = "tcp"
case "udp4", "udp6":
network = "udp"
}
s, err := syscall.GetServByName(service, network)
if err != nil {
return 0, os.NewSyscallError("GetServByName", err)
}
return int(syscall.Ntohs(s.Port)), nil
}
示例4: LookupPort
func LookupPort(network, service string) (port int, err os.Error) {
switch network {
case "tcp4", "tcp6":
network = "tcp"
case "udp4", "udp6":
network = "udp"
}
serventLock.Lock()
defer serventLock.Unlock()
s, e := syscall.GetServByName(service, network)
if e != 0 {
return 0, os.NewSyscallError("GetServByName", e)
}
return int(syscall.Ntohs(s.Port)), nil
}