title: Golang TCP Client Does Not Send `End of Stream` to Java TCP Server
body: <p>I'm writing a simple Golang TCP Client that talks with a Java TCP Server.</p>
  
  <p>I have successfully gotten my Golang Client to send a message to my Java Server.</p>
  
  <p>However, my Java code is expecting an <code>End of Stream</code> (where <code>inputStream.read()</code> returns <code>-1</code>) to know that it's time to stop reading Client messages.</p>
  
  <p>It looks like Golang does not send an <code>End of Stream</code> message via <code>connection.Write()</code> unless I <code>Close()</code> the connection first.</p>
  
  <p>Below is my Java Server code:</p>
  
  <pre><code>package com.mycompany.app;
  
  import com.google.gson.*;
  import com.google.common.primitives.Bytes;
  import java.io.*;
  import java.net.Socket;
  import java.util.ArrayList;
  import java.util.List;
  import java.net.ServerSocket;
  import java.net.SocketException;
  
  public class MyApp {
      public static void main(String[] args) {
          int port = 9000;
  
          // Start listening for messages.
          try (ServerSocket serverSocket = new ServerSocket(port)) {
              System.out.println("Listening on port " + port + "...");
  
              // Never stop listening for messages.
              while (true) {
                  try {
                      // Accept a client connection.
                      Socket socket = serverSocket.accept();
  
                      // Read message from the client.
                      DataInputStream inputStream = new DataInputStream(socket.getInputStream());
  
                      List&lt;Byte&gt; list = new ArrayList&lt;Byte&gt;();
                      int input = inputStream.read();
  
                      while (input != -1) {
                          list.add((byte) input);
                          input = inputStream.read();
                      }
  
                      byte[] jsonBytes = Bytes.toArray(list);
  
                      if (jsonBytes.length &gt; 0) {
                          String jsonString = new String(jsonBytes);
  
                          System.out.println("Received: " + jsonString);
  
                          // Unmarshal from JSON.
                          Person person = new Gson().fromJson(jsonString, Person.class);
  
                          System.out.println("Person: " + person.Name + " " + person.Age);
                      }
                  } catch (EOFException e) {
                      // A client has disconnected. Do nothing.
                  } catch (SocketException e) {
                      // A client connection has been terminated unexpectedly. Do nothing.
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  
  class Person {
      String Name;
      int Age;
  }
  </code></pre>
  
  <p>Below is my Golang Client code:</p>
  
  <pre><code>package main
  
  import (
      "bufio"
      "encoding/json"
      "fmt"
      "net"
      "os"
      "strings"
  )
  
  type Person struct {
      Name string
      Age  int
  }
  
  func main() {
      reader := bufio.NewReader(os.Stdin)
  
      for {
          func() {
              // Dial the Java Server.
              conn, err := net.Dial("tcp", "localhost:9000")
              if err != nil {
                  fmt.Println(err)
              }
  
              defer conn.Close()
  
              fmt.Print("Enter Name: ")
              strName, err := reader.ReadString('\n')
              strName = strings.TrimSpace(strName)
  
              var person Person
              person.Name = strName
              person.Age = 18
  
              jsonBytes, err := json.Marshal(person)
              if err != nil {
                  fmt.Println(err)
                  return
              }
  
              // Write our message to the connection.
              _, err = conn.Write(jsonBytes)
              if err != nil {
                  fmt.Println(err)
                  return
              }
  
              fmt.Println("Sent: " + string(jsonBytes))
          }()
      }
  }
  </code></pre>
  
  <p>Any advice on how to tell Java that we're done writing messages from Golang? Or is there a better way to handle Java x Golang TCP Client-Server connections?</p>
  
  <p>This is quite a problem because one of the basic scenarios for TCP connections is Send-and-Receive:</p>
  
  <p>e.g.</p>
  
  <ol>
  <li><p>Client sends a message and waits for the result. </p></li>
  <li><p>Server processes the message and returns the result.</p></li>
  <li><p>Client does something with the result.</p></li>
  </ol>