-- 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 versions. @package network-simple @version 0.4.2 -- | 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 shut down and 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 sockets are properly shut down and closed when done or in -- case of exceptions. Exceptions from the threads handling the -- individual connections won't cause serve to die. -- -- Note: This function performs listen, acceptFork, so -- don't perform those manually. serve :: MonadIO m => HostPreference -> ServiceName -> ((Socket, SockAddr) -> IO ()) -> m a -- | 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 and closeSock, as well as listenSock -- function. -- -- Note: The NoDelay and ReuseAddr options are set on the -- socket. The maximum number of incoming queued connections is 2048. 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 shut down and 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 shut down and 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, but it will never null. recv :: MonadIO m => Socket -> Int -> m (Maybe ByteString) -- | Writes a ByteString to the socket. -- -- Note: On POSIX, calling sendLazy once is much more efficient -- than repeatedly calling send on strict ByteStrings. Use -- sendLazy sock . fromChunks if you have more -- than one strict ByteString to send. send :: MonadIO m => Socket -> ByteString -> m () -- | Writes a lazy ByteString to the socket. -- -- Note: This uses writev(2) on POSIX. sendLazy :: MonadIO m => Socket -> ByteString -> m () -- | Writes the given list of ByteStrings to the socket. -- -- Note: This uses writev(2) on POSIX. -- | Deprecated: Use sendLazy sock . -- fromChunks sendMany :: 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. -- -- Note: The NoDelay and ReuseAddr options are set on the -- socket. bindSock :: MonadIO m => HostPreference -> ServiceName -> m (Socket, SockAddr) -- | Listen for new connections of the given bound socket. listenSock :: MonadIO m => Socket -> Int -> m () -- | 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 connection and 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) -- | Shuts down and closes the Socket, silently ignoring any -- synchronous exception that might happen. closeSock :: MonadIO m => Socket -> m () -- | With older versions of the network library (version 2.6.0.2 -- or earlier) on Windows operating systems, the networking subsystem -- must be initialised using withSocketsDo before any networking -- operations can be used. eg. -- --
--   main = withSocketsDo $ do {...}
--   
-- -- It is fine to nest calls to withSocketsDo, and to perform -- networking operations after withSocketsDo has returned. -- -- In newer versions of the network library (version v2.6.1.0 or -- later) it is only necessary to call withSocketsDo if you are -- calling the MkSocket constructor directly. However, for -- compatibility with older versions on Windows, it is good practice to -- always call withSocketsDo (it's very cheap). 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 -- | A socket data type. Sockets are not GCed unless they are closed -- by close. data Socket -- | The existence of a constructor does not necessarily imply that that -- socket address type is supported on your system: see -- isSupportedSockAddr. data SockAddr