如何在golang中的单个TCP连接上同时进行读写?

I try to create a sigle TCP connection client used for multiple write and read concurrently. For example the TCP Server will return value like you write. The problem is the data exchanged between write and read. I have tried with sync.Mutex but it still doesn't work.

My Result is :

2018/03/10 12:52:10 STRING 4:1
2018/03/10 12:52:10 STRING 5:6
2018/03/10 12:52:10 STRING 2:3
and so on

My Expectation is :

2018/03/10 12:52:10 STRING 4:4
2018/03/10 12:52:10 STRING 5:5
2018/03/10 12:52:10 STRING 2:2
2018/03/10 12:52:10 STRING 3:3

Here's my code : package main

import (
    "bufio"
    "log"
    "net"
    "strconv"
    "sync"
    "time"
)

type Cc struct {
    mux sync.Mutex
    rw  *bufio.ReadWriter
}

func main() {
    addr := "127.0.0.1:3333"
    log.Println("Dial " + addr)
    conn, err := net.Dial("tcp", addr)
    cc := Cc{
        rw: bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)),
    }

    if err != nil {
        log.Println(err)
    }
    for i := 1; i <= 10; i++ {
        go testC(cc, strconv.Itoa(i))
    }

    time.Sleep(60 * time.Second)

}

func testC(cc Cc, st string) {
    cc.mux.Lock()
    defer cc.mux.Unlock()
    _, err := cc.rw.WriteString(st + "
")
    if err != nil {
        log.Println(err)
        // return errors.Wrap(err, "Could not send additional STRING data ("+strconv.Itoa(n)+" bytes written)")
    }
    // log.Println("Flush the buffer."s)
    err = cc.rw.Flush()
    if err != nil {
        log.Println(err)
        // return errors.Wrap(err, "Flush failed.")
    }

    time.Sleep(3 * time.Second)

    // Read the reply.
    // log.Println("Read the reply.")
    response, err := cc.rw.ReadString('
')
    if err != nil {
        log.Println(err)
        // return errors.Wrap(err, "Client: Failed to read the reply: '"+response+"'")
    }

    log.Println("STRING " + st + ":" + response)
    // cc.mux.Unlock()
}

Sorry for my bad English, Thank You.

查看全部
douqiaolong0528
douqiaolong0528
2018/03/10 06:10
  • mutex
  • tcpclient
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复