Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides convenience functions for interfacing io-streams
with raw tcp. the default receive buffer size is 4096. sending is unbuffered,
anything write into OutputStream
will be immediately send to underlying socket.
Reading InputStream
will block until GHC IO manager find data is ready,
for example 'System.IO.Streams.ByteString.readExactly 1024' will block until 1024 bytes are available.
When socket is closed, the InputStream
will be closed too(further reading will return Nothing
),
no exception will be thrown. You still should handle IOError
when you write to OutputStream
for safety,
but no exception doesn't essentially mean a successful write, especially under bad network
environment(broken wire for example).
TCP_NODELAY
are enabled by default. you can use setSocketOption
to adjust.
This module is intended to be imported qualified
, e.g.:
import qualified System.IO.Streams.TCP as TCP
- connectSocket :: HostName -> PortNumber -> IO Socket
- connect :: HostName -> PortNumber -> IO (InputStream ByteString, OutputStream ByteString, Socket)
- connectWithBufferSize :: HostName -> PortNumber -> Int -> IO (InputStream ByteString, OutputStream ByteString, Socket)
- withConnection :: HostName -> PortNumber -> (InputStream ByteString -> OutputStream ByteString -> Socket -> IO a) -> IO a
- bindAndListen :: PortNumber -> Int -> IO Socket
- accept :: Socket -> IO (InputStream ByteString, OutputStream ByteString, Socket, SockAddr)
- acceptWithBufferSize :: Socket -> Int -> IO (InputStream ByteString, OutputStream ByteString, Socket, SockAddr)
- socketToStreamsWithBufferSize :: Int -> Socket -> IO (InputStream ByteString, OutputStream ByteString)
- close :: Socket -> IO ()
tcp client
:: HostName | hostname to connect to |
-> PortNumber | port number to connect to |
-> IO Socket |
Convenience function for initiating an raw TCP connection to the given
(
combination.HostName
, PortNumber
)
Note that sending an end-of-file to the returned OutputStream
will not
close the underlying Socket connection.
:: HostName | hostname to connect to |
-> PortNumber | port number to connect to |
-> IO (InputStream ByteString, OutputStream ByteString, Socket) |
connectWithBufferSize Source #
:: HostName | hostname to connect to |
-> PortNumber | port number to connect to |
-> Int | tcp read buffer size |
-> IO (InputStream ByteString, OutputStream ByteString, Socket) |
Connect to remote tcp server with adjustable receive buffer size.
:: HostName | hostname to connect to |
-> PortNumber | port number to connect to |
-> (InputStream ByteString -> OutputStream ByteString -> Socket -> IO a) | Action to run with the new connection |
-> IO a |
Convenience function for initiating an TCP connection to the given
(
combination. The socket will be
closed and deleted after the user handler runs.HostName
, PortNumber
)
tcp server
bindAndListen :: PortNumber -> Int -> IO Socket Source #
Bind and listen on port with a limit on connection count.
accept :: Socket -> IO (InputStream ByteString, OutputStream ByteString, Socket, SockAddr) Source #
Accept a new connection from remote client, return a InputStream
/ OutputStream
pair,
a new underlying Socket
, and remote SockAddr
,you should call bindAndListen
first.
This function will block if there's no connection comming.
acceptWithBufferSize :: Socket -> Int -> IO (InputStream ByteString, OutputStream ByteString, Socket, SockAddr) Source #
accept a connection with adjustable receive buffer size.
helpers
socketToStreamsWithBufferSize Source #
:: Int | how large the receive buffer should be |
-> Socket | network socket |
-> IO (InputStream ByteString, OutputStream ByteString) |
Convert a Socket
into a streams pair, catch IOException
s on receiving and close InputStream
.
You still should handle IOError
when you write to OutputStream
for safety,
but no exception doesn't essentially mean a successful write, especially under bad network
environment(broken wire for example).
During receiving, the status MVar
is locked, so that a cleanup thread
can't affect receiving until finish.