[Haskell-beginners] Q) Using Data.Binary to parse a packet
Yang, Chul-Woong
cwyang at aranetworks.com
Tue Jul 28 20:19:39 EDT 2009
Dear Haskellers.
I would like to build simple UDP server application.
To parse a packet, I decide to use Data.Binary as followings:
data Packet = Packet {
foo :: Word16,
bar :: Word16,
baz :: Word32
} deriving (Show, Eq)
instance Binary Packet where
get = do
foo <- get :: Get Word16
bar <- get :: Get Word16
baz <- get :: Get Word32
return (Packet foo bar baz)
-- omitting put because we're now just receiving only
serveUDP port handlerfunc = withSocketsDo $
do
...
procMessages sock
where procMessages sock = do
(msg, _, addr) <- recvFrom sock 10240
handlerfunc addr msg
procMessages sock
myHandler addr msg = do
putStrLn $ decode (Data.ByteString.Lazy.Char8.Pack msg)
run = serveUDP "8080" myHandler
But, running it and sending sample udp packet results in following
exception.
*Main> run
Loading package syb ... linking ... done.
Loading package base-3.0.3.1 ... linking ... done.
Loading package array-0.2.0.0 ... linking ... done.
Loading package containers-0.2.0.1 ... linking ... done.
Loading package bytestring-0.9.1.4 ... linking ... done.
Loading package parsec-2.1.0.1 ... linking ... done.
Loading package network-2.2.1 ... linking ... done.
Loading package binary-0.5.0.1 ... linking ... done.
*** Exception: too few bytes. Failed reading at byte position 9
*Main>
Why and what can I do for this? Thanks in advance.
Chul-Woong Yang
More information about the Beginners
mailing list