Binary read/write WAS [storing to a file]

Shawn P. Garbett listman@garbett.org
Thu, 14 Nov 2002 16:59:42 -0600


On Thursday 14 November 2002 03:46 pm, you wrote:
> > I've been fiddling with binary read/write in Haskell. I put together
> > a little example demonstrating my lack of understanding. It creates
> > a connection requestion XAtom and spits it out over a socket. My
> > real hangup occurs when I get a String back from the Socket and
> > would like it nicely marshalled into the ConnectSuccess type. These
> > techniques I would assume apply to binary read/write for files as
> > well.
>
> IIRC, the Fudgets library reads and writes X sockets so you could look
> here:
>
>   http://www.cs.chalmers.se/ComputingScience/Research/Functional/Fudget=
s/

Xlib sockets and binding wasn't the point of the question. The example wa=
s=20
contrived to show the basic problem or reading / writing some arbitrary=20
binary sequence.=20

I looked at the solution in the Fudgets library, an interesting solution =
is=20
ine there for typed sockets.=20
http://www.cs.chalmers.se/~hallgren/Thesis/client_server.html. It allows =
for=20
a typed return on a socket, with a nice tight set of code. This typed ret=
urn=20
I understand relies on the types being members of the Read class.

The problem of the Xatom, is a bit more difficult. Embedded in the reply =
of=20
the open connection request is a "string" for the vendor. The length of t=
he=20
string is at byte 24 in the reply, it's a 2 byte Word. The string of the=20
vendor starts at byte 40. This is followed by a 4-byte padding. Once your=
=20
past this oddness, it gets a lot funner with all kinds of other embedded=20
information pointing all over the place in the packet.

I don't think String is a member of the Read class. So my thinking was to=
 read=20
the reply in as a String and process it. Problem is I don't even know how=
 to=20
extract the first four bytes of a String as a raw Word32. That would be a=
=20
start, but the general problem is a tad bit more difficult.

I've been reading some about the "Binary" module and it's translation int=
o GHC=20
going on. Is that the purpose of it to help in such situations?

Shawn