Haskell将套接字绑定到特定的IP


Haskell网络包中

bindSocket使用HostAddress和HostAddress6

其定义为:

-- | Network byte order.
type HostAddress = Word32

-- | Host byte order.
type HostAddress6 = (Word32, Word32, Word32, Word32)

>我如何从123.123.123.123转到Word32?
>如何从IPv6字符串转到(Word32,Word32,Word32,Word32)?
> endianness是否在不同的操作系统(Windows,Linux,OS X,FreeBSD)上发挥作用?

最佳答案 使用
getAddrInfo

getAddrInfo :: Maybe AddrInfo -> Maybe HostName -> Maybe ServiceName -> IO [AddrInfo]   

Resolve a host or service name to one or more addresses. The AddrInfo values that this function returns contain SockAddr values that you can pass directly to connect or bindSocket.

This function is protocol independent. It can return both IPv4 and IPv6 address information.

点赞