ocaml-bert /

UENISHI Kota a17abd7 

open Sys;;
open Unix;;

let start_tcp_server addr =
  let sock  = socket PF_INET SOCK_STREAM 0 in
      setsockopt sock SO_REUSEADDR true;
      bind sock addr;
      listen sock 10;
    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

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 ... ";
  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);
  let my_read sock len = 
    let buf = String.create len in
      my_read_ sock buf 0 len 
  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;;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.