title: C# to Java UTF8 string over TCP
body: <p>I'm unable to send a UTF-8 string from a C# server to a Java client due to an EOF error in the client. How do I properly configure the C# server? I assume the error lies there because this client works with the Java server shown below.</p>
  <p>Java client's receive function does this (this also works if I receive from a <em>Java</em> server, shown below):</p>
  <pre><code>DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); //The constructor initialises a field, using the socket object.        
  StringBuilder inputMessage = new StringBuilder();
  inputMessage.append((String) dataInputStream.readUTF());
  </code></pre>
  <p>Desired C# server:</p>
  <pre><code>        static async Task Main(string[] args)
          {
              TcpListener server = new TcpListener(IPAddress.Any, 34567);
              server.Start();
              byte[] bytes = new byte[4096];
              byte[] responseBytes;
              using (var client = await server.AcceptTcpClientAsync()){
                  using(var tcpStream = client.GetStream())
                  {
                      await tcpStream.ReadAsync(bytes, 0, bytes.Length);
                      var playerNumber = Encoding.UTF8.GetString(bytes);
                      Console.WriteLine(&quot;Player &quot; + playerNumber + &quot; connected.&quot;); //java client to server works.
  
                      StringBuilder outputMessage = new StringBuilder(&quot;Some output&quot;);
                      responseBytes = Encoding.UTF8.GetBytes(outputMessage.ToString());
                      await tcpStream.WriteAsync(responseBytes, 0, responseBytes.Length); //This doesn't work...
                  }
                  server.Stop();
              }
          }
  </code></pre>
  <p>The error:</p>
  <pre><code>java.io.EOFException
      at java.base/java.io.DataInputStream.readFully(DataInputStream.java:201)
      at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:613)
      at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:568)
      at Client.Connection.Receive(Connection.java:26)
      at Client.Main.lambda$main$0(Main.java:30)
      at com.sun.javafx.application.PlatformImpl.lambda$startup$5(PlatformImpl.java:271)
      at com.sun.glass.ui.Application.invokeAndWait(Application.java:464)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runToolkit(QuantumToolkit.java:366)
      at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$startup$10(QuantumToolkit.java:280)
      at com.sun.glass.ui.Application.lambda$run$1(Application.java:153)
  </code></pre>
  <p>Interestingly, a <em>Java server</em> doing this works:</p>
  <pre><code>            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
              StringBuilder outputMessage = new StringBuilder(&quot;Some output&quot;);
              dataOutputStream.writeUTF(outputMessage.toString());
              dataOutputStream.flush();
  </code></pre>
  <p>EDIT</p>
  <p>This is received from the working Java server. The &quot;bytearr&quot; contains 100 bytes that I am using for my message and 100 bytes that are 0 (they come after my message). The &quot;chararr&quot; correctly sees the first 100 bytes as something meaningful and the last 200 bytes as '\u0000':
  <a href="https://i.stack.imgur.com/HhMqN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HhMqN.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/Ka2t7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ka2t7.png" alt="enter image description here" /></a></p>
  <p>This is received form the non-working C# server. It seems to start <strong>two bytes in</strong> compared to the correct version and also it's &quot;chararr&quot; contains only thousands of '\u0000':
  <a href="https://i.stack.imgur.com/YCBYH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YCBYH.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/Qmq6D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qmq6D.png" alt="enter image description here" /></a></p>