src/Tools/WWW_Find/server_socket.ML
author wenzelm
Wed, 21 Sep 2011 17:50:25 +0200
changeset 45026 5c0b0d67f9b1
parent 43703 src/Tools/WWW_Find/socket_util.ML@c37a1f29bbc0
permissions -rw-r--r--
slightly more general Socket_IO as part of Pure;

(*  Title:      Tools/WWW_Find/socket_util.ML
    Author:     Timothy Bourke, NICTA

Server sockets.
*)

signature SERVER_SOCKET =
sig
  val init: string option -> int -> Socket.passive INetSock.stream_sock
end;

structure Server_Socket: SERVER_SOCKET =
struct

fun init opt_host port =
  let
    val sock = INetSock.TCP.socket ();
    val addr =
      (case opt_host of
         NONE => INetSock.any port
       | SOME host =>
           NetHostDB.getByName host
           |> the
           |> NetHostDB.addr
           |> rpair port
           |> INetSock.toAddr
           handle Option => raise Fail ("Cannot resolve hostname: " ^ host));
    val _ = Socket.bind (sock, addr);
    val _ = Socket.listen (sock, 5);
  in sock end;

end;