Re: [q-lang-users] OpenBSD, threading
Brought to you by:
agraef
From: <mip...@ya...> - 2004-02-03 05:59:59
|
(OK, more agressive formatting.) Albert Graef wrote: > Julian Fondren wrote: > > ;; OpenBSD [...] > > > > Everything compiles fine expect for > > modules/clib/clib.c -- there I had to add an > > "#include <unistd.h>", since something > > apparently failed to define HAVE_UNISTD_H > > That's *really* strange. Could you please check the > generated config.h (in the toplevel directory of the > sources), to see if it defines HAVE_UNISTD_H? It does. With more investigation, the compilation only falls apart when the '#include <unistd.h>' comes *after* the '#define _XOPEN_SOURCE 500' in modules/clib/clib.c > Hmm, I tried to run your example, but somehow I > can't get it to work, the accept call in the server > always fails with EINVAL for me. Here's the call > sequence I used for initializing the server and the > client, I guess that's correct so far? Maybe I'm > missing something obvious? > > def S = server 5001 > def C = socket AF_INET SOCK_STREAM 0 > connect C ("localhost",5001) Huh, this seems to work with my current version. bwrite C (bytestr "Hello!\n") bstr (bread C 64) (though I used netcat to test it, for some reason) > Anyway, the problem with your code is that you don't > keep the handle for the echo_loop thread. You see, > the thread will be canceled automatically as soon as > the thread handle T is garbage collected. Here this > happens when server_loop FD tail-reduces to itself > (the local T variable is not used anymore and its > value is therefore thrown away). The solution is to > keep track of the thread handles, either in the > server_loop function or in echo_loop itself. The > latter solution would be something like the > following: Ah, OK. I'd actually thought of doing that, but didn't want to change my design until I understood why what I had failed. Thanks. [snip] > (As you'll notice I changed the second server_loop > rule to track down the cause of the mysterious > accept failures I encountered. Also note that you > don't have to explicitly cancel this_thread on exit > from echo_loop, in the second rule for echo_loop, as > the thread will be terminated anyway when the loop > ends.) > > Can you try whether this works for you? It did =) I ended up with a more nicely formatted version of the following: server P = thread (server_loop FD) where FD:Int = socket AF_INET SOCK_STREAM 0, () = bind FD ("localhost",P), () = listen FD 4; = perror "server" otherwise; server_loop FD = server_loop FD where (C:Int,(H,P)) = accept FD, _ = thread (echo_loop this_thread C H P), _ printf "Got connection from: %s:%d\n" (H,P); = server_loop FD otherwise where _ = perror "server(accept)"; echo_loop T FD H P = echo_loop T FD H P where MSG = recv FD 0 4096, () = echo FD MSG (bcmp MSG (bytestr "")); = closesocket FD otherwise where _ = printf "Lost connection from: %s:%d\n" (H,P); echo FD MSG 0 = error; echo FD MSG _ = () where _ = send FD 0 MSG; > Cheers, > Albert ________________________________________________________________________ BT Yahoo! Broadband - Free modem offer, sign up online today and save £80 http://btyahoo.yahoo.co.uk |