duanha3539 2014-05-05 11:50
浏览 11
已采纳

如何在net.Con中正确地对消息进行分块并防止广播

Updated with solution: http://play.golang.org/p/Skgk9reT6c

http://play.golang.org/p/gtWYPXRsKo

without goroutines: http://play.golang.org/p/Vgne7e3RVO

Question 1: Why is it that the message "YOU'LL NEVER SEE ME!" doesn't display?

Question 2: As soon as I initiate a client I send these messages, all on their own threads:

func client() {
  net, _ := net.Dial("tcp", "127.0.0.1:9988")
  go clientBroadcast(net,"123456789101112")
  go clientBroadcast(net,"do dododo do dodo do")
  go clientBroadcast(net,"123456789101112")
  go clientBroadcast(net,"do dododo do dodo do")
  go clientBroadcast(net,"123456789101112")
  go clientBroadcast(net,"TWELVE!")
  time.Sleep(100 * time.Millisecond)
  go clientBroadcast(net,"YOU'LL NEVER SEE ME!")
}

The server receives them and prints them out:

func serverBroadcast(con net.Conn, ch chan string) {
  buf := make([]byte, 1024)
  bytenum, _ := con.Read(buf)
  strin := string(buf[0:bytenum])
  fmt.Printf("
 Server Broadcasting Message {%v}
",string(strin))
}

How is it that the server is apparently receiving all 4 messages at once and not one at a time?

I'm getting this message:

Server Broadcasting Message {123456789101112do dododo do dodo do123456789101112do dododo do dodo do123456789101112TWELVE!}

Server Broadcasting Message {123456789101112do dododo do dodo do123456789101112do dododo do dodo do123456789101112TWELVE!}

Server Broadcasting Message {123456789101112do dododo do dodo do123456789101112do dododo do dodo do123456789101112TWELVE!}

However I would have assumed I should have received this (in some random order as they are all threaded)

Server Broadcasting Message {123456789101112}

Server Broadcasting Message {do dododo do dodo}

Server Broadcasting Message {123456789101112}

Server Broadcasting Message {do dododo do dodo}

Server Broadcasting Message {TWELVE!}

Server Broadcasting Message {123456789101112}

Server Broadcasting Message {do dododo do dodo}

Server Broadcasting Message {123456789101112}

Server Broadcasting Message {do dododo do dodo}

Server Broadcasting Message {TWELVE!}

Server Broadcasting Message {123456789101112}

Server Broadcasting Message {do dododo do dodo}

Server Broadcasting Message {123456789101112}

Server Broadcasting Message {do dododo do dodo}

Server Broadcasting Message {TWELVE!}

  • 写回答

1条回答 默认 最新

  • doufuxi7093 2014-05-05 12:38
    关注

    The problem is that in this function you aren't reading all the data available on the stream. You'll need to call Read more than once to get all the data.

    func serverBroadcast(con net.Conn, ch chan string) {
        buf := make([]byte, 1024)
        bytenum, _ := con.Read(buf)
        strin := string(buf[0:bytenum])
        fmt.Printf("
     Server Broadcasting Message {%v}
    ", string(strin))
    }
    

    However if you do that you'll find it will block, (playground), eg

    func serverBroadcast(con net.Conn, ch chan string) {
        buf := make([]byte, 1024)
        for {
            bytenum, err := con.Read(buf)
            if err != nil {
                fmt.Printf("Err %v", err)
                return
            }
            strin := string(buf[0:bytenum])
            fmt.Printf("
     Server Broadcasting Message {%v}
    ", string(strin))
        }
    }
    

    You need to delimit your messages over TCP, or send a count before the message so know where the messages start and end if you want to avoid blocking

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢