ocaml-core / base / async / examples / socket.ml

open Jane.Std
open Async.Std
module Socket = Unix.Socket

let printf = Print.printf

let port = 10000

let doit () =
  upon (Socket.bind (Socket.create Socket.Type.tcp)
           (Socket.Address.inet_addr_any ~port)) (fun s ->
    upon (Socket.accept (Socket.listen s)) (fun (s, _) ->
      let buf = String.create 1000 in
      let reader = Reader.create (Socket.fd s) in
      let rec loop bytes_read =
        upon (Reader.read reader buf) (function
          | `Eof -> printf "EOF\n"
          | `Ok n ->
              let bytes_read = bytes_read + n in
              printf "read %d bytes in total.\n" bytes_read;
              loop bytes_read)
      in
      loop 0));
  upon (Clock.after (sec 2.)) (fun () ->
    let s = Socket.create Socket.Type.tcp in
    upon (Socket.connect s (Socket.Address.inet
                               (Unix.Inet_addr.of_string "127.0.0.1") ~port))
      (fun s ->
        let w = Writer.create (Socket.fd s) in
        let buf = String.create 4096 in
        let rec loop bytes_written =
          Writer.write w buf;
          upon (Writer.flushed w) (fun _ ->
            let bytes_written = bytes_written + String.length buf in
            printf "wrote %d bytes in total.\n" bytes_written;
            loop bytes_written);
        in
        loop 0))
;;

let () = doit ()

let () = never_returns (Scheduler.go ())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.