(************************************************************ nonblock.ml Created : Thu Dec 11 15:44:06 2003 Last modified: Thu Dec 11 16:02:09 2003 Compile: ocamlc -dtypes str.cma unix.cma mytcp.ml nonblock.ml -o nonblock # FTP Directory: sources/ocaml # ************************************************************) (** @author Takashi Masuyama *) let connect_timeout = 5.0 let non_blocking_connect host port = let addr = Unix.gethostbyname host in let s = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in Unix.setsockopt_float s Unix.SO_RCVTIMEO connect_timeout; Unix.setsockopt_float s Unix.SO_SNDTIMEO connect_timeout; try Unix.connect s (Unix.ADDR_INET(addr.Unix.h_addr_list.(0), port)); Unix.setsockopt_float s Unix.SO_RCVTIMEO 0.0; Unix.setsockopt_float s Unix.SO_SNDTIMEO 0.0; s with Unix.Unix_error(Unix.EINPROGRESS, _, _) -> Unix.close s; raise Exit ;; let _ = let host = "pauillac.inria.fr" in let port = 80 in try let s = non_blocking_connect host port in print_endline "finished"; Unix.close s with Exit -> print_endline "timeout";