Version française
Home     About     Download     Resources     Contact us    
Browse thread
Netwoking examples..
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: tyler <tyler@t...>
Subject: Re: Netwoking examples..
Hey,
These are two very short programs I hacked up that worked when I tried
them.

Here's a server:
open Unix

let _ = 
  let this_socket = (Unix.socket PF_INET SOCK_STREAM 0) in 
  let this_socket_addr = 
    ADDR_INET ((Unix.inet_addr_of_string "127.0.0.1"), 60648) in
  Unix.bind this_socket this_socket_addr;
  Unix.listen this_socket 3;
  let (client_socket, client_addr) = Unix.accept this_socket in
  let this_string = String.create 100 in
  let number_read = Unix.read client_socket this_string 0 100 in
  let number_written = Unix.write client_socket this_string 0 number_read
in
  Unix.close this_socket;
  exit 0
;;

Here's a client:
open Unix 

let _ = 
  let this_socket = (Unix.socket PF_INET SOCK_STREAM 0) in 
  let server_addr = 
    ADDR_INET ((Unix.inet_addr_of_string "127.0.0.1"), 60648) in
  Unix.connect this_socket server_addr;
  let num_written = Unix.write this_socket "hello world" 0 11 in 
  let response_string = String.create 20 in
  let num_read = Unix.read this_socket response_string 0 20 in 
  print_string response_string;
  Unix.close this_socket;
  exit 0
;;  

Compile them, start up the server program, and then run the client
program. It should print out "hello world".
Again, it's way simple, but it should hopefully give you some ideas. For a
server, make the socket, then bind it to an address, then have it listen
on a port, then have it accept a connection. When you're done, close the
socket (is there an ocaml function for flushing a file descriptor, not an
out_channel?). For a client, just make the socket and tell it where to
connect to. Its own address and port are handled automatically. You might
want to look around for a socket tutorial in c (I've seen one somewhere
before...). If you understand what's happening on both ends, it's a
question of syntax at that point. Have fun!
tyler

On Thu, 4 May 2000, Ravi Chamarty wrote:

> 
> Hi,
> 
>   Can  anyone give me a pointer to a few networking examples in
> OCaml using sockets and connects ??
> 
> 
> Thanks,
> Ravi
> ----------------------------------------------------------
> Ravi S Chamarty                    E-mail: ravi@ittc.ukans.edu
> Graduate Research Assistant,       Voice :785-864-7799 
> ITTC,2291 Irving Hill Road,                
> University of Kansas,
> Lawrence KS 66044-7541
>