Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: tyler <tyler@tricycle.org>
To: Ravi Chamarty <ravi@ittc.ukans.edu>
Cc: caml-list@inria.fr
Subject: Re: Netwoking examples..
Date: Fri, 5 May 2000 12:29:15 -0500 (CDT)	[thread overview]
Message-ID: <Pine.LNX.4.10.10005051220440.1435-100000@nobuko-cum.uchicago.edu> (raw)
In-Reply-To: <Pine.LNX.4.10.10005041804110.8149-100000@elroy.ittc.ukans.edu>

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
> 



      parent reply	other threads:[~2000-05-05 18:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-05-04 23:06 Ravi Chamarty
2000-05-05  8:20 ` Michel Quercia
2000-05-05 19:39   ` Charles Neveu
2000-05-05  8:39 ` Daniel de Rauglaudre
2000-05-05 11:16 ` David Mentré
2000-05-05 13:00 ` Benjamin C. Pierce
2000-05-05 17:29 ` tyler [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=Pine.LNX.4.10.10005051220440.1435-100000@nobuko-cum.uchicago.edu \
    --to=tyler@tricycle.org \
    --cc=caml-list@inria.fr \
    --cc=ravi@ittc.ukans.edu \
    /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