Up

Module Socket

Signature

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

bind socket addr sets close_on_exec for the fd of socket.

val listen : ?backlog:int -> ([
| `Bound
], 'addr) t -> ([
| `Passive
], 'addr) t
val accept : ([
| `Passive
], 'addr) t -> [
| `Ok of ([
| `Active
], 'addr) t * 'addr
| `Socket_closed
] Async_unix.Import.Deferred.t
val 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.t
val shutdown : ('a, 'addr) t -> [
| `Receive
| `Send
| `Both
] -> unit
val fd : ('a, 'addr) t -> Async_unix.Fd.t
val of_fd : Async_unix.Fd.t -> 'addr Type.t -> ('a, 'addr) t
val getsockname : ('a, 'addr) t -> 'addr
val getpeername : ('a, 'addr) t -> 'addr
module Opt : sig .. end
val getopt : ('a, 'addr) t -> 'c Opt.t -> 'c
val setopt : ('a, 'addr) t -> 'c Opt.t -> 'c -> unit
val mcast_join : ?ifname:string -> ?source:Inet_addr.t -> ('a, 'addr) t -> 'addr -> unit
val mcast_leave : ?ifname:string -> ('a, 'addr) t -> 'addr -> unit
val bind_to_interface_exn : (([
| `Unconnected
], _) t -> [
| `Any
| `Interface_name of string
] -> unit) Core.Std.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.