0

From linux I can send a socket message using netcat

echo "Hello" | nc 88.109.110.161 100

But I don't know how to setup the socket server for listening incoming messages using the ethernet shield.

What I have so far is one from a lot of examples I tried but still not showing anything

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetServer server(1000);

void setup() {
  Ethernet.begin(mac);
  Serial.begin(9600);
// Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // listen for incoming clients
 EthernetClient client = server.available();
 if (client) {
   Serial.println("Client");
   // an http request ends with a blank line
   boolean currentLineIsBlank = true;
   while (client.connected()) {
     while(client.available()) {
       char c = client.read();
       // if you've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so you can send a reply
       if (c == '\n' && currentLineIsBlank) {

// Here is where the POST data is.  
         while(client.available())
         {
            Serial.write(client.read());
            //Serial.println(c);
         }
         Serial.println();

         Serial.println("Sending response");
         // send a standard http response header
         //client.println("HTTP/1.0 200 OK");
         //client.println("Content-Type: text/html");
         //client.println();
         client.println("OK");
         client.stop();
       }
       else if (c == '\n') {
         // you're starting a new line
         currentLineIsBlank = true;
       }
       else if (c != '\r') {
         // you've gotten a character on the current line
         currentLineIsBlank = false;
       }
     }
   }
   Serial.println("Disconnected");
 }
} 
  • have you confirmed that your ethernet shield is functional? – jsotola Jun 4 at 2:11
  • see the ChatServer example – Juraj Jun 4 at 5:23
  • Where is your Ethernet.begin? Surely you still need one of those. have a look at the setup() in the docs here: EthernetServer Example – GMc Jun 4 at 10:14
  • the shield is fully functional, I'm able to receive http post requests, that's how I figure it out, but I still needing the socket server, just to receive raw messaages – Martin Ocando Corleone Jun 4 at 12:23
  • I updated the code, that's how actually receives http requests – Martin Ocando Corleone Jun 4 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.