With TCP a successful write means the bytes are now managed by your
network stack. If there are no permanent errors, then it will get
those bytes to the other end of the TCP connection eventually. A
successful write is not a receipt from the other end.

On Sun, Oct 22, 2017 at 4:50 AM, Matt Mueller <mattmue...@gmail.com> wrote:
> Hey everyone!
>
> This question cropped up while testing. It's 2 related questions:
>
> 1. Why am I able to write to without accepting a connection?
>
>   addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
>   if err != nil {
>     t.Fatal(err)
>   }
>
>   ln, err := net.ListenTCP("tcp", addr)
>   if err != nil {
>     t.Fatal(err)
>   }
>   defer ln.Close()
>
>   raddr, err := net.ResolveTCPAddr("tcp", ln.Addr().String())
>   if err != nil {
>     t.Fatal(err)
>   }
>
>   client, e := net.DialTCP("tcp", nil, raddr)
>   if e != nil {
>     t.Fatal(e)
>   }
>
>   if n, e := client.Write([]byte("hi world!")); e != nil {
>     fmt.Printf("error writing %s\n", e)
>   } else {
>     fmt.Printf("wrote %d bytes\n", n)
>   }
>
> This will come back as: "wrote 9 bytes"
> - https://play.golang.org/p/HmiRu-9xEv
>
> 2. How would I only allow it to write what it's able to read? In this
> example, the server connection only reads 1 byte before closing. This would
> be useful for testing flaky connections and retry logic:
>
> addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
> if err != nil {
>   t.Fatal(err)
> }
>
> ln, err := net.ListenTCP("tcp", addr)
> if err != nil {
>   t.Fatal(err)
> }
> defer ln.Close()
>
> // mock server
> go func() {
>   for {
>     s, e := ln.AcceptTCP()
>     if e != nil {
>       t.Fatal(e)
>     }
>
>     buf := make([]byte, 1)
>     if n, e := s.Read(buf); e != nil {
>       t.Fatal(e)
>     } else {
>       fmt.Printf("read %d bytes\n", n)
>     }
>
> // close before reading everything
>     s.Close()
>   }
> }()
>
> // make the connection to our little server
> raddr, err := net.ResolveTCPAddr("tcp", ln.Addr().String())
> if err != nil {
>   t.Fatal(err)
> }
>
> client, e := net.DialTCP("tcp", nil, raddr)
> if e != nil {
>   t.Fatal(e)
> }
>
> if n, e := client.Write([]byte("hi world!")); e != nil {
>   fmt.Printf("error writing %s\n", e)
> } else {
>   fmt.Printf("wrote %d bytes\n", n)
> }
>
> This will come back as: "wrote 9 bytes", "read 1 bytes"
> - https://play.golang.org/p/-T2UunLy5T
>
> Thanks for the help in advance!
>
> Matt
>
> --
> 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.

-- 
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.

Reply via email to