title: ESP8266 and Android socket won't communicate
body: <p>I am having issues getting my Android device and an ESP8266 communicate using sockets. Here is the code for the ESP8266:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;Arduino.h&gt;
  #include &lt;ESP8266WiFi.h&gt;
  #include &lt;ESP8266WiFiMulti.h&gt;
  #include &lt;WebSocketsServer.h&gt;
  #include &lt;ESP8266WebServer.h&gt;
  #include &lt;ESP8266mDNS.h&gt;
  #include &lt;Hash.h&gt;
  
  ESP8266WebServer server = ESP8266WebServer(80);
  WebSocketsServer webSocket = WebSocketsServer(81);
  
  void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
    switch (type) {
      case WStype_DISCONNECTED:
        Serial.printf("[%u] Disconnected!\n", num);
        break;
      case WStype_CONNECTED: {
          IPAddress ip = webSocket.remoteIP(num);
          Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  
          // send message to client
          webSocket.sendTXT(num, "Connected");
        }
        break;
      case WStype_TEXT:
        IPAddress ip = webSocket.remoteIP(num);
        Serial.printf("$%s?\n", payload);
        webSocket.sendTXT(num, payload, sizeof(payload), false);
        break;
    }
  }
  
  void setup() {
    Serial.begin(115200);
    Serial.println();
    Serial.println();
    Serial.println();
    for (uint8_t t = 4; t &gt; 0; t--) {
      Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
      Serial.flush();
      delay(1000);
    }
    Serial.println("Starting AP");
    WiFi.mode(WIFI_AP);
    WiFi.softAP("Alpine_SIM_CNTRL", "12345678");
    Serial.println("AP Started");
  
    Serial.println("Starting Socket");
    // start webSocket server
    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
    Serial.println("Socket Started");
  
    if (MDNS.begin("simcontrol")) {
      Serial.println("MDNS responder started");
    }
    Serial.println("Starting Server");
    //handle index
    server.on(
    "/", []() {
      server.send(200, "text/plain", "You are connected");
    });
  
    server.begin();
    Serial.println("Server Started");
  
    //Add service to MDNS
    MDNS.addService("http", "tcp", 80);
    MDNS.addService("ws", "tcp", 81);
  }
  
  void loop() {
    webSocket.loop();
    server.handleClient();
  }
  </code></pre>
  
  <p>And below is the Java code for the socket for Android:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class WriteToServer extends AsyncTask&lt;MyTaskParams, Void, String&gt; {
    Context context;
    Socket socket;
  
    public WriteToServer(MyTaskParams Params) {
    }
  
    @Override
    protected String doInBackground(MyTaskParams... params) {
      socket = null;
      DataInputStream in = null;
      DataOutputStream out = null;
      context = params[0].context;
      String command = params[0].cmd;
      String response = null;
      try {
        socket = new Socket("192.168.4.1", 81);
        out = new DataOutputStream(socket.getOutputStream());
        in = new DataInputStream(socket.getInputStream());
        out.writeUTF(command);
        Log.i("Command", "Command sent: " + command);
        while (in.available() &gt; 0) {
          response = in.readUTF();
        }
      } catch (IOException e) {
        e.printStackTrace();
        Log.e("Error", "There was an error writing to the socket. Thrown IO exception");
      } finally {
        if (socket != null) {
          try {
            Log.i("INFO", "closing the socket");
            socket.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        if (in != null) {
          try {
            in.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        if (out != null) {
          try {
            out.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
      return response;
    }
  }
  </code></pre>
  
  <p>Can anyone please point out why I am not getting any communication? Using the debugger in Android Studio I can see that the socket is connected to the correct address and port, but when I send something to the ESP it does not print out what I sent or respond back. If I connect to the ESP with my PC and type in 192.168.4.1:81/*** I get a response that states "This is a websocket server only". So I think the socket server should be up and running based on this. Any help would be appreciated.</p>
  
  <p><strong>[EDIT]</strong>
  Thanks to response from Codo I know I was using two different socket technologies on the server and client. I am now using Java WebSockets. I can now see on the ESP that the Android device is trying to connect but the ESP closes the connection right away. Below is from Debug of ESP device:</p>
  
  <pre><code>[WS-Server][0] new client from 192.168.4.2
  [WS-Server][0][handleHeader] RX: GET / HTTP/1.1
  [WS-Server][0][handleHeader] RX: Connection: Upgrade
  [WS-Server][0][handleHeader] RX: Host: 192.168.4.1:81
  [WS-Server][0][handleHeader] RX: Sec-WebSocket-Key: LmtO2xH7n9k+KKJNN/4+GA==
  [WS-Server][0][handleHeader] RX: Sec-WebSocket-Version: 8
  [WS-Server][0][handleHeader] RX: Upgrade: websocket
  [WS-Server][0][handleHeader] Header read fin.
  [WS-Server][0][handleHeader]  - cURL: /
  [WS-Server][0][handleHeader]  - cIsUpgrade: 1
  [WS-Server][0][handleHeader]  - cIsWebsocket: 1
  [WS-Server][0][handleHeader]  - cKey: LmtO2xH7n9k+KKJNN/4+GA==
  [WS-Server][0][handleHeader]  - cProtocol:
  [WS-Server][0][handleHeader]  - cExtensions: &lt;null&gt;
  [WS-Server][0][handleHeader]  - cVersion: 8
  [WS-Server][0][handleHeader]  - base64Authorization:
  [WS-Server][0][handleHeader]  - cHttpHeadersValid: 1
  [WS-Server][0][handleHeader]  - cMandatoryHeadersCount: 0
  [WS-Server][0][handleHeader] no Websocket connection close.
  [WS-Server][0] client disconnected.
  </code></pre>
  
  <p>Can anyone help me figure out why the ESP is closing the connection?</p>
  
  <p><strong>[Update and Fix]</strong>
  I have fixed the issue. When creating the socket on the Android device using org.java.websockets I was using:</p>
  
  <pre><code>mWebSocketClient = new WebSocketClient(uri)
  </code></pre>
  
  <p>I found that if you don't assign the draft version when creating the socket that it defaults to websockets Version 8 which won't work with the ESP8266(at least not with the arduinowebsockets library. I had to create the socket using the following:</p>
  
  <pre><code>mWebSocketClient = new WebSocketClient(uri, new Draft_17())
  </code></pre>
  
  <p>After this the websocket is created as version 13 which now works.</p>
  
  <p>The issue is now fixed!</p>