我目前正在设计一个程序,其中部分程序文件在 Raspberry Pi 上运行,另一部分在我的计算机上运行。
为了在它们之间进行通信,我通过 TCP/IP 发送消息。所以要阅读传入的消息,我使用(read port)
. 然后我做一些计算并将答案发回。
现在我注意到当答案是一个数字时,我在另一边没有收到那个答案(我不知道是不是因为它是一个数字,我认为它是)。虽然已经寄出。之后它会导致不正确的读取(我想是因为它仍在缓冲区中)。
所以这就是我发送消息的方式:
#lang racket
; Not important
(require (rename-in racket/tcp
(tcp-connect racket-tcp-connect)
(tcp-listen racket-tcp-listen)))
(define (tcp-connect adress port)
(let-values ([(port-in port-out) (racket-tcp-connect adress port)])
(cons port-in port-out)))
;;;;
(define ports (tcp-connect "localhost" 6667))
(define in (car ports))
(define out (cdr ports))
(define (send destination message expectAnswer? . arguments)
(write-byte destination out) ; Send the destination (is a number < 256) (so that I know on the other side which object I have to send the message to).
(newline out) ; I noticed that if I don't do this, sometimes the message won't be sent.
(write message out)
(newline out)
(write arguments out)
(newline out)
(write expectAnswer? out)
(newline out)
(flush-output out)
(display "destination : ") (display destination) (newline)
(display "Message : ") (display message) (newline)
(display "Arguments : ") (display arguments) (newline)
(display "Expects an answer? ") (display expectAnswer?) (newline)
(when expectAnswer?
(let ((answer (read in)))
(if (eof-object? answer)
'CC ; CC = Connection Closed
(begin (display "Answer : ")(display answer)(newline)(newline)
answer)))))
这就是我阅读传入消息(在 Raspberry Pi 上)并返回答案的方式:
#lang racket
; Not important
(require (rename-in racket/tcp
(tcp-listen racket-tcp-listen)
(tcp-accept racket-tcp-accept)))
(define (tcp-accept port)
(let-values ([(port-in port-out) (racket-tcp-accept (racket-tcp-listen port))])
(cons port-in port-out)))
;;;;
(define ports (tcp-accept 6667))
(define in (car ports))
(define out (cdr ports))
(define (executeMessage destination message argumentList expectAnswer?)
(let ((destinationObject (decode destination)) ; This is the object that corresponds to the number we received
(answer '()))
(if (null? argumentList)
(set! answer (destinationObject message))
(set! answer (apply (destinationobject message) argumentList)))
(display "Destination : ")(display destination)(newline)
(display "Message : ")(display message)(newline)
(display "Arguments : ")(display argumentList)(newline)
(display "Expects answer? ")(display expectAnswer?) (newline)
(display "Answer : ")(display answer)(newline)(newline)
; We send the answer back if it is needed.
(when expectAnswer?
(write answer out)
(newline out) ; Because I noticed that if I don't to this, it won't be sent.
(flush-output out))))
; We call this function to skip the newlines that are send "(newline out)"
(define (skipNewline)
(read-byte in))
(define (listenForMessages)
(when (char-ready? in) ; Could be omitted.
; A message was sent
(let ((destination (read-byte in))
(message (begin (skipNewline) (read in)))
(argumentList (begin (skipNewline) (read in)))
(expectAnswer? (begin (skipNewline) (read in))))
(skipNewline)
(executeMessage destination message argumentList expectAnswer?)))
(listenForMessages))
(listenForMessages)
运行程序时,我看到一堆消息被正确发送和回答。但后来我看到一条消息,它期望得到答案但没有得到答案。这是树莓派上显示的内容:
Destination : 2
Message : getStationHoogte
Arguments : '()
Expects answer? #t
Answer : 15
所以消息被执行,结果是 15(我检查了它,这就是它应该产生的结果,所以到目前为止我很高兴)。
请注意, 的显示Answer : ...
发生在发送答案之前。
但在我的电脑上,我读到了这个:
Destination : 2
Message : getStationHoogte
Arguments : ()
Expects answer? #t
Answer :
我发现真正奇怪的是答案是什么? 这怎么可能?我使用“读取”来读取传入的答案,这是一个阻塞操作。它怎么可能检测到一个答案(在这个例子中我假设是 15 个)(因为它停止了阻塞)但又产生“无”。
这种行为的原因可能是什么?未发送消息(在本例中为数字)可能是什么原因?