tcp-streams-0.5.0.0: One stop solution for tcp client and server with tls support.

Safe HaskellNone
LanguageHaskell2010

System.IO.Streams.TCP

Contents

Description

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

Synopsis

tcp client

connectSocket Source #

Arguments

:: HostName

hostname to connect to

-> PortNumber

port number to connect to

-> IO Socket 

Convenience function for initiating an raw TCP connection to the given (HostName, PortNumber) combination.

Note that sending an end-of-file to the returned OutputStream will not close the underlying Socket connection.

connect Source #

Arguments

:: HostName

hostname to connect to

-> PortNumber

port number to connect to

-> IO (InputStream ByteString, OutputStream ByteString, Socket) 

Connect to remote tcp server.

You may need to use bracket pattern to enusre Socket 's safety.

connectWithBufferSize Source #

Arguments

:: 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.

withConnection Source #

Arguments

:: 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 (HostName, PortNumber) combination. The socket will be closed and deleted after the user handler runs.

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 #

Arguments

:: Int

how large the receive buffer should be

-> Socket

network socket

-> IO (InputStream ByteString, OutputStream ByteString) 

Convert a Socket into a streams pair, catch IOExceptions 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.

close :: Socket -> IO () #

Close the socket. Sending data to or receiving data from closed socket may lead to undefined behaviour.