Skip to content

The Network.Socket library does not take into account that the bits in the network are sent as big enddian.

We got the program (1) below that is supposed to open a server in the port 51777. However, when we run the program, we observed that the port created was the number 16842. We believe that this difference comes from the fact that we run the program in a little enddian machine (Intel processor) and the socket library consider big enddian numbers instead. If we take 51777 and converted as indicated by function conv (see (2)), the socket open is indeed 51777.

Kind regards, Alejandro Russo & Josef Svenningsson

(1)

import Network.Socket import System

main = do s <- socket AF_INET Stream 0

arg <- getArgs

let shost = head arg

port = PortNum 51777

host <- inet_addr shost

putStrLn "Making the server..."

bindSocket s (SockAddrInet port host)

b <- sIsBound s

putStrLn "Socket bounded!"

listen s 5

putStrLn "Listening OK"

putStrLn "Waiting for clients..."

(s', c) <- accept s

putStrLn "Client found!"

str <- recv s' 255

putStr "Message received:"

putStr str

(2)

conv :: Word16 -> Word16 conv w16 = shiftL (fromIntegral a) 8 .|. fromIntegral b

where a = fromIntegral w16 Word8
b = fromIntegral (shiftR w16 8) Word8
Trac metadata
Trac field Value
Version
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/network
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information