Example application that implements a simple TCP server on top of the NetworkAPI
Dependencies: mbed mbed-rtos EthernetInterface NetworkAPI
Revision 11:90554d22ade5, committed 2014-11-15
- Comitter:
- NegativeBlack
- Date:
- Sat Nov 15 21:46:59 2014 +0000
- Parent:
- 10:9e8d5928537a
- Commit message:
- Version bump of included libraries
Changed in this revision
diff -r 9e8d5928537a -r 90554d22ade5 EthernetInterface.lib --- a/EthernetInterface.lib Thu Sep 27 09:35:46 2012 +0000 +++ b/EthernetInterface.lib Sat Nov 15 21:46:59 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/EthernetInterface/#a0ee3ae75cfa +http://mbed.org/users/mbed_official/code/EthernetInterface/#de796e2a5e98
diff -r 9e8d5928537a -r 90554d22ade5 NetworkAPI.lib --- a/NetworkAPI.lib Thu Sep 27 09:35:46 2012 +0000 +++ b/NetworkAPI.lib Sat Nov 15 21:46:59 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/NegativeBlack/code/NetworkAPI/#cdee0f2b6ff0 +http://mbed.org/users/NegativeBlack/code/NetworkAPI/#7ac7c29fea3d
diff -r 9e8d5928537a -r 90554d22ade5 main.cpp --- a/main.cpp Thu Sep 27 09:35:46 2012 +0000 +++ b/main.cpp Sat Nov 15 21:46:59 2014 +0000 @@ -1,13 +1,14 @@ #include "mbed.h" #include "EthernetInterface.h" - + #include "NetworkAPI/buffer.hpp" +#include "NetworkAPI/select.hpp" +#include "NetworkAPI/ip/address.hpp" #include "NetworkAPI/tcp/socket.hpp" using namespace network; - -#define PORT 1234 -#define MAX_PENDING 1 - + +#define MAX_CLIENTS 5 + int main() { @@ -15,56 +16,93 @@ interface.init(); interface.connect(); printf("IP Address is %s\n\r", interface.getIPAddress()); - + + Select select; tcp::Socket server; - tcp::Socket client; - - Buffer buffer(256); - - if (server.open() < 0) { - printf("Could not open server socket.\n\r"); - return -1; + tcp::Socket client[MAX_CLIENTS]; + tcp::Socket *socket = NULL; + + int result = 0; + int index = 0; + + network::Buffer buffer(256); + std::string message("Hello world!"); + + // Configure the server socket (assume everty thing works) + server.open(); + server.bind(1234); + server.listen(MAX_CLIENTS); + + // Add sockets to the select api + select.set(&server, Select::Read); + for (index = 0; index < MAX_CLIENTS; index++) { + select.set(&client[index], Select::Read); } - - if (server.bind(PORT) < 0) { - printf("Could not bind server socket to port '%d'.\n\r", PORT); - return -1; - } - - if (server.listen(MAX_PENDING) < 0) { - printf("Could not put server socket into listening mode.\n\r"); - return -1; - } - - while (server.getStatus() == tcp::Socket::Listening) { - if (server.accept(client) < 0) { - printf("Warning: failed to accept connection.\n\r"); - continue; + + do { + // Wait for activity + result = select.wait(); + if (result < -1) { + printf("Failed to select\n\r"); + break; } - - printf("Client connected '%s:%d'.\n\r", - client.getRemoteEndpoint().getAddress().toString().c_str(), - client.getRemoteEndpoint().getPort()); - - while (client.getStatus() == tcp::Socket::Connected) { - buffer.flush(); - - switch (client.read(buffer)) { - case -1: - printf("Warning: failed to read data from client.\n\r"); - break; - + + // Get the first socket + socket = (tcp::Socket *)select.getReadable(); + + for (; socket != NULL; socket = (tcp::Socket *)select.getReadable()) { + // Check if there was a connection request. + if (socket->getHandle() == server.getHandle()) { + // Find an unused client + for (index = 0; index < MAX_CLIENTS; index++) { + if (client[index].getStatus() == network::Socket::Closed) { + break; + } + } + + // Maximum connections reached + if (index == MAX_CLIENTS) { + printf("Maximum connections reached\n\r"); + continue; + } + + // Accept the client + socket->accept(client[index]); + printf("Client connected %s:%d\n\r", + client[index].getRemoteEndpoint().getAddress().toString().c_str(), + client[index].getRemoteEndpoint().getPort()); + + // Send a nice message to the client + client[index].write((void *)message.data(), message.size()); + continue; + } + + // It was not the server socket, so it must be a client talking to us. + switch (socket->read(buffer)) { case 0: - printf("Connection closed.\n\r"); + // Remote end disconnected + printf("Client disconnected %s:%d\n\r", + socket->getRemoteEndpoint().getAddress().toString().c_str(), + socket->getRemoteEndpoint().getPort()); + + // Close socket + socket->close(); break; - + + case -1: + printf("Error while reading data from socket\n\r"); + socket->close(); + break; + default: - printf("Received %d bytes.\n\r%s\r", buffer.length(), (char *)buffer.data()); - continue; + printf("Message from %s:%d\n\r", + socket->getRemoteEndpoint().getAddress().toString().c_str(), + socket->getRemoteEndpoint().getPort()); + + printf("%s\n\r", (char *)buffer.data()); + break; } - - client.shutdown(); - client.close(); } - } + + } while (server.getStatus() == network::Socket::Listening); } \ No newline at end of file
diff -r 9e8d5928537a -r 90554d22ade5 mbed-rtos.lib --- a/mbed-rtos.lib Thu Sep 27 09:35:46 2012 +0000 +++ b/mbed-rtos.lib Sat Nov 15 21:46:59 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90 +http://mbed.org/users/mbed_official/code/mbed-rtos/
diff -r 9e8d5928537a -r 90554d22ade5 mbed.bld --- a/mbed.bld Thu Sep 27 09:35:46 2012 +0000 +++ b/mbed.bld Sat Nov 15 21:46:59 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/976df7c37ad5 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file