Go Down

Topic: How to use EthernetServer and parseInt? (Read 532 times) previous topic - next topic

GR0B

Does anyone know where I can find an example code that allows using EthernetServer and parseInt?
I am creating a multi client telnet server that bridges ethernet with a nRF24l01 network.

I want to send a string to the EthernetServer like "SEND,1,2,3,4,5,6,7,8" and using parseInt extract the values. 

Quote

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

byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
IPAddress ip(192,168,1, 211);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);

EthernetClient client;
EthernetClient serverclient ;
EthernetServer server(23);

void setup() {
  Serial.begin(9600);
  Serial.println("Booting...");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
     Serial.println("Failed to configure Ethernet using DHCP");
     Serial.println("Using Static IP settings");
     Ethernet.begin(mac, ip, gateway, subnet);
     Serial.print("IP: ");
     Serial.println(ip);
     Serial.print("Gateway: ");
     Serial.println(gateway);
     Serial.print("Subnet: ");
     Serial.println(subnet);
  }
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
  server.begin();
  Serial.println("Server started :23");
 }


void loop()
{
  EthernetClient serverclient = server.available();
  if (serverclient.available()) {         server_comand(); }
  
  if (!serverclient.connected()) {
    Serial.println("disconnecting.");
    serverclient.stop();
  }       
}

void server_comand(){
   if (server.available()) { 
      if (serverclient.find("SEND") == 1){
          Serial.println(" server command ");
      //    Serial.print(serverclient.read());
          int i = 0;
          unsigned int data[11] ;
          for (i = 0; i < 8; i++) {
              data = serverclient.parseInt();
              Serial.print(data,DEC);
              Serial.print(",");
           }
          serverclient.println("OK");
          delay(20);
          serverclient.stop(); 
        }     
   } 
}


PHP code
<?php
$fp = stream_socket_client("tcp://192.168.1.31:23", $errno, $errstr, 3);
stream_set_timeout($fp, 3);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "SEND,1,2,3,4,5,6,7,8,\n");
//   while (!feof($fp)) {
//       echo fgets($fp, 3);
//   }
    fclose($fp);
}
?>


PaulS

Quote
Does anyone know where I can find an example code that allows using EthernetServer and parseInt?

The parseInt() method is defined in the Stream class. Serial derives from Stream, which derives from Print.

EthernetServer derives from Server, which derives from Print.

So, the answer is that you can't.

GR0B

Never mind
I got it to work.
Changed EthernetClient serverclient = server.available(); to EthernetClient client = server.available();
then client.parseInt(); started working I think I must have changed a few other things too.


Go Up