module Cf_socket:Extended network sockets interface.sig
..end
The Objective Caml Unix
library contains a simplified interface for
manipulating network sockets that is sufficient for most applications.
This module and its cognates implement an alternative interface for using
network sockets that offers the same extensibility as the Berkeley sockets
interface does in the C language, while using the Objective Caml type
system to enforce usage constraints.
type 'a
socktype
type 'a
domain
type
protocol
type 'a
sockaddr
struct sockaddr_xxx
structure. The type parameter
is a shadow type attributed to the address family identifier value.type ('a, -'b, -'c)
sockopt
type ('a, 'b)
t
type
msg_flags = {
|
msg_oob : |
(* | Message is out-of-band/expedited. | *) |
|
msg_peek : |
(* | Read message without dequeue. | *) |
|
msg_dontroute : |
(* | Use direct interface. | *) |
|
msg_eor : |
(* | End of record. | *) |
|
msg_trunc : |
(* | Message truncated in receive. | *) |
|
msg_ctrunc : |
(* | Control message truncated in receive. | *) |
|
msg_waitall : |
(* | Block until message completely received. | *) |
|
msg_dontwait : |
(* | Don't block. | *) |
send
and recv
functions (and their cognates).module type AF =sig
..end
module type ST =sig
..end
module type P =sig
..end
module SOCK_STREAM:ST
with type tag = [ `SOCK_STREAM ]
SOCK_STREAM
socket type.
module SOCK_DGRAM:ST
with type tag = [ `SOCK_DGRAM ]
val msg_flags_none : msg_flags
Most of the functions here are fairly straightforward wrappers around the
Berkeley sockets API. For a more convenient interface, consider using the
object-oriented alternative, described in the next section.
val create : 'a domain ->
'b socktype -> protocol -> ('a, 'b) t
create dom st p
to create a new socket descriptor with the socket
domain dom
, the socket type st
and the protocol identifier p
. Raises
Unix.Error
if a system error occurs.val createpair : 'a domain ->
'b socktype ->
protocol -> ('a, 'b) t * ('a, 'b) t
createpair dom st p
to create a pair of new socket descriptors
that are already bound and connected to one another, using the socket
domain dom
, the socket type st
and the protocol identifier p
. Raises
Unix.Error
if a system error occurs.val to_unix_file_descr : ('a, 'b) t -> Unix.file_descr
to_unix_file_descr sock
to obtain the file descriptor to use with
functions in the Unix
library that corresponds to the socket descriptor
sock
.val domain_of_sockaddr : 'a sockaddr -> 'a domain
domain_of_sockaddr sa
to obtain the socket domain identifier
associated with a socket address of unknown address family.val dup : ('a, 'b) t -> ('a, 'b) t
dup sock
to create a duplicate of socket descriptor sock
. Raises
Unix.Error
if there is an error.val dup2 : ('a, 'b) t -> ('a, 'b) t -> unit
dup2 sock sock2
to create a duplicate of socket descriptor sock
by
overwriting the descriptor sock2
. Raises Unix.Error
if there is an
error.val getsockname : ('a, 'b) t -> 'a sockaddr
getsockname sock
to create a new socket address corresponding to the
local bound endpoint of the socket sock
. Raises Unix.Error
if there is
an error.val getpeername : ('a, 'b) t -> 'a sockaddr
getpeername sock
to create a new socket address corresponding to the
connected remote endpoint of the socket sock
. Raises Unix.Error
if
there is an error.val bind : ('a, 'b) t -> 'a sockaddr -> unit
bind sock sa
to bind the local endpoint address of the socket sock
to the socket address sa
. Raises Unix.Error
if there is an error.val connect : ('a, 'b) t -> 'a sockaddr -> unit
connect sock sa
to connect the remote endpoint address of the socket
sock
to the socket address sa
. Raises Unix.Error
if there is an
error.val listen : ('a, [< `SOCK_SEQPACKET | `SOCK_STREAM ]) t -> int -> unit
listen sock n
to set the socket sock
into the mode of listening for
connections with a backlog queue n
spaces deep. Raises Unix.Error
if
there is an error.val accept : ('a, [ `SOCK_STREAM ]) t ->
('a, [ `SOCK_STREAM ]) t * 'a sockaddr
accept sock
to accept a connected request on the listening socket
sock
. Returns a new socket descriptor and the socket address of the
remote peer. Raises Unix.Error
if there is an error.val shutdown : ('a, 'b) t -> Unix.shutdown_command -> unit
shutdown sock cmd
to shutdown either sending or receiving (or both)
on the socket sock
. Raises Unix.Error
if there is an error.val close : ('a, 'b) t -> unit
close sock
to close a socket descriptor. Raises Unix.Error
if
there is an error.val send : ('a, 'b) t -> string -> int -> int -> msg_flags -> int
send sock buf pos len flags
to send len
octets from the string
buf
starting at position pos
on the socket sock
with the flags
indicated by flags
. Returns the number of octets actually sent. Raises
Unix.Error
if there is an error. Raises Invalid_argument
if pos
and
len
do not correspond to a valid substring of buf
.val sendto : ('a, [ `SOCK_DGRAM ]) t ->
string -> int -> int -> msg_flags -> 'a sockaddr -> int
sendto sock buf pos len flags sa
to send len
octets from the string
buf
starting at position pos
on the socket sock
with the flags
indicated by flags
to the socket address sa
. Returns the number of
octets actually sent. Raises Unix.Error
if there is an error. Raises
Invalid_argument
if pos
and len
do not correspond to a valid
substring of buf
.val recv : ('a, 'b) t -> string -> int -> int -> msg_flags -> int
recv sock buf pos len flags
to receive len
octets into the string
buf
starting at position pos
on the socket sock
with the flags
indicated by flags
. Returns the number of octets actually received.
Raises Unix.Error
if there is an error. Raises Invalid_argument
if
pos
and len
do not correspond to a valid substring of buf
.val recvfrom : ('a, [ `SOCK_DGRAM ]) t ->
string -> int -> int -> msg_flags -> int * 'a sockaddr
recvfrom sock buf pos len flags
to receive len
octets into the
string buf
starting at position pos
on the socket sock
with the flags
indicated by flags
. Returns the number of octets actually received and
the socket address of the remote endpoint that sent them. Raises
Unix.Error
if there is an error. Raises Invalid_argument
if pos
and
len
do not correspond to a valid substring of buf
.val getsockopt : ('a, 'b) t -> ('c, 'a, 'b) sockopt -> 'c
getsockopt sock opt
to obtain the value associated with the socket
option opt
for the socket descriptor sock
. Raises Unix.Error
if
there is an error.val setsockopt : ('a, 'b) t -> ('c, 'a, 'b) sockopt -> 'c -> unit
setsockopt sock opt v
to set the value associated with the socket
option opt
for the socket descriptor sock
to the value v
. Raises
Unix.Error
if there is an error.
The following socket options are available on sockets of all socket types
and address/protocol families.
val so_debug : (bool, 'a, 'b) sockopt
val so_reuseaddr : (bool, 'a, 'b) sockopt
val so_reuseport : (bool, 'a, 'b) sockopt
val so_keepalive : (bool, 'a, 'b) sockopt
val so_dontroute : (bool, 'a, 'b) sockopt
val so_linger : (int option, 'a, 'b) sockopt
val so_broadcast : (bool, 'a, 'b) sockopt
val so_oobinline : (bool, 'a, 'b) sockopt
val so_sndbuf : (int, 'a, 'b) sockopt
val so_rcvbuf : (int, 'a, 'b) sockopt
val so_sndlowat : (int, 'a, 'b) sockopt
val so_rcvlowat : (int, 'a, 'b) sockopt
val so_sndtimeo : (float, 'a, 'b) sockopt
val so_rcvtimeo : (float, 'a, 'b) sockopt
val so_error : (unit, 'a, 'b) sockopt
val so_nosigpipe : (bool, 'a, 'b) sockopt
Unix.Error Unix.EPIPE
.