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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制