Simple TCP server
Posted: Mon Dec 07, 2015 11:12 am
I am trying to get a simple TCP raw socket server started, but am running into a little trouble.
Would really appreciate some pointers!
I currently am trying this.
However, when I try to connect using PuTTY, I get a "Connected to client" on the serial monitor, and then a -1, followed by a "Connection was closed by the remote host" on PuTTY.
Any ideas what I am missing? Am I progressing in the right direction?
Would really appreciate some pointers!
I currently am trying this.
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
// Hardcode WiFi parameters as this isn't going to be moving around.
const char* ssid = "<>";
const char* password = "<>";
// Start a TCP Server on port 5045
WiFiServer server(5045);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
Serial.println("");
//Wait for connection
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to "); Serial.println(ssid);
Serial.print("IP Address: "); Serial.println(WiFi.localIP());
// Start the TCP server
server.begin();
}
void loop() {
TCPServer();
}
void TCPServer () {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println("Connected to client");
}
if (client.available() > 0) {
// Read incoming message
char inChar = client.read();
// Send back something to the clinet
server.write(inChar);
// Echo input on Serial monitor
Serial.write(inChar);
}
However, when I try to connect using PuTTY, I get a "Connected to client" on the serial monitor, and then a -1, followed by a "Connection was closed by the remote host" on PuTTY.
Any ideas what I am missing? Am I progressing in the right direction?