* Unix Domain Sockets and (write) (read)
@ 2013-07-02 22:31 Alex Sassmannshausen
2013-07-02 22:57 ` Aleix Conchillo Flaqué
0 siblings, 1 reply; 5+ messages in thread
From: Alex Sassmannshausen @ 2013-07-02 22:31 UTC (permalink / raw)
To: guile-user
Hello,
I'm playing around with interprocess communication through Unix Domain
Sockets and guile. I've got 2 programs: a 'server' and a 'client'. I'm
trying to send s-expressions from the one to the other using the 'read'
and 'write' procedures.
I've got the sockets set up currently:
On the server:
(let ((s (socket PF_UNIX SOCK_STREAM 0))
(sock-addr (make-socket-address AF_UNIX "/path/to/socket")))
(setsockopt s SOL_SOCKET SO_REUSEADDR 1)
(bind s sock-addr)
(listen s 5)
(let* ((client-connection (accept s))
(client-details (cdr client-connection))
(client (car client-connection)))
(simple-format #t "Got new client connection:")
(newline)
(simple-format #t "writing message")
(newline)
(write 'test-comms client)
(simple-format #t "Written. Now waiting for response.")
(newline)
(read) ; to make the server wait for standard input
(close client)))
On the client:
(let* ((s (socket PF_UNIX SOCK_STREAM 0))
(path (string-append "/path/to/socket"))
(address (make-socket-address AF_UNIX path)))
(connect s address)
(display (read s))
(close s))
- I first run the server, which sits and wait for client connections.
- I run the client, which connects to the server
- The server accepts, writes a message to client
- Then it moves on to the (read) stage
- The client now simply reads from s in perpetuity
When I finally provide some input and a newline in std input for the
server, it closes the connection to the client, and at this stage the
client finally returns with its reading from the socket.
My question is simply: is this supposed to happen? Would I somehow need
to close and re-open the socket to have a two-way conversation between
the client and the server (e.g. client writes request, closes the socket,
server reads from socket, evaluates, client re-connects, server provides
response)?
The documentation suggests that read simply reads one s-expression and
returns (guile 2 > API > REPL > Scheme Read):
Read an s-expression from the input port PORT, or from the current
input port if PORT is not specified. Any whitespace before the
next token is discarded.
When I write several s-expressions to the unix port and then disconnect,
the client does indeed read one s-expression at a time.
Any ideas or comments as to what is going on?
Best wishes,
Alex
PS: just noticed that changing the client socket from SOCK_STREAM to
SOCK_DGRAM allows the client to read all but the final s-expression from
the socket — so it hangs on the last one. Again, closing the socket
server-side then returns the last s-expression client-side.
This wouldn't resolve the 2 way communications issue as SOCK_DGRAM does
not seem to work for a server: (listen) returns an error with SOCK_DGRAM
server-side.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unix Domain Sockets and (write) (read)
2013-07-02 22:31 Unix Domain Sockets and (write) (read) Alex Sassmannshausen
@ 2013-07-02 22:57 ` Aleix Conchillo Flaqué
2013-07-04 22:05 ` Alex Sassmannshausen
0 siblings, 1 reply; 5+ messages in thread
From: Aleix Conchillo Flaqué @ 2013-07-02 22:57 UTC (permalink / raw)
To: Alex Sassmannshausen; +Cc: guile-user
On Tue, Jul 2, 2013 at 3:31 PM, Alex Sassmannshausen
<alex.sassmannshausen@gmail.com> wrote:
>
> My question is simply: is this supposed to happen? Would I somehow need
> to close and re-open the socket to have a two-way conversation between
> the client and the server (e.g. client writes request, closes the socket,
> server reads from socket, evaluates, client re-connects, server provides
> response)?
>
I think that the answer is yes. It is supposed to happen with the code
you provided.
When the server calls (write) you should flush the port (force-output)
so data gets immediately sent to the client, if that's what you want.
For the two-way conversation, you simply need to keep on calling
(read) and (write) wherever you want in the server and/or client and
not close the socket until you are really done.
Aleix
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unix Domain Sockets and (write) (read)
2013-07-02 22:57 ` Aleix Conchillo Flaqué
@ 2013-07-04 22:05 ` Alex Sassmannshausen
2013-07-05 12:25 ` Ludovic Courtès
0 siblings, 1 reply; 5+ messages in thread
From: Alex Sassmannshausen @ 2013-07-04 22:05 UTC (permalink / raw)
To: Aleix Conchillo Flaqué; +Cc: guile-user
Hello again,
Aleix Conchillo Flaqué <aconchillo@gmail.com> writes:
> On Tue, Jul 2, 2013 at 3:31 PM, Alex Sassmannshausen
> <alex.sassmannshausen@gmail.com> wrote:
>>
>> My question is simply: is this supposed to happen? Would I somehow need
>> to close and re-open the socket to have a two-way conversation between
>> the client and the server (e.g. client writes request, closes the socket,
>> server reads from socket, evaluates, client re-connects, server provides
>> response)?
>>
>
> I think that the answer is yes. It is supposed to happen with the code
> you provided.
>
> When the server calls (write) you should flush the port (force-output)
> so data gets immediately sent to the client, if that's what you want.
Ah, so that is the missing piece in the puzzle. Thanks for your
response.
>
> For the two-way conversation, you simply need to keep on calling
> (read) and (write) wherever you want in the server and/or client and
> not close the socket until you are really done.
In the meantime I've also figured out that read-line and write-line
allows me to have this 2-way conversation…
I'm thinking of writing a summary of my attempts as a novice in socket
communications with an example of Unix domain sockets in action. Do
people think that something like that might also be a useful addition to
the network chapter in Guile's manual? Or is the Examples section kept
quite brief on purpose?
Best wishes,
Alex
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unix Domain Sockets and (write) (read)
2013-07-04 22:05 ` Alex Sassmannshausen
@ 2013-07-05 12:25 ` Ludovic Courtès
2013-07-16 17:17 ` Alex Sassmannshausen
0 siblings, 1 reply; 5+ messages in thread
From: Ludovic Courtès @ 2013-07-05 12:25 UTC (permalink / raw)
To: guile-user
Hi,
Alex Sassmannshausen <alex.sassmannshausen@gmail.com> skribis:
> I'm thinking of writing a summary of my attempts as a novice in socket
> communications with an example of Unix domain sockets in action. Do
> people think that something like that might also be a useful addition to
> the network chapter in Guile's manual? Or is the Examples section kept
> quite brief on purpose?
It’s always useful to have more introductory material.
My (biased) viewpoint is that the “Network Socket Examples” section has
the important thing because after all, changing these examples to use
Unix-domain sockets instead of TCP should be a simple exercise for the
reader. ;-)
What would you add to that section?
The glibc manual has additional examples to datagram sockets, among
other things. Perhaps that should be an inspiration?
Thanks,
Ludo’.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unix Domain Sockets and (write) (read)
2013-07-05 12:25 ` Ludovic Courtès
@ 2013-07-16 17:17 ` Alex Sassmannshausen
0 siblings, 0 replies; 5+ messages in thread
From: Alex Sassmannshausen @ 2013-07-16 17:17 UTC (permalink / raw)
To: Ludovic Courtès; +Cc: guile-user
ludo@gnu.org (Ludovic Courtès) writes:
Hello,
> Hi,
>
> Alex Sassmannshausen <alex.sassmannshausen@gmail.com> skribis:
>
>> I'm thinking of writing a summary of my attempts as a novice in socket
>> communications with an example of Unix domain sockets in action. Do
>> people think that something like that might also be a useful addition to
>> the network chapter in Guile's manual? Or is the Examples section kept
>> quite brief on purpose?
>
> It’s always useful to have more introductory material.
>
> My (biased) viewpoint is that the “Network Socket Examples” section has
> the important thing because after all, changing these examples to use
> Unix-domain sockets instead of TCP should be a simple exercise for the
> reader. ;-)
I agree on this bit — I found it a useful exercise myself.
> What would you add to that section?
Maybe it's not quite this section, but in the section relating to
read/write scheme objects (6.17 Reading and Evaluating Scheme Code), if
the solution to my original problem is indeed resolved by Aleix's
suggestions to use (force-output) on the socket to enable two way
communication.
Maybe a sub-section in that chapter containing examples of read/write,
in a similar fashion to the examples in the sockets chapter?
> The glibc manual has additional examples to datagram sockets, among
> other things. Perhaps that should be an inspiration?
I will have a look at that manual and see what I can come up with — in
time :-)
Best wishes,
Alex
>
> Thanks,
> Ludo’.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2013-07-16 17:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-02 22:31 Unix Domain Sockets and (write) (read) Alex Sassmannshausen
2013-07-02 22:57 ` Aleix Conchillo Flaqué
2013-07-04 22:05 ` Alex Sassmannshausen
2013-07-05 12:25 ` Ludovic Courtès
2013-07-16 17:17 ` Alex Sassmannshausen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).