title: Whats the correct way to use net.Conn.Read for persistant TCP sockets
body: <p>I have a working TCP socket setup on my Go server. I accept an incoming connection, run a for loop and read incoming data using the net.Conn.Read function.</p>
  
  <p>But it just doesn't make sense to me. How does it know the full message has been received in order to continue with a return of message size?</p>
  
  <p>This is my code currently:</p>
  
  <pre><code> func (tcpSocket *TCPServer) HandleConnection(conn net.Conn) {
  
      println("Handling connection! ", conn.RemoteAddr().String(), " connected!")    
      recieveBuffer := make([]byte, 50) // largest message we ever get is 50 bytes
  
      defer func() {          
          fmt.Println("Closing connection for: ", conn.RemoteAddr().String())
          conn.Close()
      }()
  
      for {
          // how does it know the end of a message vs the start of a new one?
          messageSize, err := conn.Read(recieveBuffer) 
          if err != nil {
              return
          }
          if messageSize &gt; 0 { // update keep alive since we got a message
              conn.SetReadDeadline(time.Now().Add(time.Second * 5))    
          }
      }
  }
  </code></pre>
  
  <p>Lets say my application sends a message which is 6 bytes long (could be any size). How does <code>conn.Read</code> know when its received the end of said message to then continue?</p>
  
  <p>My experience mainly lies in C#, so Go is but unusual here. For my C# application the messages have the size of the message contained in first byte, then i use a for loop to read the remaining bytes up to message size. </p>
  
  <p>Yet the above code in Go seems to get the full message and continues - it some how automatically knows the size of my message?</p>
  
  <p>I am really confused how this is happening or if its just working by luck when i'm actually approaching it wrong. </p>
  
  <p>All my messages have the header in the first byte stating the size of the message. But it seems i don't need it on a Go server, am i misunderstanding how this works?</p>