title: Python server write UTF to Java client over socket
body: <p>I am making a Python server for a third-party Java client. The client uses <code>DataInputStream.readUTF()</code> and I was wondering how I could write a UTF in Python over a socket to Java.</p>
  
  <p>Server current code:</p>
  
  <pre><code>import socket
  port = 3687
  
  def main():
      print("Running on port " + str(port))
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind(("", port))
      s.listen(1)
      c, a = s.accept()
      c.send(str(unichr(1))) #Packet id
      c.send("Hello".encode('utf-8')) #MOTD
      c.send(str(unichr(0)) + str(unichr(3))) #Players
      c.close()
      s.close()
      print("Complete")
  
  main()
  </code></pre>
  
  <p>Client code (Can't be changed):</p>
  
  <pre><code>Socket s = new Socket();
  s.setKeepAlive(false);
  s.setSoTimeout(3000);
  s.connect(new InetSocketAddress(address,port), 3000);
  s.getOutputStream().write(0);
  DataInputStream in = new DataInputStream(s.getInputStream());
  if(in.read() != 1)
      throw new Exception("Invalid response");
  serverMOTD = in.readUTF());
  playersOnline = in.readChar();
  s.close();
  </code></pre>
  
  <p>The client crashes when it reads the UTF with an error of:</p>
  
  <pre><code>java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:168)
      at java.io.DataInputStream.readFully(DataInputStream.java:178)
      at java.io.DataInputStream.readUTF(DataInputStream.java:592)
      at java.io.DataInputStream.readUTF(DataInputStream.java:547)
  </code></pre>