From: David Allsopp <dra-news@metastack.com>
To: "'Gerd Stolpmann'" <info@gerd-stolpmann.de>
Cc: "'OCaml List'" <caml-list@inria.fr>
Subject: RE: [Caml-list] Writing to a blocked socket
Date: Tue, 2 Aug 2011 20:17:51 +0000 [thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D9C23D38EE@Remus.metastack.local> (raw)
In-Reply-To: <1312312561.6236.307.camel@thinkpad>
[-- Attachment #1: Type: text/plain, Size: 1375 bytes --]
Gerd Stolpmann wrote:
> Am Dienstag, den 02.08.2011, 18:01 +0100 schrieb David Allsopp:
> > I don't seem to be able to ask Google this in a way which will give me
> > a reasonable answer!
> >
> > In the same process, if you have one thread blocked on a [recv]
> > operation on a socket, under Unix another thread can still write to
> > the socket. Under Windows, however, the call to [send] blocks because
> > there's another thread blocked on a [recv] to the same socket. Are
> > there any options that can be set to change that behaviour or is that
> > just "the way it is" and the application has to be coded using [select]
> instead?
>
> Really? This does not make sense at all. It's quite normal that one
> direction is blocked, and the other not. Are you sure about your
> observation?
Seems to be, from the attached - important bit is on the last 10 lines - the function readThread is spawned in a thread of its own and then the loop below reads input and sends each line down the socket.
Compiled with:
ocamlfind ocamlopt -o foo -thread -package unix,threads -linkpkg Foo.ml
and then executed as:
./foo www.google.com
I enter:
GET / HTTP/1.0
followed by two new lines... on Linux I get a response from Google, on Windows it hangs after the first line. 3.12.0 (and 3.10.1 on an old machine) all behaving the same way.
David
[-- Attachment #2: Foo.ml --]
[-- Type: application/octet-stream, Size: 1723 bytes --]
let host = ref ""
let port = ref 80
let usage = "usage: foo host [-p port]"
let _ =
Arg.parse
[
"-p", Arg.Int (fun i -> port := i), "\tPort";
]
(fun s -> host := s) usage;
if !host = ""
then Printf.printf "%s\n\n" usage
else let sockaddr =
let host =
try
Unix.gethostbyname !host
with Not_found -> failwith "Host not found"
in
Unix.ADDR_INET (host.Unix.h_addr_list.(0), !port)
in
let socket =
let s = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0
in
Unix.connect s sockaddr;
s
in
let bufsize = 1024
in
let buf = String.create bufsize
in
let loop = ref true
in
Printf.printf "Connection ok.\nType 'exit' to quit.\n\n%!";
let readThread () =
let buf = String.create bufsize in
while !loop
do
let r = Unix.recv socket buf 0 bufsize []
in
Printf.printf "%s%!" (String.sub buf 0 r)
done
in
ignore (Thread.create readThread ());
while !loop
do
Printf.printf "Ready> %!";
let r = Unix.read Unix.stdin buf 0 bufsize
in
if r = 4 && String.sub buf 0 4 = "exit"
then loop := false
else ignore (Unix.send socket buf 0 r [])
done
next prev parent reply other threads:[~2011-08-02 20:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-02 17:01 David Allsopp
2011-08-02 19:16 ` Gerd Stolpmann
2011-08-02 20:17 ` David Allsopp [this message]
2011-08-02 20:41 ` David Allsopp
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=E51C5B015DBD1348A1D85763337FB6D9C23D38EE@Remus.metastack.local \
--to=dra-news@metastack.com \
--cc=caml-list@inria.fr \
--cc=info@gerd-stolpmann.de \
/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