(* A simple sockets library. *) module Socket = struct local module S = Prim.Socket with let getAddrByName (host: string) (port: string) : S.sockAddr = let info = S.AddrInfo([], S.AF_INET, S.Stream, S.defaultProtocol, S.SockAddrInet(S.PortNum 0, 0), None) in match S.getAddrInfo (Some info) (Some host) (Some port) with | S.AddrInfo (_, _, _, _, sockAddr, _) ∷ _ -> sockAddr | _ -> failwith ("Could not resolve address "^host^":"^port) type socket = S.socket let socket (): socket = S.socket S.AF_INET S.Stream S.defaultProtocol let bind (sock: socket) (port: int) : unit = S.bind sock (S.SockAddrInet (S.PortNum port, S.inaddr_any)) let connect (sock: socket) (host: string) (port: string) : unit = S.connect sock (getAddrByName host port) let listen (sock: socket) : unit = S.listen sock 5 let accept (sock: socket) : socket = let (new, _) = S.accept sock in new let send (sock: socket) (data: string) : int = S.send sock data let recv (sock: socket) (len: int) : string = S.recv sock len let close (sock: socket) : unit = S.close sock end end