-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Simple network sockets usage patterns. -- -- This module exports functions that abstract simple network socket -- usage patterns. -- -- See the changelog.md file in the source distribution to learn -- about any important changes between version. @package network-simple @version 0.4 -- | This module exports functions that abstract simple TCP Socket -- usage patterns. -- -- This module uses MonadIO and MonadMask extensively so -- that you can reuse these functions in monads other than IO. -- However, if you don't care about any of that, just pretend you are -- using the IO monad all the time and everything will work as -- expected. module Network.Simple.TCP -- | Connect to a TCP server and use the connection. -- -- The connection socket is closed when done or in case of exceptions. -- -- If you prefer to acquire and close the socket yourself, then use -- connectSock and closeSock. connect :: (MonadIO m, MonadMask m) => HostName -> ServiceName -> ((Socket, SockAddr) -> m r) -> m r -- | Start a TCP server that accepts incoming connections and handles them -- concurrently in different threads. -- -- Any acquired network resources are properly closed and discarded when -- done or in case of exceptions. -- -- Note: This function performs listen and acceptFork, so -- you don't need to perform those manually. serve :: MonadIO m => HostPreference -> ServiceName -> ((Socket, SockAddr) -> IO ()) -> m () -- | Bind a TCP listening socket and use it. -- -- The listening socket is closed when done or in case of exceptions. -- -- If you prefer to acquire and close the socket yourself, then use -- bindSock, closeSock and the listen function from -- Network.Socket instead. -- -- Note: maxListenQueue is tipically 128, which is too small for -- high performance servers. So, we use the maximum between -- maxListenQueue and 2048 as the default size of the listening -- queue. The NoDelay and ReuseAddr options are set on the -- socket. listen :: (MonadIO m, MonadMask m) => HostPreference -> ServiceName -> ((Socket, SockAddr) -> m r) -> m r -- | Accept a single incoming connection and use it. -- -- The connection socket is closed when done or in case of exceptions. accept :: (MonadIO m, MonadMask m) => Socket -> ((Socket, SockAddr) -> m r) -> m r -- | Accept a single incoming connection and use it in a different thread. -- -- The connection socket is closed when done or in case of exceptions. acceptFork :: MonadIO m => Socket -> ((Socket, SockAddr) -> IO ()) -> m ThreadId -- | Read up to a limited number of bytes from a socket. -- -- Returns Nothing if the remote end closed the connection or -- end-of-input was reached. The number of returned bytes might be less -- than the specified limit. recv :: MonadIO m => Socket -> Int -> m (Maybe ByteString) -- | Writes the given bytes to the socket. send :: MonadIO m => Socket -> ByteString -> m () -- | Obtain a Socket bound to the given host name and TCP service -- port. -- -- The obtained Socket should be closed manually using -- closeSock when it's not needed anymore. -- -- Prefer to use listen if you will be listening on this socket -- and using it within a limited scope, and would like it to be closed -- immediately after its usage or in case of exceptions. bindSock :: MonadIO m => HostPreference -> ServiceName -> m (Socket, SockAddr) -- | Obtain a Socket connected to the given host and TCP service -- port. -- -- The obtained Socket should be closed manually using -- closeSock when it's not needed anymore, otherwise you risk -- having the socket open for much longer than needed. -- -- Prefer to use connect if you will be using the socket within a -- limited scope and would like it to be closed immediately after its -- usage or in case of exceptions. connectSock :: MonadIO m => HostName -> ServiceName -> m (Socket, SockAddr) -- | Close the Socket. closeSock :: MonadIO m => Socket -> m () -- | On Windows operating systems, the networking subsystem has to be -- initialised using withSocketsDo before any networking -- operations can be used. eg. -- --
-- main = withSocketsDo $ do {...}
--
--
-- Although this is only strictly necessary on Windows platforms, it is
-- harmless on other platforms, so for portability it is good practice to
-- use it all the time.
withSocketsDo :: IO a -> IO a
-- | Preferred host to bind.
data HostPreference
-- | Any available host.
HostAny :: HostPreference
-- | Any available IPv4 host.
HostIPv4 :: HostPreference
-- | Any available IPv6 host.
HostIPv6 :: HostPreference
-- | An explicit host name.
Host :: HostName -> HostPreference
-- | Either a host name e.g., "haskell.org" or a numeric host
-- address string consisting of a dotted decimal IPv4 address or an IPv6
-- address e.g., "192.168.0.1".
type HostName = String
type ServiceName = String
data Socket :: *
data SockAddr :: *