I am writing a program which needs communication between processes.
my code:
#lang racket
(define-values (sp o i e) (subprocess #f #f #f "c://player1.exe" ))
(define count 10)
(for ([c (in-naturals)])
(cond
[(equal? count 0) (error "Province is empty!") ]
[else
(write "server" i)
(set! count (sub1 count))
(flush-output i)
(display (read o))]))
and the player1.exe code:
#lang racket
(define (interact notification)
(cond
[(eq? notification "server") (write "true" (current-output-port))]
[else (write "false" (current-output-port))]))
(for ([c (in-naturals)])
(interact (read (current-input-port)))
(write "player" (current-output-port))
(sleep 0.1)
flush-output (current-output-port))
I am getting output if I run without loops. I am also getting output when only player is sending messages. But with both server and player sending messages the program gets hanged.
What do you think the problem is?
The last line in your
player1.exefile looks suspicious.flush-outputis not actually being applied as a function. Rather thanyou probably mean:
From a style point of view: the functions
read,write, andflush-outputall work on the current input and output ports by default, so you don’t need to provide them. Take a look at the documentation for those functions, such asflush-output, and you’ll see that it mentions that thecurrent-output-portis its default.So the line that we just looked at can be written as:
More issues: don’t use
eq?to compare strings. Usestring=?instead. The reason is that there can be two strings that have the same textual content, but for whicheq?will still be able to distinguish the two. e.g.: