duanpanyang1962 2019-04-04 15:56
浏览 73
已采纳

如何从服务器上运行的两个并发进程分别接收数据?

I am trying to build a basic client/server architecture in which there is some exchange of data between the two and also some processing on both sides. So my server has two threads that are sending some data to the client side. I want to know how to receive this data separately into two different variables.

I have learnt, although I am still not sure, that this has something to do with concepts like race condition, mutex lock etc. I have a basic idea about them but have never used them practically. I want to know if there is some pre-designed solution regarding this problem.

Server Side:

func handleConn(conn net.Conn) {
    go func() {
        io.WriteString(conn, "Text 1")
    }()
    go func() {
        io.WriteString(conn, "Text 2")
    }()
}

Client Side:

func SocketClient(ip string, port string) {
    addr := strings.Join([]string{ip, port}, ":")
    conn, err := net.Dial("tcp", addr)

    defer conn.Close()

    if err != nil {
        log.Fatalln(err)
    }

    buff := make([]byte, 1024)
    n, _ := conn.Read(buff)
    log.Printf("Received: %s", buff[:n])
}

Both "Text 1" and "Text 2" are read by the variable buff. I want to make it like there are two separate variables buff1 and buff2 to hold both texts separately.

  • 写回答

1条回答 默认 最新

  • dongxian3418 2019-04-04 17:13
    关注

    You read the connection just once and you should expect to receive the whole data in one call. You can separate your messages with a delimiter like newline and in a loop read the data by that delimiter. This way it's like you received two different messages . I made a sample for you to check out.

    package main
    
    import (
        "bufio"
        "io"
        "log"
        "net"
        "sync"
    )
    
    func handleConn(conn net.Conn) {
        go func() {
            io.WriteString(conn, "Text 1
    ")
        }()
        go func() {
            io.WriteString(conn, "Text 2
    ")
        }()
    }
    
    func SocketClient() {
        conn, err := net.Dial("tcp", ":3000")
        if err != nil {
            log.Fatal(err)
        }
        defer conn.Close()
        if err != nil {
            log.Fatalln(err)
        }
        reader := bufio.NewReader(conn)
        for {
            buff, err := reader.ReadString('
    ')
            if err != nil {
                log.Fatalln(err)
            }
            log.Printf("Received: %s", buff)
        }
    }
    
    func main() {
        wg := &sync.WaitGroup{}
        wg.Add(1)
        go func() {
            a, _ := net.Listen("tcp", ":3000")
            wg.Done()
            for {
                conn, err := a.Accept()
                if err != nil {
                    log.Fatalln(err)
                }
                handleConn(conn)
            }
        }()
        wg.Wait()
        SocketClient()
    }
    

    Output:

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么