Guest User

Untitled

a guest
Feb 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. let host = ref ""
  2. let port = ref 80
  3.  
  4. let usage = "usage: foo host [-p port]"
  5.  
  6. let _ =
  7. Arg.parse
  8. [
  9. "-p", Arg.Int (fun i -> port := i), "\tPort";
  10. ]
  11. (fun s -> host := s) usage;
  12. if !host = ""
  13. then Printf.printf "%s\n\n" usage
  14. else let sockaddr =
  15. let host =
  16. try
  17. Unix.gethostbyname !host
  18. with Not_found -> failwith "Host not found"
  19. in
  20. Unix.ADDR_INET (host.Unix.h_addr_list.(0), !port)
  21. in
  22. let socket =
  23. let s = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0
  24. in
  25. Unix.connect s sockaddr;
  26. s
  27. in
  28. let bufsize = 1024
  29. in
  30. let buf = String.create bufsize
  31. in
  32. let loop = ref true
  33. in
  34. Printf.printf "Connection ok.\nType 'exit' to quit.\n\n%!";
  35. let readThread () =
  36. let buf = String.create bufsize in
  37. while !loop
  38. do
  39. let r = Unix.recv socket buf 0 bufsize []
  40. in
  41. Printf.printf "%s%!" (String.sub buf 0 r)
  42. done
  43. in
  44. ignore (Thread.create readThread ());
  45. while !loop
  46. do
  47. Printf.printf "Ready> %!";
  48. let r = Unix.read Unix.stdin buf 0 bufsize
  49. in
  50. if r = 4 && String.sub buf 0 4 = "exit"
  51. then loop := false
  52. else ignore (Unix.send socket buf 0 r [])
  53. done
Add Comment
Please, Sign In to add comment