Commits

UENISHI Kota  committed e5e8be9

tcp mock server workedhg st

  • Participants
  • Parent commits ecf470f

Comments (0)

Files changed (3)

+syntax: glob
+.omakedb*
+*~
+*.cmx
+*.opt
+*.o
+*.omc
+*.cmi
 # This project requires ocamlfind (default - false).
 #
 # USE_OCAMLFIND = true
-#
-# OCAMLPACKS[] =
+
+#OCAMLPACKS[] =
 #    pack1
 #    pack2
-#
-# if $(not $(OCAMLFIND_EXISTS))
-#    eprintln(This project requires ocamlfind, but is was not found.)
-#    eprintln(You need to install ocamlfind and run "omake --configure".)
-#    exit 1
+
+#if $(not $(OCAMLFIND_EXISTS))
+#	eprintln('This project requires ocamlfind, but is was not found.')
+#	eprintln('You need to install ocamlfind and run "omake --configure"')
+#	exit 1
 
 #
 # Include path
 #
 # The default values are defined as follows:
 #
-NATIVE_ENABLED = $(OCAMLOPT_EXISTS)
-BYTE_ENABLED = $(not $(OCAMLOPT_EXISTS))
+# NATIVE_ENABLED = $(OCAMLOPT_EXISTS)
+# BYTE_ENABLED = $(not $(OCAMLOPT_EXISTS))
 
 #
 # Various options
 OCAMLFLAGS    +=
 OCAMLCFLAGS   +=
 OCAMLOPTFLAGS +=
-OCAML_LINK_FLAGS +=
+OCAML_LINK_FLAGS += 
 OCAML_BYTE_LINK_FLAGS +=
 OCAML_NATIVE_LINK_FLAGS +=
 
 #    file2
 
 PROGRAM = tcp_server
-# OCAML_LIBS +=
-# OCAML_CLIBS +=
-# OCAML_OTHER_LIBS +=
-# OCAML_LIB_FLAGS +=
-#
+OCAML_LIBS +=
+OCAML_CLIBS +=
+OCAML_OTHER_LIBS += unix
+OCAML_LIB_FLAGS +=
+
 .DEFAULT: $(OCamlProgram $(PROGRAM), $(FILES))
 
 #test: $(PROGRAM)

File tcp_server.ml

-Printf.printf "hoge hoge"
+open Sys;;
+open Unix;;
 
+let start_tcp_server addr =
+  let sock  = socket PF_INET SOCK_STREAM 0 in
+    try 
+      setsockopt sock SO_REUSEADDR true;
+      bind sock addr;
+      listen sock 10;
+      sock
+    with z-> close sock; raise z;; 
+
+let start_default_tcp_server port = 
+  start_tcp_server (ADDR_INET (inet_addr_any, port));;
+
+let tcp_server treat_connection = 
+  ignore (signal sigpipe Signal_ignore);
+  let sock = start_default_tcp_server 10009 in
+    while true do
+      let client = accept sock in
+	treat_connection client
+    done;;
+
+Printf.printf "staring server in 10009\n";;
+let callback (csock, caddr) = 
+(*  print_endline (string_of_inet_addr ADDR_UNIX (addr)); *)
+  begin match caddr with
+    | ADDR_INET(caller, _)->
+	prerr_endline ("connection from " ^ string_of_inet_addr caller );
+    | ADDR_UNIX(_)->
+	prerr_endline "connection from the unix domain ... ";
+  end;
+  close csock in
+  handle_unix_error tcp_server callback;;
+
+(*    print_endline (string_of_inet_addr ADDR_UNIX(addr)); 
+  let (sockaddr, port) = addr in
+    Printf.printf "accepting %d " port;
+*)