Test socket con telnet
#include <SPI.h>
#include <Ethernet.h>
#include <Flash.h>
#include <utility/socket.h>
#include "utility/w5100.h"
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 177);
//IPAddress gateway(192,168,2, 1);
//IPAddress subnet(255, 255, 255, 0);
byte sock_occ= 99;
byte point_1=0;
// telnet defaults to port 23
EthernetServer server(23);
void setup() {
//initialize connect callback
server.registerConnectCallback(&onConnect);
// initialize the ethernet device
Ethernet.begin(mac, ip);//, gateway, subnet);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
//while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
Serial.print("Server address:");
Serial.println(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available();
status_sock();
//-----------------test------------------
if((sock_occ == 99) && (point_1 == 0))
{
Serial.println( "nessun client connesso" );
point_1=1;
}
//---------------------------------------
}
void onConnect(EthernetClient &client) {
point_1=0;
Serial.print("onConnect called, client ID: ");
Serial.println( client.getId() );
sock_occ=client.getId() ;
//if(client.getId()==0) Serial.println("onConnect called, client ID: 0");
status_sock();
//client.write();
//initialize connect callback for the client
client.registerDisconnectCallback(&onDisconnect);
}
void onDisconnect(EthernetClient &socket) {
Serial.println("onDisconnect called");
}
void status_sock()
{
Serial << F("\n");
for (int i = 0; i < MAX_SOCK_NUM; i++) {
Serial << F("Socket#");
Serial.print(i);
uint8_t s = W5100.readSnSR(i);
Serial << F(":0x");
Serial.print(s,16);
Serial << F(" - Porta ");
Serial.print(W5100.readSnPORT(i));
Serial << F(" IP Client:");
uint8_t dip[4];
W5100.readSnDIPR(i, dip);
for (int j=0; j<4; j++) {
Serial.print(dip[j],10);
if (j<3) Serial << F(".");
}
Serial << F(" Porta (");
Serial.print(W5100.readSnDPORT(i));
Serial << F(")");
Serial << F(" - Status Socket ");
uint8_t state = W5100.readSnIR(i);
Serial << F(":0x");
Serial.print(state,16);
Serial << F("\n");
//comand_sock();
}
}