friendev EtherDune TCP/IP library
EchoServer.ino
Go to the documentation of this file.
1 // EtherDune Echo server demo
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Demonstrates how to build a simple server application
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
29 #include <ACross.h>
30 #include <TCPSocket.h>
31 #include <FlowScanner.h>
32 
33 #define AC_LOGLEVEL 6
34 #include <ACLog.h>
35 ACROSS_MODULE("EchoServer");
36 
37 
38 static const uint8_t CS_PIN = 10; //Put here what pin you are using for your ENC28J60's chip select
39 static MACAddress_P mymac = { 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64 };
40 static IPAddress_P gatewayIP = { 192, 168, 1, 1 };
41 static IPAddress_P myIP = { 192, 168, 1, 33 };
42 static IPAddress_P netmask = { 255, 255, 255, 0 };
43 
44 static const uint16_t ECHO_SERVER_TCP_PORT = 2500;
45 
46 class EchoServer : public TCPSocket
47 {
48 
49 public:
50 
51  void start(uint16_t port)
52  {
53  localPort = port;
54  listen();
55  }
56 
57  void onConnectRequest()
58  {
59  ACTRACE("onConnectRequest");
60  accept(); //accept connection.
61  }
62  void onConnect()
63  {
64  ACTRACE("onConnect");
65 
66  write(F("How about a nice game of chess?\n"));
67  Serial.println(F("Client connected"));
68 
69  }
70 
71  void onClose()
72  {
73  close(); //property close the connection and then listen again.
74  Serial.println(F("Client disconnected"));
75  }
76 
77  void onReceive(uint16_t len, const byte* data)
78  {
79  ACTRACE("onReceive: %d bytes",len);
80  write(len, data); //echo everything back
81  }
82 
83  void onTerminate()
84  {
85  listen();
86  }
87 
88 
89 } echoServer;
90 
91 void setup()
92 {
93 
94  ACross::init();
95  Serial.begin(115200);
96  Serial.println(F("EtherDune Echo TCP server sample"));
97  Serial.print(F("Free RAM: ")); Serial.println(ACross::getFreeRam());
98  Serial.println(F("Press any key to start..."));
99 
100  while (!Serial.available());
101 
102 
103  net::localIP = myIP;
107 
108 
109  if (!net::begin(CS_PIN))
110  ACERROR("failed to start EtherDune");
111 
112  ACINFO("waiting for link...");
113 
114  while (!net::isLinkUp());
115 
116  ACINFO("link is up");
117 
118  echoServer.start(ECHO_SERVER_TCP_PORT);
119 
120  Serial.println(F("Echo server is up"));
121  Serial.print(F("Listening on TCP port "));
122  Serial.println(echoServer.localPort);
123 }
124 
125 
126 
127 void loop()
128 {
129  net::loop();
130 }
131 /// \endcond
virtual void onConnectRequest()
Called when a listening socket receives a connection request.
Definition: TCPSocket.cpp:44
#define MACAddress_P
Definition: inet.h:242
#define IPAddress_P
helper macro to store an IP address in PROGMEM
Definition: inet.h:170
uint16_t write(uint16_t len, const byte *data)
In the case of TCP, writes the given data buffer to the socket.
Definition: Socket.cpp:54
virtual void onConnect()
Fires when the socket connection is established.
Definition: TCPSocket.cpp:39
static IPAddress_P gatewayIP
void listen()
Starts listening on the local port indicated by the localPort property.
Definition: TCPSocket.cpp:91
static bool begin(uint8_t cspin)
Initializes EtherDune and the underlying hardware
static IPAddress netmask
Subnet mask.
static IPAddress gatewayIP
IP address of the gateway in this network.
ACROSS_MODULE("ARP")
static const uint8_t CS_PIN
static MACAddress_P mymac
static bool isLinkUp()
Determines whether the network link is ready
Definition: ENC28J60.cpp:306
Implements the TCP protocol.
Definition: TCPSocket.h:45
void setup()
static MACAddress localMAC
Ethernet MAC address.
static IPAddress_P myIP
static void loop()
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets...
virtual void onTerminate()
Called when the socket is ready to be reused.
Definition: TCPSocket.cpp:54
static IPAddress localIP
IP address of this application.
void close()
Attempts to gracefully close a connection.
Definition: TCPSocket.cpp:194
void accept()
Accepts a connection request that has been received on the port this instance was listening on ...
Definition: TCPSocket.cpp:101
virtual void onReceive(uint16_t len, const byte *data)
Called for each data packet received
Definition: TCPSocket.cpp:50
static IPAddress_P netmask
virtual void onClose()
Fires when the other party closed the incoming end of the connection because it has no more data to s...
Definition: TCPSocket.cpp:31
void loop()