On Mon, Oct 23, 2017 at 9:05 PM, Matt Mueller <mattmue...@gmail.com> wrote: > > Thanks for taking the time to respond Ian! If I'm understanding you > correctly, the client's socket is what's storing the buffer. When accept is > called, it looks in the queue of pending requests and it then creates a > matching socket on the server. Is that right?
Approximately. As I recall it creates the socket before accept is called, so the backlog is simply a queue of sockets, but I could be misremembering and it doesn't really matter. > The thing that's still throwing me is the following: > https://play.golang.org/p/YjxTKDYwnj > > func TestTCP(t *testing.T) { > ln, err := net.Listen("tcp", "127.0.0.1:0") > if err != nil { > panic(err) > } > > // mock server > connected := make(chan bool) > go func() { > conn, err := ln.Accept() > if err != nil { > panic(err) > } > > fmt.Printf("closing connection\n") > if e := conn.Close(); e != nil { > panic(e) > } > > fmt.Printf("closing listener\n") > if e := ln.Close(); e != nil { > panic(e) > } > > close(connected) > }() > > client, e := net.Dial("tcp", ln.Addr().String()) > if e != nil { > panic(e) > } > > <-connected > > fmt.Printf("writing...\n") > n, e := client.Write([]byte("hi world!")) > if e != nil { > panic(e) > } > fmt.Printf("wrote %d bytes\n", n) > } > > Results in: > > closing connection > closing listener > writing... > wrote 9 bytes > > > > Playground link: https://play.golang.org/p/YjxTKDYwnj > > Anyone know what's going on here? I would have thought that write would > return EOF after closing the connection, but at the very least, after > closing the listener. TCP is designed for communication across a wide network. Closing the server side of a socket does not immediately close the client side; how could it? The server side does send a FIN packet to the client, but in your program presumably the write is happening before that packet is processed. If you want to really understand this I recommend the TCP/IP Illustrated books. They are fairly old but still good. Ian -- 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. For more options, visit https://groups.google.com/d/optout.