caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Unix.sendto Unix.recvfrom
@ 2002-11-22 17:10 altavillasalvatore
  2002-11-22 21:43 ` Dimitri Timofeev
  0 siblings, 1 reply; 2+ messages in thread
From: altavillasalvatore @ 2002-11-22 17:10 UTC (permalink / raw)
  To: caml-list

Hi all,
I would want to have an example of like sending and receiving a 
package (type string) in ocaml via UDP protocol
 
Thanks.
 


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Unix.sendto Unix.recvfrom
  2002-11-22 17:10 [Caml-list] Unix.sendto Unix.recvfrom altavillasalvatore
@ 2002-11-22 21:43 ` Dimitri Timofeev
  0 siblings, 0 replies; 2+ messages in thread
From: Dimitri Timofeev @ 2002-11-22 21:43 UTC (permalink / raw)
  To: altavillasalvatore; +Cc: caml-list

[-- 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 () ;;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-11-22 21:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-22 17:10 [Caml-list] Unix.sendto Unix.recvfrom altavillasalvatore
2002-11-22 21:43 ` Dimitri Timofeev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).