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

我正在尝试构建一种基本的客户端/服务器体系结构,在该体系结构中,两者之间还有一些数据交换,并且 双方都有一些处理。 因此,我的服务器有两个线程正在向客户端发送一些数据。 我想知道如何分别将数据分为两个不同的变量。</ p>

尽管我仍然不确定,但是这与竞态条件,互斥量等概念有关 锁等。我对它们有一个基本的想法,但从未实际使用它们。 我想知道是否有一些针对此问题的预先设计的解决方案。</ p>

服务器端:</ p>

  func handleConn(conn net。  Conn){
go func(){
io.WriteString(conn,“文本1”)
}()
go func(){
io.WriteString(conn,“文本2”)
}()
}
</ code> </ pre>

客户端:</ p>

  func SocketClient(IP字符串,端口字符串){

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

延迟conn.Close()\ n
if err!= nil {
log.Fatalln(err)
}

buff:= make([] byte,1024)
n,_:= conn.Read(buff)
log .printf(“ Received:%s”,buff [:n])
}
</ code> </ pre>

“文本1”和“文本2”都由 可变的增益 我想让它像有两个单独的变量buff1和buff2分别保存两个文本。</ p>
</ div>

展开原文

原文

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.

douzhe2981
douzhe2981 Wikipedia和Google以及IETF都是非常可靠的资源。“标准协议”的范围非常广,涵盖了数百个彼此之间几乎没有相似之处。几乎每个“标准协议”都可能会过分满足您的需求,只需写下您需要的内容即可。
一年多之前 回复
dongzexi5125
dongzexi5125 那么,您能否提供一些我应该学习@Adrian的标准协议的资源?
一年多之前 回复
dongzhang6021
dongzhang6021 此时,您基本上正在编写自定义协议,这意味着您需要协议设计。您的客户端将需要某种方式来知道有效负载的哪一部分,以及它们之间的边界在哪里,并且它将需要实现该逻辑。
一年多之前 回复

1个回答

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
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐