[Haskell-cafe] Sockets get bound to wrong port on Windows

Antoine Latter aslatter at gmail.com
Thu Jun 3 01:38:43 EDT 2010

On Wed, Jun 2, 2010 at 10:10 PM, Matthias Reisner
<matthias.reisner at googlemail.com> wrote:
> Hi,
> there's something wrong with port numbers in the Network.Socket module of
> package network. Printing values gives:
>   *Main> PortNum 8888
>   47138
>   *Main> PortNum 47138
>   8888
> So I thought it's just an error in the show instance of PortNumber, which
> shows the bytes flipped. But if I use the following code snippet
>   sock <- socket AF_INET Datagram 0
>   bindSocket sock $ SockAddrInet (PortNum 8888) iNADDR_ANY
> to bind a socket to port 8888, netstat and TCPView reveal that the socket is
> actually bound to the wrong port 47138. I'm using network- on Windows
> XP. Is that a bug or am I doing something wrong here?

Hi Matthias,

The PortNum constructor should rarely be used directly - it contains
the port number in network-order. You should try:

bindSocket sock $ SockAddrInet 8888 iNADDR_ANY

Here I'm we're a numeric literal, which implicitly calls (fromIntegral
8888), which correctly converts to network order for us. It's not
obvious from that haddocks that this is the way to go.

Take care,

More information about the Haskell-Cafe mailing list