![](/media/cache/profiles/8cf40e328089249cf77075692aa0c09b.jpg.50x50_q85.jpg)
Thermometer connected to internet
Dependencies: BME280 EthernetInterface FXOS8700Q HTTPClient-wolfSSL NTPClient NetworkAPI OAuth4Tw TSL2561_I2C eCompass_FPU_Lib mbed-rtos mbed wolfSSL
Fork of TCP_Server_Example by
main.cpp
- Committer:
- NegativeBlack
- Date:
- 2012-09-27
- Revision:
- 10:9e8d5928537a
- Parent:
- 9:a4c85bea2d77
- Child:
- 11:90554d22ade5
File content as of revision 10:9e8d5928537a:
#include "mbed.h" #include "EthernetInterface.h" #include "NetworkAPI/buffer.hpp" #include "NetworkAPI/tcp/socket.hpp" using namespace network; #define PORT 1234 #define MAX_PENDING 1 int main() { EthernetInterface interface; interface.init(); interface.connect(); printf("IP Address is %s\n\r", interface.getIPAddress()); tcp::Socket server; tcp::Socket client; Buffer buffer(256); if (server.open() < 0) { printf("Could not open server socket.\n\r"); return -1; } 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; } 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; case 0: printf("Connection closed.\n\r"); break; default: printf("Received %d bytes.\n\r%s\r", buffer.length(), (char *)buffer.data()); continue; } client.shutdown(); client.close(); } } }