diff options
author | Damien Doligez <damien.doligez-inria.fr> | 2015-10-09 15:45:37 +0000 |
---|---|---|
committer | Damien Doligez <damien.doligez-inria.fr> | 2015-10-09 15:45:37 +0000 |
commit | 3397e7ff162a5bf066b0f6b76d180c2c4a6ce7c5 (patch) | |
tree | e6322141f6a35378ee33b25e08bdb58e096269b4 /testsuite/tests/lib-threads/sockets.ml | |
parent | 54ace9943baf35970e1525bf67c37259694dbe0a (diff) | |
download | ocaml-3397e7ff162a5bf066b0f6b76d180c2c4a6ce7c5.tar.gz |
GPR#243: Faster test suite
(Xavier Leroy)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@16466 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'testsuite/tests/lib-threads/sockets.ml')
-rw-r--r-- | testsuite/tests/lib-threads/sockets.ml | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/testsuite/tests/lib-threads/sockets.ml b/testsuite/tests/lib-threads/sockets.ml new file mode 100644 index 0000000000..3f9f39376d --- /dev/null +++ b/testsuite/tests/lib-threads/sockets.ml @@ -0,0 +1,52 @@ +(***********************************************************************) +(* *) +(* OCaml *) +(* *) +(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) +(* *) +(* Copyright 1996 Institut National de Recherche en Informatique et *) +(* en Automatique. All rights reserved. This file is distributed *) +(* under the terms of the Q Public License version 1.0. *) +(* *) +(***********************************************************************) + +open Printf + +(* Threads and sockets *) + +let serve_connection s = + let buf = String.make 1024 '>' in + let n = Unix.read s buf 2 (String.length buf - 2) in + Thread.delay 1.0; + ignore (Unix.write s buf 0 (n + 2)); + Unix.close s + +let server sock = + while true do + let (s, _) = Unix.accept sock in + ignore(Thread.create serve_connection s) + done + +let client (addr, msg) = + let sock = + Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in + Unix.connect sock addr; + let buf = String.make 1024 ' ' in + ignore(Unix.write sock msg 0 (String.length msg)); + let n = Unix.read sock buf 0 (String.length buf) in + print_string (String.sub buf 0 n); flush stdout + +let _ = + let addr = Unix.ADDR_INET(Unix.inet_addr_loopback, 9876) in + let sock = + Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in + Unix.setsockopt sock Unix.SO_REUSEADDR true; + Unix.bind sock addr; + Unix.listen sock 5; + ignore (Thread.create server sock); + ignore (Thread.create client (addr, "Client #1\n")); + Thread.delay 0.5; + client (addr, "Client #2\n") + + + |