Projectlab Elektronica-ICT KULeuven
Dependencies: EthernetInterface TMP102 TextLCD mbed-rtos mbed
Diff: TCPServer.cpp
- Revision:
- 0:ae3af7d18c4a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TCPServer.cpp Fri Mar 14 19:41:14 2014 +0000 @@ -0,0 +1,78 @@ +#include "mbed.h" +#include "TCPServer.h" +#include "EthernetInterface.h" + +TCPServer::TCPServer(char* sourceAddress) +{ + eth = new EthernetInterface(); + eth->init(sourceAddress,"255.255.255.0",""); + eth->connect(); + + server = new TCPSocketServer(); + + server->bind(4000); + server->listen(); + + socket = new TCPSocketConnection(); + socket->set_blocking(false, 10); + server->set_blocking(false, 10); + + +} +void TCPServer::printStatus() +{ + printf("IP Address is %s\n\r", eth->getIPAddress()); +} +int TCPServer::connect(char* destinationAddr) +{ + socket->set_blocking(false, 10); + if(socket->connect(destinationAddr, 4000) < 0) { + printf("Unable to connect to (%s) on port (%d)\n\r", destinationAddr, 4000); + return 0; + } + return 1; +} +void TCPServer::send(char* data) +{ + socket->send_all(data, 256); + printf("Send data %s\n\r",data); + socket->close(); +} +int TCPServer::read(char* buffer) +{ + resetSocket(); + server->accept(*socket); + + int connectionRead = 0; + if(socket->is_connected()) { + + printf("connection incoming from: %s \n\r",socket->get_address()); + while(true) { + int n = socket->receive(buffer, 256); + if (n <= 0) break; + } + printf("-------------\n\r"); + printf("\n reciving: %s\n\r",buffer); + printf("\n\r"); + connectionRead = 1; + } + socket->close(); + return connectionRead; +} + +int TCPServer::close() +{ + socket->close(); + return 1; +} +TCPServer::~TCPServer() +{ + eth->disconnect(); +} +void TCPServer::resetSocket() +{ + socket->close(); + delete socket; + socket = new TCPSocketConnection(); + socket->set_blocking(false, 10); +} \ No newline at end of file