Sockets get bound to wrong port on Windows

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-2.2.1.7 on Windows XP. Is that a bug or am I doing something wrong here? Regards, Matthias

Sorry for the double-reply Matthias, but I forgot to CC -cafe (GMail
labs isn't working, so reply-to-all is no longer the default for me :(
).
On 3 June 2010 13:10, Matthias Reisner
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
This happens to me as well (self-built GHC 6.12.2 with network 2.2.1.7 on some random Ubuntu version I use at uni).
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-2.2.1.7 on Windows XP. Is that a bug or am I doing something wrong here?
Tried testing this as well; not sure how to test if the binding worked but "netstat --numeric-ports" didn't contain 8888 or 47138. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

Am 03.06.2010 05:20 schrieb Ivan Miljenovic:
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-2.2.1.7 on Windows XP. Is that a bug or am I doing something wrong here?
Tried testing this as well; not sure how to test if the binding worked but "netstat --numeric-ports" didn't contain 8888 or 47138.
Hi Ivan, here's a complete program to reproduce the error: module Main (main) where import Network.Socket main :: IO () main = withSocketsDo $ do sock <- socket AF_INET Datagram 0 bindSocket sock $ SockAddrInet (PortNum 8888) iNADDR_ANY recvFrom sock 65535 return () The output of netstat/TCPView after starting the program is: MinSocketBind.exe:2724 UDP PC1:47138 *:* By the way, I'm using GHC 6.10.4, which I think shouldn't make a difference. Matthias

On 2010-06-03 05:10, Matthias Reisner 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
Try (fromIntegral 8888) :: PortNumber (Yes, it's weird.) Cheers, Bardur

Am 03.06.2010 07:34 schrieb Bardur Arantsson:
On 2010-06-03 05:10, Matthias Reisner 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
Try
(fromIntegral 8888) :: PortNumber
(Yes, it's weird.)
Cheers,
Bardur
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Hi Bardur, indeed, this is weird. Thanks for the hint! Regards, Matthias

On Wed, Jun 2, 2010 at 10:10 PM, Matthias Reisner
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-2.2.1.7 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, Antoine

Antoine Latter
*Main> PortNum 8888 47138
The PortNum constructor should rarely be used directly
So, shouldn't the constructor be hidden, and exported from an .Internal module?
- it contains the port number in network-order. You should try:
Or perhaps even better, the type could be: data PortNum = PortNum { msb, lsb :: Word8 } so that we avoid this kind of confusion? -k -- If I haven't seen further, it is by standing in the footprints of giants

Am 03.06.2010 08:05 schrieb Ketil Malde:
Antoine Latter
writes: *Main> PortNum 8888 47138
The PortNum constructor should rarely be used directly
So, shouldn't the constructor be hidden, and exported from an .Internal module?
- it contains the port number in network-order. You should try:
Or perhaps even better, the type could be:
data PortNum = PortNum { msb, lsb :: Word8 }
so that we avoid this kind of confusion?
-k
Hi, since this is really not obvious, I'd second to hide the constructor or at least a note should be added to the docs how to use PortNumber correctly. Regards, Matthias
participants (5)
-
Antoine Latter
-
Bardur Arantsson
-
Ivan Miljenovic
-
Ketil Malde
-
Matthias Reisner