Eio.NetSourceNetworking.
Example:
let addr = `Tcp (Ipaddr.V4.loopback, 8080)
let http_get ~net ~stdout addr =
Switch.run @@ fun sw ->
let flow = Net.connect ~sw net addr in
Flow.copy_string "GET / HTTP/1.0\r\n\r\n" flow;
Flow.shutdown flow `Send;
Flow.copy flow stdoutconnect ~sw t addr is a new socket connected to remote address addr.
The new socket will be closed when sw finishes, unless closed manually first.
val with_tcp_connect :
?timeout:Time.Timeout.t ->
host:string ->
service:string ->
t ->
(< stream_socket ; Flow.close > -> 'b) ->
'bwith_tcp_connect ~host ~service t f creates a tcp connection conn to host and service and executes f conn.
conn is closed after f returns (if it isn't already closed by then).
host is either an IP address or a domain name, eg. "www.example.org", "www.ocaml.org" or "127.0.0.1".
service is an IANA recognized service name or port number, eg. "http", "ftp", "8080" etc. See https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml.
Addresses are tried in the order they are returned by getaddrinfo, until one succeeds.
val listen :
?reuse_addr:bool ->
?reuse_port:bool ->
backlog:int ->
sw:Switch.t ->
t ->
Sockaddr.stream ->
listening_socketlisten ~sw ~backlog t addr is a new listening socket bound to local address addr.
The new socket will be closed when sw finishes, unless closed manually first.
For (non-abstract) Unix domain sockets, the path will be removed afterwards.
val accept :
sw:Switch.t ->
listening_socket ->
< stream_socket ; Flow.close > * Sockaddr.streamaccept ~sw socket waits until a new connection is ready on socket and returns it.
The new socket will be closed automatically when sw finishes, if not closed earlier. If you want to handle multiple connections, consider using accept_fork instead.
val accept_fork :
sw:Switch.t ->
listening_socket ->
on_error:(exn -> unit) ->
(stream_socket -> Sockaddr.stream -> unit) ->
unitaccept_fork socket fn accepts a connection and handles it in a new fiber.
After accepting a connection to socket, it runs fn flow client_addr in a new fiber.
flow will be closed when fn returns.
val accept_sub :
sw:Switch.t ->
listening_socket ->
on_error:(exn -> unit) ->
(sw:Switch.t -> stream_socket -> Sockaddr.stream -> unit) ->
unitval datagram_socket :
sw:Switch.t ->
t ->
Sockaddr.datagram ->
< datagram_socket
; Flow.close >datagram_socket ~sw t addr creates a new datagram socket that data can be sent to and received from. The new socket will be closed when sw finishes.
send sock addr buf sends the data in buf to the address addr using the the datagram socket sock.
recv sock buf receives data from the socket sock putting it in buf. The number of bytes received is returned along with the sender address and port. If the buf is too small then excess bytes may be discarded depending on the type of the socket the message is received from.
getaddrinfo ?service t node returns a list of IP addresses for node. node is either a domain name or an IP address.
For a more thorough treatment, see getaddrinfo.
getaddrinfo_stream is like getaddrinfo, but filters out non-stream protocols.
getaddrinfo_datagram is like getaddrinfo, but filters out non-datagram protocols.
getnameinfo t sockaddr is (hostname, service) corresponding to sockaddr. hostname is the registered domain name represented by sockaddr. service is the IANA specified textual name of the port specified in sockaddr, e.g. 'ftp', 'http', 'https', etc.
Closing
close t marks the socket as closed. It can no longer be used after this.