title: Java sending handshake packets to minecraft server
body: <p>I have been working on a java program that basically acts like Minechat(text-based app to just view chat.) I have never really worked with networking too much, so the issue is figuring out how to send packets correctly. I am currently at the position of creating the handshake with the server. After hours of research, I have come up with the following code, but it always runs into the &quot;Failed! (Exception)&quot; message. To me, everything looks correct, but for all I know it could be 100% wrong. If someone could point out what I'm doing wrong here, I'd really appreciate it.</p>
  <p>For reference, feel free to use <a href="http://wiki.vg/Protocol#Packet_format" rel="nofollow noreferrer">this</a> and <a href="http://wiki.vg/Protocol#Handshake" rel="nofollow noreferrer">this</a>.</p>
  <pre><code>public static void main(String[] args) throws IOException {
      host = new InetSocketAddress(&quot;162.244.165.111&quot;, 48040);
      socket = new Socket();
      System.out.println(&quot;Connecting...&quot;);
      socket.connect(host, 3000);
      System.out.println(&quot;Done!&quot;);
      System.out.println(&quot;Making streams...&quot;);
      output = new DataOutputStream(socket.getOutputStream());
      input = new DataInputStream(socket.getInputStream());
      System.out.println(&quot;Done!&quot;);
      System.out.println(&quot;Attempting handshake... &quot;+host.getAddress().toString().substring(1));
      byte[] msg = (&quot;47;&quot;+host.getAddress().toString().substring(1)+&quot;;&quot;+host.getPort()+&quot;;2;&quot;).getBytes(Charset.forName(&quot;UTF-16&quot;));
      output.writeInt(msg.length+Integer.valueOf(0x00));
      output.writeByte(0x00);
      output.write(msg);
      output.flush();
      try {
          if (input.readByte() != 0x02)
              System.out.println(&quot;Failed!&quot;);
          else
              System.out.println(&quot;Done!&quot;);
      } catch (EOFException e) {
          System.out.println(&quot;Failed! (Exception)&quot;);
      }
  }
  </code></pre>
  <p>EDIT:
  More research suggests I use a Byte array, but this confuses me on how to represent a string and using strings is required?</p>