Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let host = ref ""
- let port = ref 80
- let usage = "usage: foo host [-p port]"
- let _ =
- Arg.parse
- [
- "-p", Arg.Int (fun i -> port := i), "\tPort";
- ]
- (fun s -> host := s) usage;
- if !host = ""
- then Printf.printf "%s\n\n" usage
- else let sockaddr =
- let host =
- try
- Unix.gethostbyname !host
- with Not_found -> failwith "Host not found"
- in
- Unix.ADDR_INET (host.Unix.h_addr_list.(0), !port)
- in
- let socket =
- let s = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0
- in
- Unix.connect s sockaddr;
- s
- in
- let bufsize = 1024
- in
- let buf = String.create bufsize
- in
- let loop = ref true
- in
- Printf.printf "Connection ok.\nType 'exit' to quit.\n\n%!";
- let readThread () =
- let buf = String.create bufsize in
- while !loop
- do
- let r = Unix.recv socket buf 0 bufsize []
- in
- Printf.printf "%s%!" (String.sub buf 0 r)
- done
- in
- ignore (Thread.create readThread ());
- while !loop
- do
- Printf.printf "Ready> %!";
- let r = Unix.read Unix.stdin buf 0 bufsize
- in
- if r = 4 && String.sub buf 0 4 = "exit"
- then loop := false
- else ignore (Unix.send socket buf 0 r [])
- done
Add Comment
Please, Sign In to add comment