instance Show SockAddr

Peter Simons simons at cryp.to
Fri Oct 15 10:24:01 EDT 2004


Hi,

I've found myself needing to show a SockAddr type left and
right in my code. Is it possible to add the appropriate
definition to Network.Socket? Here is a suggestion:

instance Show SockAddr where
  show (SockAddrUnix str) = str
  show (SockAddrInet port ha) =
    let (b1,b2,b3,b4) = ha2tpl ha
    in
    shows b1 . ('.':) .
    shows b2 . ('.':) .
    shows b3 . ('.':) .
    shows b4 . (':':) $ show port

-- |Split up an 32-bit word in network byte order.

ha2tpl :: HostAddress -> (Int, Int, Int, Int)
ha2tpl n =
  let (b1,n1) = (n  .&. 255, n  `shiftR` 8)
      (b2,n2) = (n1 .&. 255, n1 `shiftR` 8)
      (b3,n3) = (n2 .&. 255, n2 `shiftR` 8)
      b4      = n3 .&. 255
  in
  (fromEnum b1, fromEnum b2, fromEnum b3, fromEnum b4)

Peter



More information about the Libraries mailing list