updated. 
the order of calls to read and write does not matter, read and write will not 
block each other.
the same handleConnection function can be used on both server and client sides.
the only difference is the message content which is read and written.


```




func handleConnection(conn net.Conn, tag string) {
        defer conn.Close()


        msgID := 0
        reader := bufio.NewReader(conn)
        writer := bufio.NewWriter(conn)
        readChannel := make(chan struct{})
        writeChannel := make(chan struct{})


        go func() {
                for {
                        line, err := reader.ReadString('\n')
                        if err != nil {
                                log.Println(err, line)
                                close(readChannel)
                                break
                        }
                        fmt.Print(line)
                }
        }()


        go func() {
                for {
                        msgID++
                        msg := fmt.Sprintf("msg from client: %s, msgID: %d\n", 
tag, msgID)
                        if n, err := writer.WriteString(msg); err != nil {
                                log.Println(err, n)
                                close(writeChannel)
                                break
                        }
                        writer.Flush()
                }
        }()


        if _, ok := <-readChannel; !ok {
                return
        }
        if _, ok := <-writeChannel; !ok {
                return
        }
}





```

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/tencent_31565E40ECF4907B7E6AB00146A8B1F3A406%40qq.com.

Reply via email to