Anonymous avatar Anonymous committed 875e157

network code ok

Comments (0)

Files changed (1)

-
-let accept_con _ =
-    let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
-    let saddr = Unix.ADDR_INET (Unix.inet_addr_any, 13337) in
-    Unix.bind sock saddr;
-    Unix.listen sock 4;
-    Unix.accept sock
 
 let main _ =
     print_endline "Sup dawg, I heard you like starcraft";
     print_endline "------------------------------------";
     print_endline "Waiting...";
-    let (fd, _) = accept_con () in
+    let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
+    let saddr = Unix.ADDR_INET (Unix.inet_addr_any, 13337) in
+    Unix.bind sock saddr;
+    Unix.listen sock 4;
+    let buflen = 512 in
+    let buf = String.create buflen in
+    let (fd, _) = Unix.accept sock in
     print_endline "Got it !";
-    let buf = String.make 512 'X' in
+    let total = ref 0 in
     while true do
-        let z = Unix.recv fd buf 0 512 [] in
-        Printf.printf "Recv %d chars : <%s>" z
-            (String.sub buf 0 z)
+        let z = Unix.recv fd buf 0 buflen [] in
+        total := !total + z;
+        Printf.printf "Recv %d <%s>\n" z (String.sub buf 0 z);
+        flush stdout;
+        if (!total >= 28) then
+            begin
+                ignore (Unix.send fd "00" 0 2 []);
+                total := 0;
+            end
     done
 
+
 let _ = main ()
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 ProjectModifiedEvent.java.
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.