-->
Page 1 of 1

ESP8266 fails to connect to Java Socket

PostPosted: Fri Jan 01, 2016 11:35 am
by vSocket
Hello Guys!

I have a Socket on my vServer programmed in Java an I am flashing the code directly to the ESP.
My ESP connects to the Socket the first time without problems. If I reset the ESP one connection attempt fails before it suceeds. If I power cycle the ESP again it fails two times before it works. And so on...
The errors increase with every connection the ESP establishes.
This problem does not occur if I connect to the socket with my PC or my iPhone

The code is the WifiClient Example but slightly modified. What it does:
1. Connects to WiFi
2. Connects to my TCP Socket on specific port
3. Sends Message to Server
4. Closes Connection

Here is my code:
Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "MySSID";
const char* password = "MySecretPassword";
const char* host = "myvserver.com";

void setup() {
  Serial.begin(115200);
  delay(800);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.print("Connected with IP ");
  Serial.println(WiFi.localIP());
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void loop() {

  WiFiClient client;
 
  if (!client.connect(host, 36443)) {
    Serial.print("X");
    return;
  }
 
  Serial.println();
  Serial.print("Connected to ");
  Serial.println(host);

  Serial.println("Sending Message to server");
  client.println("Hey I am ESP Boss");
  delay(100);
 
  String line = client.readStringUntil('\n');
  Serial.print(line);

  Serial.println();
  Serial.println("closing connection");
  client.flush();
  client.stop();
  Serial.println("connection closed");

  Serial.println("Stopping Connection Attemtps"); 
}

Re: ESP8266 fails to connect to Java Socket

PostPosted: Wed Jan 13, 2016 10:04 am
by yomasa
Where is the java code?

Solved: ESP8266 fails to connect to Java Socket

PostPosted: Mon Feb 22, 2016 9:04 am
by vSocket
Finally I found the Problem.
I did not espect the error to be located in the Java Code that is why I did not post it.
But guess where the Error was located: In the Java Code.

I did not close the connection on the server side :roll:

Code: Select allprivate final Socket socket;

if(reader.read() ==  -1)  // Client disconnected from Server
socket.close();  // THIS is the point


Hope it helps somebody out there :)