Commits

Anonymous committed a17abd7

changed name.

Comments (0)

Files changed (4)

 *.cmi
 *.cmxa
 *.beam
-tcp_server
+echo_server
 termreader
 *.bert
 # Build an OCaml program
 #
 
+SERVER_FILES[] = 
+     echo_server
+
 FILES[] =
      tests_bert
 #     tcp_server
 OCAML_LIB_FLAGS +=
 
 .DEFAULT: $(OCamlProgram $(PROGRAM), $(FILES)) \
-	  $(OCamlProgram termreader, $(FILES2))
+	  $(OCamlProgram termreader, $(FILES2)) \
+	  $(OCamlProgram echo_server, $(SERVER_FILES))
 
 test: $(PROGRAM) termreader
 	./termreader$(EXE)
+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 port = 10009 in
+    Printf.printf "staring server in %d ... %!" port;
+    let sock = start_default_tcp_server port  in
+      print_endline "[done]";
+      while true do
+	let client = accept sock in
+	  treat_connection client
+      done;;
+
+let accept_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;
+  let rec my_read_ sock buf start remain = 
+    let len = recv sock buf start remain [MSG_PEEK] in
+      begin match remain with 
+	| 0 -> (start+len, buf) ;
+	| r -> my_read_ sock buf (start+len) (r-len);
+      end;
+  in
+  let my_read sock len = 
+    let buf = String.create len in
+      my_read_ sock buf 0 len 
+  in
+  let (len, buffer) = my_read csock 1024 in
+    prerr_endline ((string_of_int len) ^ " bytes recv'd");
+    prerr_endline buffer;
+    close csock;
+    prerr_endline "----------------";;
+
+(* print_endline (string_of_inet_addr ADDR_UNIX(addr));  *)
+handle_unix_error tcp_server accept_callback;;
+
+
+

tcp_server.ml

-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;;
-
-let accept_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;
-  let rec my_read_ sock buf start remain = 
-    let len = recv sock buf start remain [MSG_PEEK] in
-      begin match remain with 
-	| 0 -> (start+len, buf) ;
-	| r -> my_read_ sock buf (start+len) (r-len);
-      end;
-  in
-  let my_read sock len = 
-    let buf = String.create len in
-      my_read_ sock buf 0 len 
-  in
-  let (len, buffer) = my_read csock 1024 in
-    prerr_endline ((string_of_int len) ^ " bytes recv'd");
-    prerr_endline buffer;
-    close csock;
-    prerr_endline "----------------";;
-
-(* print_endline (string_of_inet_addr ADDR_UNIX(addr));  *)
-Printf.printf "staring server in 10009\n";;
-handle_unix_error tcp_server accept_callback;;
-
-
-
-