The Network.Socket module is for when you want full control over
sockets. Essentially the entire C socket API is exposed through
this module; in general the operations follow the behaviour of the C
functions of the same name (consult your favourite Unix networking book).

A higher level interface to networking operations is provided
through the module Network.

Resolve a host or service name to one or more addresses.
The AddrInfo values that this function returns contain SockAddr
values that you can pass directly to connect or
bindSocket.

This function is protocol independent. It can return both IPv4 and
IPv6 address information.

The AddrInfo argument specifies the preferred query behaviour,
socket options, or protocol. You can override these conveniently
using Haskell's record update syntax on defaultHints, for example
as follows:

myHints = defaultHints { addrFlags = [AI_ADDRCONFIG, AI_CANONNAME] }

Values for addrFlags control query behaviour. The supported
flags are as follows:

The HostName argument must be a numeric
address in string form, and network name lookups will not be
attempted.

Note: Although the following flags are required by RFC 3493, they
may not have an effect on all platforms, because the underlying
network stack may not support them. To see whether a flag from the
list below will have any effect, call addrInfoFlagImplemented.

AI_NUMERICSERV

The ServiceName argument must be a port
number in string form, and service name lookups will not be
attempted.

AI_ADDRCONFIG

The list of returned AddrInfo values will
only contain IPv4 addresses if the local system has at least
one IPv4 interface configured, and likewise for IPv6.

AI_V4MAPPED

If an IPv6 lookup is performed, and no IPv6
addresses are found, IPv6-mapped IPv4 addresses will be
returned.

AI_ALL

If AI_ALL is specified, return all matching IPv6 and
IPv4 addresses. Otherwise, this flag has no effect.

You must provide a Just value for at least one of the HostName
or ServiceName arguments. HostName can be either a numeric
network address (dotted quad for IPv4, colon-separated hex for
IPv6) or a hostname. In the latter case, its addresses will be
looked up unless AI_NUMERICHOST is specified as a hint. If you
do not provide a HostName value and do not set AI_PASSIVE as
a hint, network addresses in the result will contain the address of
the loopback interface.

If the query fails, this function throws an IO exception instead of
returning an empty list. Otherwise, it returns a non-empty list
of AddrInfo values.

There are several reasons why a query might result in several
values. For example, the queried-for host could be multihomed, or
the service might be available via several protocols.

Note: the order of arguments is slightly different to that defined
for getaddrinfo in RFC 2553. The AddrInfo parameter comes first
to make partial application easier.

Resolve an address to a host or service name.
This function is protocol independent.

The list of NameInfoFlag values controls query behaviour. The
supported flags are as follows:

NI_NOFQDN

If a host is local, return only the
hostname part of the FQDN.

NI_NUMERICHOST

The name of the host is not
looked up. Instead, a numeric representation of the host's
address is returned. For an IPv4 address, this will be a
dotted-quad string. For IPv6, it will be colon-separated
hexadecimal.

NI_NUMERICSERV

The name of the service is not
looked up. Instead, a numeric representation of the
service is returned.

NI_NAMEREQD

If the hostname cannot be looked up, an IO error
is thrown.

NI_DGRAM

Resolve a datagram-based service name. This is
required only for the few protocols that have different port
numbers for their datagram-based versions than for their
stream-based versions.

Hostname and service name lookups can be expensive. You can
specify which lookups to perform via the two Bool arguments. If
one of these is False, the corresponding value in the returned
tuple will be Nothing, and no lookup will be performed.

If a host or service's name cannot be looked up, then the numeric
form of the address or service will be returned.

If the query fails, this function throws an IO exception.

Example:
(hostName, _) <- getNameInfo [] True False myAddress

Socket operations

Create a new socket using the given address family, socket type
and protocol number. The address family is usually AF_INET,
AF_INET6, or AF_UNIX. The socket type is usually Stream or
Datagram. The protocol number is usually defaultProtocol.
If AF_INET6 is used, the IPv6Only socket option is set to 0
so that both IPv4 and IPv6 can be handled with one socket.

Build a pair of connected socket objects using the given address
family, socket type, and protocol number. Address family, socket
type, and protocol number are as for the socket function above.
Availability: Unix.

Bind the socket to an address. The socket must not already be
bound. The Family passed to bindSocket must be the
same as that passed to socket. If the special port number
aNY_PORT is passed then the system assigns the next available
use port.

Accept a connection. The socket must be bound to an address and
listening for connections. The return value is a pair (conn,
address) where conn is a new socket object usable to send and
receive data on the connection, and address is the address bound
to the socket on the other end of the connection.

Sending and receiving data

Do not use the send and recv functions defined in this module
in new code, as they incorrectly represent binary data as a Unicode
string. As a result, these functions are inefficient and may lead
to bugs in the program. Instead use the send and recv
functions defined in the ByteString module.

Send data to the socket. The recipient can be specified
explicitly, so the socket need not be in a connected state.
Returns the number of bytes sent. Applications are responsible for
ensuring that all data has been sent.

Send data to the socket. The recipient can be specified
explicitly, so the socket need not be in a connected state.
Returns the number of bytes sent. Applications are responsible for
ensuring that all data has been sent.

Receive data from the socket. The socket need not be in a
connected state. Returns (bytes, nbytes, address) where bytes
is a String of length nbytes representing the data received and
address is a SockAddr representing the address of the sending
socket.

Receive data from the socket, writing it into buffer instead of
creating a new string. The socket need not be in a connected
state. Returns (nbytes, address) where nbytes is the number of
bytes received and address is a SockAddr representing the
address of the sending socket.

Receive data from the socket. The socket must be in a connected
state. This function may return fewer bytes than specified. If the
message is longer than the specified length, it may be discarded
depending on the type of socket. This function may block until a
message arrives.

Considering hardware and network realities, the maximum number of
bytes to receive should be a small power of 2, e.g., 4096.

For TCP sockets, a zero length return value means the peer has
closed its half side of the connection.

Shut down one or both halves of the connection, depending on the
second argument to the function. If the second argument is
ShutdownReceive, further receives are disallowed. If it is
ShutdownSend, further sends are disallowed. If it is
ShutdownBoth, further sends and receives are disallowed.