From: Zelphir Kaltstahl <zelphirkaltstahl@gmail.com>
To: guile-user@gnu.org
Subject: Re: Trouble trying to use some modules from the docs (Matt Wette)
Date: Sat, 16 Jun 2018 11:35:38 +0200 [thread overview]
Message-ID: <209f27c1-8191-67dc-3f9d-1bc7c7ecd562@gmail.com> (raw)
In-Reply-To: <mailman.103.1528041619.20010.guile-user@gnu.org>
Hello,
I have managed to get another case of a binding not being available
according to the Guile REPL, this time I have the code and way to
reproduce the issue.
After some struggling, trying and reading the docs of networking
procedures multiple times, I managed to write a little TCP server and
client example program.
(More examples in this area would be cool and if my code is worth
anything, feel free to take it, improve it, and make an example for TCP
server/client in the docs. Or maybe I can put examples in the docs?)
The directory structure looks as follows (tree):
.
├── main.scm
├── networking-lib
│ └── helpers.scm
├── tcp-client.scm
└── tcp-server.scm
The contents of the files are:
;; ===== HELPERS MODULE (networking-lib/helpers.scm) =====
(use-modules (rnrs bytevectors))
(define-module (networking-lib helpers)
#:export (display-byte-vector))
(define (display-byte-vector vec len)
(let ([minimal-vec (make-bytevector len)])
(bytevector-copy! vec 0 minimal-vec 0 len)
(display (simple-format #f
"I got the following message: ~s\n"
(utf8->string minimal-vec)))))
;; ===== HELPERS MODULE END =====
;; ===== CLIENT (tcp-client.scm) =====
(add-to-load-path
(string-append "/home/xiaolong"
"/development/Guile"
"/examples-and-convenience-procedures/network-programming"))
(use-modules (rnrs bytevectors)
(networking-lib helpers))
(define receive-buffer (make-bytevector 1024))
(define (connect-to-server)
(let ([sock
(socket PF_INET SOCK_STREAM
(protoent:proto (getprotobyname "TCP")))])
(connect sock
(make-socket-address AF_INET
INADDR_LOOPBACK
12345))
sock))
(define (send-message-to-server sock message)
(display (simple-format #f "sending message to server: ~s" message))
(send sock (string->utf8 message)))
(define (receive-message-from-server! sock receive-buffer)
(recv! sock receive-buffer))
(define in-out-sock (connect-to-server))
(let ([bytes-count (receive-message-from-server! in-out-sock
receive-buffer)])
(display-byte-vector receive-buffer bytes-count)
(send-message-to-server in-out-sock "Hello from client!\n"))
;; ===== CLIENT END =====
;; ===== SERVER (tcp-server.scm) =====
(add-to-load-path
(string-append "/home/user"
"/development/Guile"
"/examples-and-convenience-procedures/network-programming"))
(use-modules (rnrs bytevectors)
(networking-lib helpers)
(ice-9 threads))
(define receive-buffer (make-bytevector 1024))
(define (create-server-socket)
(let ([sock
(socket PF_INET SOCK_STREAM (protoent:proto (getprotobyname
"TCP")))]
[backlog-of-connection-requests 10])
(bind sock (make-socket-address AF_INET INADDR_ANY 12345))
(listen sock backlog-of-connection-requests)
sock))
(define (send-message-to-client sock message)
(display (simple-format #f "sending message to client: ~s\n" message))
(send sock (string->utf8 message)))
(define (handle-messages sock)
(call-with-new-thread
(λ ()
(while #t
(let* ([client-connection
(accept sock)]
[client-details (cdr client-connection)]
[in-out-sock (car client-connection)])
(display
(simple-format #f
"Got new client connection: ~S\n"
client-details))
(display
(simple-format #f
"Client address: ~S\n"
(gethostbyaddr (sockaddr:addr client-details))))
(send-message-to-client in-out-sock "Hello from server!\n")
(display-byte-vector receive-buffer (recv! in-out-sock
receive-buffer))
(display "closing input-output-socket with client ...\n"))))))
(define sock (create-server-socket))
(handle-messages sock)
;; ===== SERVER END =====
With that code, I do the following:
1. Start two Guile REPLs inside the
.../examples-and-convenience-procedures/network-programming directory.
(in XFCE4 terminal)
2. In the first REPL I paste all code of the server. It starts a new
thread and runs fine without showing any error.
3. In the second REPL I paste all code of the client. It seems to run
fine until I hit the last let form.
The error I get is:
;; ===== ERROR =====
scheme@(guile-user)> (let ([bytes-count (receive-message-from-server!
in-out-sock receive-buffer)])
... (display-byte-vector receive-buffer bytes-count)
... (send-message-to-server in-out-sock "Hello from client!\n"))
networking-lib/helpers.scm:7:0: In procedure display-byte-vector:
In procedure module-lookup: Unbound variable: make-bytevector
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]>
;; ===== ERROR END =====
But I did import bytevectors in helpers.scm! How can it not find that?
When I paste the procedure defined in helpers.scm into the server and
client programs and do not use the helpers module, it seem to work just
fine.
Is it impossible to use modules inside the helpers module, which are
already used code, which uses the helper module (rnrs bytevectors)?
Regards,
Zelphir
next prev parent reply other threads:[~2018-06-16 9:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.103.1528041619.20010.guile-user@gnu.org>
2018-06-04 16:35 ` Trouble trying to use some modules from the docs (Matt Wette) Zelphir Kaltstahl
2018-06-05 19:36 ` Mark H Weaver
2018-06-16 9:35 ` Zelphir Kaltstahl [this message]
2018-06-16 13:36 ` Matt Wette
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
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=209f27c1-8191-67dc-3f9d-1bc7c7ecd562@gmail.com \
--to=zelphirkaltstahl@gmail.com \
--cc=guile-user@gnu.org \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).