Copyright | (c) The University of Glasgow 2001 |
---|---|
License | BSD-style (see the file libraries/network/LICENSE) |
Maintainer | libraries@haskell.org |
Stability | provisional |
Portability | portable |
Safe Haskell | None |
Language | Haskell98 |
This module is kept for backwards-compatibility. New users are encouraged to use Network.Socket instead.
Network was intended as a "higher-level" interface to networking facilities, and only supports TCP.
- data Socket
- data PortID
- type HostName = String
- data PortNumber
- withSocketsDo :: IO a -> IO a
- listenOn :: PortID -> IO Socket
- accept :: Socket -> IO (Handle, HostName, PortNumber)
- sClose :: Socket -> IO ()
- connectTo :: HostName -> PortID -> IO Handle
- sendTo :: HostName -> PortID -> String -> IO ()
- recvFrom :: HostName -> PortID -> IO String
- socketPort :: Socket -> IO PortID
Basic data types
Represents a socket. The fields are, respectively:
- File descriptor
- Socket family
- Socket type
- Protocol number
- Status flag
If you are calling the MkSocket
constructor directly you should ensure
you have called withSocketsDo
and that the file descriptor is
in non-blocking mode. See setNonBlockIfNeeded
.
Socket
s are not GCed unless they are closed by close
.
type HostName = String Source #
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"
.
data PortNumber Source #
Use the Num
instance (i.e. use a literal) to create a
PortNumber
value with the correct network-byte-ordering. You
should not use the PortNum constructor. It will be removed in the
next release.
>>>
1 :: PortNumber
1>>>
read "1" :: PortNumber
1
Initialisation
withSocketsDo :: IO a -> IO a Source #
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).
Server-side connections
Creates the server side socket which has been bound to the specified port.
maxListenQueue
(typically 128) is specified to the listen queue.
This is good enough for normal network servers but is too small
for high performance servers.
To avoid the "Address already in use" problems,
the ReuseAddr
socket option is set on the listening socket.
If available, the IPv6Only
socket option is set to 0
so that both IPv4 and IPv6 can be accepted with this socket.
If you don't like the behavior above, please use the lower level
listen
instead.
:: Socket | Listening Socket |
-> IO (Handle, HostName, PortNumber) | Triple of: read/write |
Accept a connection on a socket created by listenOn
. Normal
I/O operations (see System.IO) can be used on the Handle
returned to communicate with the client.
Notice that although you can pass any Socket to Network.accept,
only sockets of either AF_UNIX, AF_INET, or AF_INET6 will work
(this shouldn't be a problem, though). When using AF_UNIX, HostName
will be set to the path of the socket and PortNumber to -1.
sClose :: Socket -> IO () Source #
Close the socket. Sending data to or receiving data from closed socket may lead to undefined behaviour.
Client-side connections
Simple sending and receiving
Send and receive data from/to the given host and port number. These
should normally only be used where the socket will not be required for
further calls. Also, note that due to the use of hGetContents
in recvFrom
the socket will remain open (i.e. not available) even if the function already
returned. Their use is strongly discouraged except for small test-applications
or invocations from the command line.
Miscellaneous
Networking Issues
Buffering
The Handle
returned by connectTo
and accept
is NoBuffering
by
default. For an interactive application you may want to set the
buffering mode on the Handle
to
LineBuffering
or BlockBuffering
, like so:
h <- connectTo host port hSetBuffering h LineBuffering