Calling TCPConn.Write without any newline or null byte delimiter doesn't seem to do anything until the socket or writer is closed.
In this example I'd expect conn.Write to trigger the read on the server as soon as the write is complete, but nothing happens until the socket is closed. If I Write a string with a newline character before writing the ones without it works fine. Is this intended behavior? Are delimiters required? Or am I just missing something here..
Server
package main
import (
"log"
"net"
)
func main() {
addr, _ := net.ResolveTCPAddr("tcp4", ":8080")
l, err := net.ListenTCP("tcp4", addr)
if err != nil {
log.Fatal(err.Error())
}
defer l.Close()
for {
var conn *net.TCPConn
if conn, err = l.AcceptTCP(); err != nil {
log.Println(err.Error())
continue
}
log.Println("client connected")
go handleConnection(conn)
}
}
func handleConnection(conn *net.TCPConn) {
defer conn.Close()
for {
var b = make([]byte, 128)
bytesRead, err := conn.Read(b)
if err != nil {
break
}
log.Printf("got: %s
", string(b[:bytesRead]))
}
log.Println("client disconnected")
}
Client
package main
import (
"log"
"net"
"time"
)
func main() {
addr, _ := net.ResolveTCPAddr("tcp", "localhost:8080")
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
log.Fatal(err.Error())
}
// uncommenting the following line will make the following writes work as expected
//conn.Write([]byte("hello world
"))
for i := 0; i < 5; i++ {
conn.Write([]byte("hello"))
log.Println("wrote hello")
time.Sleep(time.Second)
}
time.Sleep(time.Second)
conn.Close()
}
Thanks for giving it a go, I ran it on a Ubuntu vm where it worked as expected. So, as of 1.5.1 for Windows, it seems to be a bug.
Edit:
Turns out it's Eset Smart Security's protocol filtering, go isn't the culprit
Running the code verbatim (client write hello world remains commented) shows:
Server:
2015/12/04 09:57:00 client connected
2015/12/04 09:57:00 got: hello
2015/12/04 09:57:01 got: hello
2015/12/04 09:57:02 got: hello
2015/12/04 09:57:03 got: hello
2015/12/04 09:57:04 got: hello
2015/12/04 09:57:06 client disconnected
Client:
2015/12/04 09:57:00 wrote hello
2015/12/04 09:57:01 wrote hello
2015/12/04 09:57:02 wrote hello
2015/12/04 09:57:03 wrote hello
2015/12/04 09:57:04 wrote hello
It seems to work as expected, the server outputs once per second, as the client writes.
This was tested on: go version devel +54bd5a7 Sat Nov 21 10:19:16 2015 +0000 linux/amd64
What go version are you running?