Module Async_unix__Unix_syscalls.Socket
module Address : sig ... endmodule Family : sig ... endtype (+'a, 'b) tconstraint 'a = [< `Unconnected | `Bound | `Passive | `Active ] constraint 'b = [< Address.t ]Sockets have a phantom type parameter that tracks the state of the socket in order to eliminate certain errors in which socket functions are called in the wrong order. Initially, a socket is
`Unconnected. As various socket functions are called, they return a socket with a new phantom state. Here is a chart of the allowed state transitions.Unconnected ---connect--> Active | | ---bind--> Bound ---listen--> Passive ---accept---> Active | | ---connect--> Active
val sexp_of_t : ('a -> Ppx_sexp_conv_lib.Sexp.t) -> ('b -> Ppx_sexp_conv_lib.Sexp.t) -> ('a, 'b) t -> Ppx_sexp_conv_lib.Sexp.t
module Type : sig ... endval create : 'addr Type.t -> ([ `Unconnected ], 'addr) tval connect : ([< `Unconnected | `Bound ], 'addr) t -> 'addr -> ([ `Active ], 'addr) t Async_unix__.Import.Deferred.tval connect_interruptible : ([< `Unconnected | `Bound ], 'addr) t -> 'addr -> interrupt:unit Async_unix__.Import.Deferred.t -> [ `Ok of ([ `Active ], 'addr) t | `Interrupted ] Async_unix__.Import.Deferred.tval bind : ?reuseaddr:bool -> ([ `Unconnected ], 'addr) t -> 'addr -> ([ `Bound ], 'addr) t Async_unix__.Import.Deferred.tbind socket addrsets close_on_exec for the fd ofsocket.
val bind_inet : ?reuseaddr:bool -> ([ `Unconnected ], Address.Inet.t) t -> Address.Inet.t -> ([ `Bound ], Address.Inet.t) tbind_inet socket addris just likebindbut is restricted toInet.taddresses and is therefore guaranteed not to block.
val listen : ?backlog:int -> ([ `Bound ], 'addr) t -> ([ `Passive ], 'addr) tval accept : ([ `Passive ], 'addr) t -> [ `Ok of ([ `Active ], 'addr) t * 'addr | `Socket_closed ] Async_unix__.Import.Deferred.tval accept_interruptible : ([ `Passive ], 'addr) t -> interrupt:unit Async_unix__.Import.Deferred.t -> [ `Ok of ([ `Active ], 'addr) t * 'addr | `Socket_closed | `Interrupted ] Async_unix__.Import.Deferred.tval accept_at_most : ([ `Passive ], 'addr) t -> limit:int -> [ `Ok of (([ `Active ], 'addr) t * 'addr) list | `Socket_closed ] Async_unix__.Import.Deferred.taccept_at_mostis likeaccept, but will return up tolimitconnections before yielding, wherelimit >= 1.accept_at_mostfirst waits for one connection and then attempts to retrieve up tolimitconnections through non-blockingUnix.accept calls. If a call toUnix.accept would block beforelimitis reached,accept_at_mostreturns the connections retrieved thus far.Servers that must service a large number of connections tend to observe a stall in connection accept rates when under heavy load. Increasing
limitwill ameliorate this effect, and increase accept rates and overall throughput of the server at the cost of increased contention for resources amongst connections.For details, see:
Acceptable strategies for improving web server performance Brecht, Pariag, and Gammo. USENIX ATEC '04
val accept_at_most_interruptible : ([ `Passive ], 'addr) t -> limit:int -> interrupt:unit Async_unix__.Import.Deferred.t -> [ `Ok of (([ `Active ], 'addr) t * 'addr) list | `Socket_closed | `Interrupted ] Async_unix__.Import.Deferred.tval shutdown : ('a, 'addr) t -> [ `Receive | `Send | `Both ] -> unitval fd : ('a, 'addr) t -> Async_unix.Fd.tval of_fd : Async_unix.Fd.t -> 'addr Type.t -> ('a, 'addr) tval getsockname : ('a, 'addr) t -> 'addrval getpeername : ('a, 'addr) t -> 'addr
module Opt : sig ... endval getopt : ('a, 'addr) t -> 'c Opt.t -> 'cval setopt : ('a, 'addr) t -> 'c Opt.t -> 'c -> unitval mcast_join : ?ifname:string -> ?source:Inet_addr.t -> ('a, 'addr) t -> 'addr -> unitval mcast_leave : ?ifname:string -> ?source:Inet_addr.t -> ('a, 'addr) t -> 'addr -> unitval bind_to_interface_exn : (([ `Unconnected ], Address.t) t -> Linux_ext.Bound_to_interface.t -> unit) Core.Or_error.tbind_to_interface_exn t (`Interface_name "eth0")restricts messages from being received or sent on interfaces other thaneth0. SeeLinux_ext.bind_to_interface.Typically, one would use this function for very specific non-multicast requirements. For similar functionality when using multicast, see
Core_unix.mcast_set_ifname.