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");
 }
} 
7
  • have you confirmed that your ethernet shield is functional?
    – jsotola
    Commented Jun 4, 2019 at 2:11
  • see the ChatServer example
    – Juraj
    Commented Jun 4, 2019 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
    Commented Jun 4, 2019 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 Commented Jun 4, 2019 at 12:23
  • I updated the code, that's how actually receives http requests Commented Jun 4, 2019 at 12:33

1 Answer 1

-1

I am learning to use the arduino library about sockets and I spent an hour detecting why your example was not working for me, I ended up noticing that you have one line wrong

if (c = '\n' && currentLineIsBlank) {

should be:

{ if (c != '\n' && !currentLineIsBlank) {

And you still have an error you are missing a return character because you are not printing it in the second loop.

Anyway thanks for your contributions.

1
  • there is == and that is right
    – Juraj
    Commented Feb 2, 2023 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.