I am trying to make a server and client in Go, I have managed to communicate with the server and client. But the problem I have is that the TCP read in golang is non-blocking. What I would like to know if is it possible for the read in golang to be blocking like read in C. Thank You
EDIT:
Here is the source code of the server:
func Init_tcp() *net.TCPListener {
laddr, err := net.ResolveTCPAddr("tcp", ":4243")
if err != nil {
log.Fatal(err)
}
tcp, err := net.ListenTCP("tcp", laddr)
if err != nil {
log.Fatal(err)
}
return tcp
}
func main() {
tcp := Init_tcp()
conn, _ := tcp.Accept()
data := make([]byte, 512)
conn.SetNoDelay(false)
for {
conn.Read(data)
fmt.Println(data)
}
}
and my client:
func Init_tcp() *net.TCPConn {
laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:4243")
if err != nil {
log.Fatal(err)
}
tcp, err := net.DialTCP("tcp", nil, laddr)
if err != nil {
log.Fatal(err)
}
return tcp
}
func main() {
tcp := Init_tcp()
tcp.Write([]byte("hello world"))
}