Module Async_unix.Unix_syscalls.Socket

module Address : sig ... end
module Family : sig ... end
type (+'a, 'b) t constraint 'a = [< `Unconnected | `Bound | `Passive | `Active ] and '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
      
include sig ... end
val sexp_of_t : ('a ‑> Sexplib.Sexp.t) ‑> ('b ‑> Sexplib.Sexp.t) ‑> ('a'bt ‑> Sexplib.Sexp.t
module Type : sig ... end
val create : 'addr Type.t ‑> ([ `Unconnected ], 'addrt
val connect : ([< `Unconnected | `Bound ], 'addrt ‑> 'addr ‑> ([ `Active ], 'addrt Import.Deferred.t
val connect_interruptible : ([< `Unconnected | `Bound ], 'addrt ‑> 'addr ‑> interrupt:unit Import.Deferred.t ‑> [ `Ok of ([ `Active ], 'addrt | `Interrupted ] Import.Deferred.t
val bind : ?reuseaddr:bool ‑> ([ `Unconnected ], 'addrt ‑> 'addr ‑> ([ `Bound ], 'addrt Import.Deferred.t

bind socket addr sets close_on_exec for the fd of socket.

val listen : ?backlog:int ‑> ([ `Bound ], 'addrt ‑> ([ `Passive ], 'addrt
val accept : ([ `Passive ], 'addrt ‑> [ `Ok of ([ `Active ], 'addrt * 'addr | `Socket_closed ] Import.Deferred.t
val accept_interruptible : ([ `Passive ], 'addrt ‑> interrupt:unit Import.Deferred.t ‑> [ `Ok of ([ `Active ], 'addrt * 'addr | `Socket_closed | `Interrupted ] Import.Deferred.t
val accept_at_most : ([ `Passive ], 'addrt ‑> limit:int ‑> [ `Ok of (([ `Active ], 'addrt * 'addr) list | `Socket_closed ] Import.Deferred.t

accept_at_most is like accept, but will return up to limit connections before yielding, where limit >= 1. accept_at_most first waits for one connection and then attempts to retrieve up to limit connections through non-blocking Unix.accept calls. If a call to Unix.accept would block before limit is reached, accept_at_most returns 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 limit will 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 ], 'addrt ‑> limit:int ‑> interrupt:unit Import.Deferred.t ‑> [ `Ok of (([ `Active ], 'addrt * 'addr) list | `Socket_closed | `Interrupted ] Import.Deferred.t
val shutdown : ('a'addrt ‑> [ `Receive | `Send | `Both ] ‑> unit
val fd : ('a'addrt ‑> Fd.t
val of_fd : Fd.t ‑> 'addr Type.t ‑> ('a'addrt
val getsockname : ('a'addrt ‑> 'addr
val getpeername : ('a'addrt ‑> 'addr
module Opt : sig ... end
val getopt : ('a'addrt ‑> 'c Opt.t ‑> 'c
val setopt : ('a'addrt ‑> 'c Opt.t ‑> 'c ‑> unit
val mcast_join : ?ifname:string ‑> ?source:Inet_addr.t ‑> ('a'addrt ‑> 'addr ‑> unit
val mcast_leave : ?ifname:string ‑> ('a'addrt ‑> 'addr ‑> unit
val bind_to_interface_exn : (([ `Unconnected ], _t ‑> [ `Any | `Interface_name of string ] ‑> unit) Core.Or_error.t

bind_to_interface_exn t (`Interface_name "eth0") restricts messages from being received or sent on interfaces other than eth0. See Linux_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.