zeromq / goczmq

goczmq is a golang wrapper for CZMQ.
Mozilla Public License 2.0
591 stars 94 forks source link

Thread safe channel interface example problem #281

Open wozuo opened 4 years ago

wozuo commented 4 years ago

Hi everyone, thanks for this great project. I'm testing out the examples right now and ran into some problems. When executing the "Thread safe channel interface" example from the readme everything runs just fine. I get the following expected output:

router created and bound
dealer created and connected
dealer sent 'Hello'
router received 'Hello' from '[0 128 0 65 167]'
router sent 'World'
dealer received 'World'

But when I try to separate the client from the server, the serve does not receive the messages from the client any longer. The modified code which does not seem to work, looks like this:

func main() {
    args := os.Args[1:]

    if len(args) != 1 {
        fmt.Println("Specify run mode (either client or server)")
        return
    }

    switch args[0] {
    case "client":
        fmt.Println("Starting client...")
        startClient()
    case "server":
        fmt.Println("Starting server...")
        startServer()
    default:
        fmt.Println("Specify run mode (either client or server) ")
    }
}

func startServer() {
    router := goczmq.NewRouterChanneler("tcp://*:5555")
    defer router.Destroy()

    for {
        fmt.Println("Receiving message...")
        request := <-router.RecvChan

        fmt.Printf("router received '%s' from '%v'", request[1], request[0])
    }
}

func startClient() {
    dealer := goczmq.NewDealerChanneler("tcp://127.0.0.1:5555")
    defer dealer.Destroy()

    dealer.SendChan <- [][]byte{[]byte("Hello")}
    fmt.Println("Client sent hello")
}

And the output I get on the server side is this:

Starting server...
Receiving message...

And then is just hangs there forever

And the output I get on the client side is this:

Starting client...
Client sent hello

after which the program quits

Does anybody know what the problem is here? Thanks a best regards Marc