getProtocolByName

Dominic Steinitz dominic.j.steinitz@britishairways.com
Sun, 3 Mar 2002 17:42:56 +0000


<P>&nbsp;Finn,</P><P>&nbsp;</P><P>Thanks for this. </P><P>I thought since I=
 wasn't actually using the socket module I wouldn't have to use withSockets=
Do.</P><P>The documentation for BSD is a little spartan and a few explanato=
ry words about how errors get handled wouldn't go amiss.</P><P>Anyway, it n=
ow works so I'll move on to the next bit.</P><P>Dominic.<BR><BR><FONT SIZE=
=3D2><B>Finn Wilcox &lt;finnw@sucs.org&gt;</B></FONT><BR><FONT SIZE=3D2>03/=
03/2002 09:30</FONT><BR><FONT SIZE=3D2>Please respond to finnw</FONT><BR><B=
R> <FONT SIZE=3D2>To:</FONT> <FONT SIZE=3D2>dominic.j.steinitz@britishairwa=
ys.com</FONT><BR> <FONT SIZE=3D2>cc:</FONT> <FONT SIZE=3D2>haskell@haskell.=
org</FONT><BR> <FONT SIZE=3D2>bcc:</FONT> <BR> <FONT SIZE=3D2>Subject:</FON=
T> <FONT SIZE=3D2>Re: getProtocolByName</FONT><BR> <BR><BR></P><P><FONT FAC=
E=3D"Monospace,Courier">On Sun, 3 Mar 2002 dominic.j.steinitz@britishairway=
s.com wrote:<BR></FONT><BR><FONT FACE=3D"Monospace,Courier">&gt; Here's my =
test program. TCP is a valid protocol. But I get the following<BR>&gt; when=
 I run it. I'm running under W98 by the way. I have two questions:<BR>&gt;<=
BR>&gt; 1) Why do I get an error?<BR></FONT><BR><FONT FACE=3D"Monospace,Cou=
rier">You want:<BR>&gt; module Main(main) where<BR>&gt;<BR>&gt; import BSD<=
BR> &nbsp;import Socket (withSocketsDo)<BR>&gt;<BR>&gt; main =3D<BR> &nbsp;=
 &nbsp; withSocketsDo $<BR>&gt; &nbsp; &nbsp;do protocolEntry &lt;- getProt=
ocolByName &quot;TCP&quot;<BR> &nbsp; &nbsp; ...<BR>&gt;<BR>See <A HREF=3Dh=
ttp://www.haskell.org/ghc/docs/latest/set/socket.html#AEN14601>http://www.h=
askell.org/ghc/docs/latest/set/socket.html#AEN14601</A><BR></FONT><BR><FONT=
 FACE=3D"Monospace,Courier">&gt; 2) Even if this is a valid error why doesn=
't the function tell me so that I<BR>&gt; can handle it rather than termina=
ting?<BR>&gt;<BR>It is a valid error. &nbsp;It is raising an exception whic=
h you can catch if you<BR>want. e.g.<BR></FONT><BR><FONT FACE=3D"Monospace,=
Courier">import IO (catch, isDoesNotExistError)<BR>...<BR> &nbsp; catch (do=
 protocolEntry &lt;- getProtocolByName &quot;TCP&quot;<BR> &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; putStrLn (&quot;Protocol name:&quot; ++ (protoNam=
e protocolEntry)))<BR> &nbsp; &nbsp; &nbsp; &nbsp; (\e -&gt; if isDoesNotEx=
istError e then<BR> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
 &nbsp; putStrLn &quot;Error&quot; else<BR> &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; ioError e)<BR></FONT><BR><FONT FACE=3D"Mono=
space,Courier">Finn.</FONT></P>=