douruhu4282 2019-02-26 12:12
浏览 28

Conn在Go中随机关闭且没有警告

I'm building an echo algorithm in go with multiple initiators, essentially I have three functions "talk", "listen" and "run algorithm". "talk" sends data to neighbours, "listen" received data from neighbours, both of these use channels. These channels are monitored within the "run algorithm" functions which decides what to send and to whom.

Essentially this:

func server(s Node, myChannel chan string){
fmt.Printf("SERVER Launching server... %s:%s 
", s.ip, s.port)

// Start listening

// We do not stop listening until the surrounding function returns (finishes)
ln, _ := net.Listen("tcp", s.ip+":"+s.port)
for {

    // Accept connections
    fmt.Println("SERVER waiting at ln.Accept()")
    conn, err := ln.Accept()

    if err != nil {
        panic(err)
    }

    //fmt.Println("SERVER connection accepted")

    // Read received message
    fmt.Println("SERVER awaiting message")
    message, err := bufio.NewReader(conn).ReadString('
')
    fmt.Println("SERVER message received = " + message)
    if err != nil {
        fmt.Println("the server is broken")
        panic(err)
    }

    if err == nil {

        // If the mmessage is not empty, then print it in the shell

        if string(message) != "" {
            fmt.Println("SERVER message put in channel")
            myChannel <- message

        }
    }
}

And the talking function as this:

func client(myNode Node, nbour Node, myChannel chan string) {
    for {
    // this loop controls the sending of algorithm messages

    conn, err = net.Dial("tcp", nbour.ip+":"+nbour.port)

    // useful info for the user
    //fmt.Println("CLIENT awaiting message to send")

    // the the message to send out of the channel
    messageToSend := <-myChannel

    // send the message
    conn.Write([]byte(messageToSend))

    fmt.Println("CLIENT SENT ALG MESSAGE = " + messageToSend + "to " + nbour.ip + ":" + nbour.port)

    // useful information for the user
    //fmt.Printf("CLIENT Message text: " + messageToSend. + "
")


}

The problem is that these connections work find for the first few messages, then at some arbitrary point one node sends a message and the neighbour does not receive it and everything stops.

Thus my question is why do connections arbitrarily stop, is there a setting somewhere to tell them how long to stay open, or how many messages to send? Or is the above code the wrong was of achieving this goal (although like I say it works fine sometimes, in fact sometimes the algorithm completes perfectly, then I run exactly the same code again and it gets stuck waiting for messages to arrive).

  • 写回答

1条回答 默认 最新

  • duanmei9980 2019-02-26 14:37
    关注

    So it appears that making a connection and THEN waiting for something to be put in the channel is causing the problem. If you instead wait for something to be put into the channel THEN (once something is removed form the channel) make the connection (and close the connection as suggested by @leaf bebop) the code runs fine every time. (see below)

    func client(myNode Node, nbour Node, myChannel chan string) {
    for {
    // this loop controls the sending of algorithm messages
    
    // useful info for the user
    //fmt.Println("CLIENT awaiting message to send")
    
    // the message to send out of the channel
    messageToSend := <-myChannel
    
    conn, err = net.Dial("tcp", nbour.ip+":"+nbour.port)
    
    // send the message
    conn.Write([]byte(messageToSend))
    conn.Close()
    
    fmt.Println("CLIENT SENT ALG MESSAGE = " + messageToSend + "to " + nbour.ip + ":" + nbour.port)
    
    // useful information for the user
    //fmt.Printf("CLIENT Message text: " + messageToSend. + "
    ")
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目