Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Telnet-like server (tcp persistent connection)

WARNING! Each device on a network must have a unique mac address. If you are using more than one ethernet shield on a network, you must insure all mac addresses are unique. No duplicates!

Change network settings to meet your localnet settings.

I used PuTTY to test this in raw mode and port 80. This code does not have the initial sends and receives to negotiate a telnet session. You can add that if that is your goal.

The code below sends a "Hello" on initial connection. You can stop that by commenting out the send.

Enter "quit" on telnet client to close the connection.

The socket information displayed:
Socket #<number> <status> <local port> D:<client IP>(client port) RX:<count> <buffer>

A status list:
0X00 = available
0X14 = waiting for a connection
0X17 = client connected
0X1C = client disconnected waiting for close
0X22 = UDP

  1. /*
  2.    Telnet server sketch for IDE v1.0.5+ and w5100/w5200
  3.    Posted 4 January 2015 by SurferTim
  4.    Last edit 6 January 2015 by SurferTim
  5. */
  6. #include <SPI.h>
  7. #include <Ethernet.h>
  8. #include <utility/w5100.h>
  9. #include <utility/socket.h>
  10.  
  11. #define port 80
  12.  
  13. byte socketStat[MAX_SOCK_NUM];
  14. byte connectStatus[MAX_SOCK_NUM];
  15.  
  16. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
  17.  
  18. // change to your network settings
  19. IPAddress ip( 192,168,2,2 );
  20. IPAddress gateway( 192,168,2,1 );
  21. IPAddress subnet( 255,255,255,0 );
  22.  
  23. EthernetServer server(port);
  24.  
  25. void setup()
  26. {
  27.   Serial.begin(115200);
  28.  
  29.   // disable SD
  30.   pinMode(4,OUTPUT);
  31.   digitalWrite(4,HIGH);
  32.  
  33.   Ethernet.begin(mac, ip, gateway, gateway, subnet);
  34.   Serial.println(Ethernet.localIP());
  35.  
  36.   delay(2000);
  37.   server.begin();
  38. }
  39.  
  40. void loop() {
  41.   ShowSockStatus();
  42.   delay(5000);  
  43. }
  44. void ShowSockStatus()
  45. {
  46.   byte listening = 0;
  47.  
  48.   Serial.println();
  49.   for (int i = 0; i < MAX_SOCK_NUM; i++) {
  50.     Serial.print(F("Socket#"));
  51.     Serial.print(i);
  52.     uint8_t s = W5100.readSnSR(i);
  53.     socketStat[i] = s;
  54.  
  55.     if(s == 0x1C) {
  56.       close(i);
  57.       connectStatus[i] = 0;
  58.     }
  59.  
  60.     if(s == 0x14) listening = 1;      
  61.  
  62.     if(s == 0x17 && connectStatus[i] == 0) {
  63.       connectStatus[i] = 1;
  64. // comment out this send to stop initial "Hello"
  65.       send(i,(unsigned char*)"Hello\r\n",7);
  66.     }
  67.  
  68.     Serial.print(F(":0x"));
  69.     if(s < 16) Serial.print(F("0"));
  70.     Serial.print(s,HEX);
  71.     Serial.print(F(" "));
  72.     Serial.print(W5100.readSnPORT(i));
  73.     Serial.print(F(" D:"));
  74.     uint8_t dip[4];
  75.     W5100.readSnDIPR(i, dip);
  76.     for (int j=0; j<4; j++) {
  77.       Serial.print(dip[j],10);
  78.       if (j<3) Serial.print(".");
  79.     }
  80.     Serial.print(F("("));
  81.     Serial.print(W5100.readSnDPORT(i));
  82.     Serial.print(F(") "));
  83.     Serial.print(F("RX:"));
  84.     unsigned int rxCount = W5100.getRXReceivedSize(i);        
  85.  
  86.     Serial.print(rxCount);
  87.     Serial.print(F("  "));
  88.  
  89.     if(rxCount > 32) rxCount = 32;
  90.  
  91.     if(rxCount > 0) {
  92.       unsigned char rxBuffer[33];
  93.  
  94.       recv(i,rxBuffer,rxCount);
  95.  
  96.       rxBuffer[rxCount] = 0;
  97.  
  98.       Serial.print((char*)rxBuffer);
  99.       send(i,(unsigned char*)"ok ",3);
  100.       send(i,(unsigned char*)rxBuffer,strlen((char*)rxBuffer));
  101.  
  102.       if(strncmp((char*)rxBuffer,"quit",4) == 0) {
  103.         disconnect(i);
  104.  
  105.         unsigned long start = millis();
  106.  
  107.         while(W5100.readSnSR(i) != SnSR::CLOSED && millis() - start < 1000)
  108.            delay(1);
  109.  
  110.         if(W5100.readSnSR(i) != SnSR::CLOSED) close(i);
  111.         connectStatus[i] = 0;
  112.       }
  113.     }
  114.  
  115.     Serial.println();
  116.  
  117.   }
  118.  
  119.   if(!listening) {
  120.     Serial.println(F("Not listening"));
  121.  
  122.     for(int i = 0;i < MAX_SOCK_NUM; i++) {
  123.       if(socketStat[i] == 0) {
  124.         socket(i,SnMR::TCP,port,0);
  125.         listen(i);
  126.         break;      
  127.       }  
  128.     }    
  129.   }
  130. }