I'm working on Cloud Server's networking thing using Go programming language. And I faced a problem with reading same TCPconn at the same time from different Goroutines.
Here is a simple example
package main
import (
"fmt"
"net"
)
func main() {
addr, _ := net.ResolveTCPAddr("tcp", ":8888")
listener, _ := net.ListenTCP("tcp", addr)
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println(err)
return
}
go Handle(conn) // output always coming from here
go Handle(conn)
}
}
func Handle(conn *net.TCPConn) {
data := make([]byte, 1024)
fmt.Println("Ok")
for {
len_data, err := conn.Read(data)
fmt.Println(err)
fmt.Println(len_data)
}
}
In this example console output always coming from 1st goroutine go Handle(conn)
...
How to make Read functionality from 2 Goroutines at the same time ?
Thanks