readping fd's and flushing buffers

Keean k.schupke@ic.ac.uk
Thu, 9 Jan 2003 21:37:07 -0000


A related problem... connections are refused when using accept if the
hostname doesn't resolve. Maybe something like this would help, unless there
is a better way?

accept sock = do
 ~(sock', (SockAddrInet port haddr)) <- Socket.accept sock
 (HostEntry peer _ _ _) <- ((getHostByAddr AF_INET haddr)
`Control.Exception.catch` (\_ ->
   return (HostEntry ((showHex ((haddr `shiftR` 24) .&. 0xff) . showChar '.'
. showHex ((haddr `shiftR` 16) .&. 0xff)
      . showChar '.' . showHex ((haddr `shiftR` 8) .&. 0xff) . showChar '.'
. showHex (haddr .&. 0xff)) "") [] AF_INET [])))
 handle             <- socketToHandle sock' ReadWriteMode
 return (handle, peer, port)

	Regards,
	Keean Schupke.

-----Original Message-----
From: glasgow-haskell-users-admin@haskell.org
[mailto:glasgow-haskell-users-admin@haskell.org]On Behalf Of Simon
Marlow
Sent: 09 January 2003 13:18
To: Keean Schupke; glasgow-haskell-users@haskell.org
Subject: RE: readping fd's and flushing buffers


> This may be related to the answer just given to do with
> finalizing the
> Handles, as I have a
> problem with sockets hanging around after a host name resolution has
> failed (using the simple socket
> library). Having looked at the code I would like to suggest the
> following change:
>
> connectTo hostname (PortNumber port) = do
>     proto   <- getProtocolNumber "tcp"
>     sock        <- socket AF_INET Stream proto
>     he     <- getHostByName hostname
>     connect sock (SockAddrInet port (hostAddress he))
>     socketToHandle sock ReadWriteMode
>
> Should become:
>
> connectTo hostname (PortNumber port) = do
>     proto   <- getProtocolNumber "tcp"
>     sock        <- socket AF_INET Stream proto
>     (do
>         he     <- getHostByName hostname
>         connect sock (SockAddrInet port (hostAddress he))
>         socketToHandle sock ReadWriteMode) `Exception.catch`
> (\e -> do
> sClose sock;throw e)
>
> Is this a sensible change to make?

Yes, well spotted.  I'll add the exception handler.

Cheers,
	Simon
_______________________________________________
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users