On 10/22/2017 04:50 AM, Matt Mueller 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?*
> 

As I understand it, by the time you accept() a connection, the TCP
stream has already been established.  The linux man page for listen()
says this about the backlog parameter:

  Now it specifies the queue length for completely established sockets
  waiting to be accepted, instead of the number of incomplete connection
  requests.

So I think we're talking about two things:  establishing a TCP
connection and building the plumbing to read from it.

> *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:*
> 

I think once you've established a TCP strean -- that is by the time
net.Dial() (and I guess the underlying connect() system call) unblocks,
you should have a working stream that you can write on.

The server has to choose to accept and read from the descriptor.

To answer why you only read one byte:

Because you only make your receiving buffer (buf) in the server 1 byte.

Instead of:

buf := make([]byte, 1)

try:

buf := make([]byte, 0, 20)

or read into your buf using a loop.

-ayan


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