From: dt@dt7463.spb.edu (Dimitri Timofeev)
To: "altavillasalvatore@libero.it" <altavillasalvatore@libero.it>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Unix.sendto Unix.recvfrom
Date: Sat, 23 Nov 2002 00:43:10 +0300 [thread overview]
Message-ID: <20021122214310.GA1352@dt7463.spb.edu> (raw)
In-Reply-To: <H5ZMDG$FF4F6D67CB14C1A03B78DB38EF83B9D8@libero.it>
[-- Attachment #1: Type: text/plain, Size: 348 bytes --]
Hi!
On Fri, 22 Nov 2002, altavillasalvatore@libero.it wrote:
> I would want to have an example of like sending and receiving a
> package (type string) in ocaml via UDP protocol
Maybe an attached file (an exercise) could help you. By the way,
i'd like to hear all the criticism and suggestions one may have
concerning this code.
--
Dimitri
[-- Attachment #2: client.ml --]
[-- Type: text/plain, Size: 2725 bytes --]
(*
* client.ml
* OCaml version of sample robocup client
*)
let message_length = 1024 ;;
let resolve_host server =
let server_addr =
try
Unix.inet_addr_of_string server
with
| Failure ("inet_addr_of_string") ->
(Unix.gethostbyname server).Unix.h_addr_list.(0)
in server_addr
;;
let connect_soccerserver sockaddr =
let sock = Unix.socket Unix.PF_INET Unix.SOCK_DGRAM 0
in Unix.bind sock (Unix.ADDR_INET (Unix.inet_addr_any, 0));
sock;
;;
let port_of_sockaddr sockaddr =
match sockaddr with
| Unix.ADDR_UNIX (_) -> 0
| Unix.ADDR_INET (_, port) -> port
;;
let send_message message sock servaddr =
Unix.sendto sock message 0 (String.length message) [] servaddr;
print_string ("send " ^ (string_of_int (port_of_sockaddr servaddr)) ^ " : " ^ message);
flush stdout
;;
(* receive_message sock *)
let receive_message =
let buf = ref (String.create message_length) in
let recv_msg sock =
let len, addr = Unix.recvfrom sock !buf 0 message_length [] in
let answer = String.sub !buf 0 len
in print_string ("recv " ^ (string_of_int (port_of_sockaddr addr)) ^ " : " ^ answer ^ "\n");
flush stdout; (answer, addr)
in recv_msg
;;
let select_loop sock sockaddr =
let icfd = Unix.descr_of_in_channel stdin in
let addr = ref sockaddr
in while true do
match Unix.select [icfd; sock] [] [] 1.0 with
| [], [], [] -> ()
| fdl, [], [] ->
if List.mem sock fdl then (let _, newaddr = receive_message sock in addr := newaddr);
if List.mem icfd fdl then send_message ((input_line stdin) ^ "\n") sock !addr;
| _ -> ()
done
;;
let message_loop sock servaddr =
try
select_loop sock servaddr
with
| End_of_file -> print_string "terminate"; print_newline ()
;;
let default_host = "localhost" ;;
let default_port = 6000 ;;
let main () =
print_string "simple robocup client"; print_newline ();
try
let argc = Array.length Sys.argv in
let host = if argc < 2 then default_host else Sys.argv.(1) in
let port = if argc < 3 then default_port else int_of_string Sys.argv.(2) in
let sockaddr = Unix.ADDR_INET (resolve_host host, port) in
let sock = connect_soccerserver sockaddr
in print_string ("Connecting to " ^ host ^ ":" ^ (string_of_int port));
print_newline ();
message_loop sock sockaddr;
Unix.close sock
with
| Failure ("int_of_string") -> Printf.eprintf "%s: bad port number\n" Sys.argv.(2)
| Not_found -> Printf.eprintf "%s: host not found\n" Sys.argv.(1)
;;
let _ = main () ;;
prev parent reply other threads:[~2002-11-22 21:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-11-22 17:10 altavillasalvatore
2002-11-22 21:43 ` Dimitri Timofeev [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20021122214310.GA1352@dt7463.spb.edu \
--to=dt@dt7463.spb.edu \
--cc=altavillasalvatore@libero.it \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox