Pages: [1]   Go Down
Author Topic: Detecting an incoming connection  (Read 486 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I'm having an issue with the Arduino Ethernet library and I'm hoping I can get some pointers.

Essentially it boils down to this: I want to detect an incoming connection WITHOUT having to have the server send something followed by a carriage return.

Instead of posting my code, I'll just reference the tutorial here as it illustrates my issue:

To test, I load this code in the Arduino, then on my Mac, I type:

telnet 80

In the Arduino serial monitor, I do not see the "new client" message until I press return.

What I would like is for the Arduino to print a message to the serial monitor as soon as the telnet connects, without having to press return.

Folks, is this possible?

Offline Offline
Tesla Member
Karma: 145
Posts: 9679
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Kind of hard to follow just what you want to do. Below is some telnet servo test code that echos back to the telnet client and to the serial monitor. A comma , is used as the packet delimiter. Using the telnet client send a servo us position like 1500, and see the result.

//zoomkat 2-05-13
//simple arduino telnet server test
//for use with IDE 1.0
//servo us typed in telnet client is echoed back from arduino server
//a comma , is used as a delimiter, use serial monitor to monitor
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
String readString;
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup()
  Serial.println("telnet delimit test 1.0"); // so I can keep track of what is loaded
  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println("multi-servo-telnet-delimit-test 3-12-12"); // so I can keep track of what

  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients

void loop()
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {

    char c =;  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        //do stuff with the captured readString
        Serial.println(readString); //prints string to serial port out
        int n = readString.toInt();  //convert readString into a number
        Serial.print("writing servo angle: ");
        if(readString.indexOf('a') >0) myservoa.write(n);
        if(readString.indexOf('b') >0) myservob.write(n);
        if(readString.indexOf('c') >0) myservoc.write(n);
        if(readString.indexOf('d') >0) myservod.write(n);

        readString=""; //clears variable for new input
    else {     
      readString += c; //makes the string readString


Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Faraday Member
Karma: 111
Posts: 5236
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The EthernetServer.available() returns a client object only if the client has some characters available. You have to edit your EthernetServer class and insert a new method like this (quick hack, untested code):

EthernetClient EthernetServer::connected()

  for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
    EthernetClient client(sock);
    if (EthernetClass::_server_port[sock] == _port &&
         (client.status() == SnSR::ESTABLISHED ||
         (client.status() == SnSR::CLOSE_WAIT && client.available()))) {
      return client;

  return EthernetClient(MAX_SOCK_NUM);

Don't forget to insert the appropriate code into EthernetServer.h too. Then use connected() instead of available on the server object.

Offline Offline
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


That method works and does the job.  Thank you very much.  I took the opportunity to look at the EthernetServer.cpp source and the approach seems sound.

Kat, thanks for the answer also.

Pages: [1]   Go Up
Jump to: