Projectlab Elektronica-ICT KULeuven
Dependencies: EthernetInterface TMP102 TextLCD mbed-rtos mbed
Server.cpp@1:635e76c52151, 2014-03-16 (annotated)
- Committer:
- seppeduwe
- Date:
- Sun Mar 16 15:08:10 2014 +0000
- Revision:
- 1:635e76c52151
- Parent:
- TCPServer.cpp@0:ae3af7d18c4a
- Child:
- 4:466d859bfb13
Main goed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
seppeduwe | 0:ae3af7d18c4a | 1 | #include "mbed.h" |
seppeduwe | 1:635e76c52151 | 2 | #include "Server.h" |
seppeduwe | 0:ae3af7d18c4a | 3 | #include "EthernetInterface.h" |
seppeduwe | 0:ae3af7d18c4a | 4 | |
seppeduwe | 1:635e76c52151 | 5 | Server::Server(char* sourceAddress) |
seppeduwe | 0:ae3af7d18c4a | 6 | { |
seppeduwe | 0:ae3af7d18c4a | 7 | eth = new EthernetInterface(); |
seppeduwe | 0:ae3af7d18c4a | 8 | eth->init(sourceAddress,"255.255.255.0",""); |
seppeduwe | 1:635e76c52151 | 9 | eth->connect(500); |
seppeduwe | 0:ae3af7d18c4a | 10 | |
seppeduwe | 0:ae3af7d18c4a | 11 | server = new TCPSocketServer(); |
seppeduwe | 0:ae3af7d18c4a | 12 | |
seppeduwe | 0:ae3af7d18c4a | 13 | server->bind(4000); |
seppeduwe | 0:ae3af7d18c4a | 14 | server->listen(); |
seppeduwe | 0:ae3af7d18c4a | 15 | |
seppeduwe | 0:ae3af7d18c4a | 16 | socket = new TCPSocketConnection(); |
seppeduwe | 0:ae3af7d18c4a | 17 | socket->set_blocking(false, 10); |
seppeduwe | 0:ae3af7d18c4a | 18 | server->set_blocking(false, 10); |
seppeduwe | 0:ae3af7d18c4a | 19 | |
seppeduwe | 0:ae3af7d18c4a | 20 | |
seppeduwe | 0:ae3af7d18c4a | 21 | } |
seppeduwe | 1:635e76c52151 | 22 | void Server::printStatus() |
seppeduwe | 0:ae3af7d18c4a | 23 | { |
seppeduwe | 0:ae3af7d18c4a | 24 | printf("IP Address is %s\n\r", eth->getIPAddress()); |
seppeduwe | 0:ae3af7d18c4a | 25 | } |
seppeduwe | 1:635e76c52151 | 26 | int Server::connect(char* destinationAddr) |
seppeduwe | 0:ae3af7d18c4a | 27 | { |
seppeduwe | 0:ae3af7d18c4a | 28 | socket->set_blocking(false, 10); |
seppeduwe | 0:ae3af7d18c4a | 29 | if(socket->connect(destinationAddr, 4000) < 0) { |
seppeduwe | 0:ae3af7d18c4a | 30 | printf("Unable to connect to (%s) on port (%d)\n\r", destinationAddr, 4000); |
seppeduwe | 0:ae3af7d18c4a | 31 | return 0; |
seppeduwe | 0:ae3af7d18c4a | 32 | } |
seppeduwe | 0:ae3af7d18c4a | 33 | return 1; |
seppeduwe | 0:ae3af7d18c4a | 34 | } |
seppeduwe | 1:635e76c52151 | 35 | void Server::send(char* data) |
seppeduwe | 0:ae3af7d18c4a | 36 | { |
seppeduwe | 0:ae3af7d18c4a | 37 | socket->send_all(data, 256); |
seppeduwe | 0:ae3af7d18c4a | 38 | printf("Send data %s\n\r",data); |
seppeduwe | 0:ae3af7d18c4a | 39 | socket->close(); |
seppeduwe | 0:ae3af7d18c4a | 40 | } |
seppeduwe | 1:635e76c52151 | 41 | int Server::read(char* buffer) |
seppeduwe | 0:ae3af7d18c4a | 42 | { |
seppeduwe | 0:ae3af7d18c4a | 43 | resetSocket(); |
seppeduwe | 0:ae3af7d18c4a | 44 | server->accept(*socket); |
seppeduwe | 0:ae3af7d18c4a | 45 | |
seppeduwe | 0:ae3af7d18c4a | 46 | int connectionRead = 0; |
seppeduwe | 0:ae3af7d18c4a | 47 | if(socket->is_connected()) { |
seppeduwe | 0:ae3af7d18c4a | 48 | |
seppeduwe | 0:ae3af7d18c4a | 49 | printf("connection incoming from: %s \n\r",socket->get_address()); |
seppeduwe | 0:ae3af7d18c4a | 50 | while(true) { |
seppeduwe | 0:ae3af7d18c4a | 51 | int n = socket->receive(buffer, 256); |
seppeduwe | 0:ae3af7d18c4a | 52 | if (n <= 0) break; |
seppeduwe | 0:ae3af7d18c4a | 53 | } |
seppeduwe | 0:ae3af7d18c4a | 54 | printf("-------------\n\r"); |
seppeduwe | 0:ae3af7d18c4a | 55 | printf("\n reciving: %s\n\r",buffer); |
seppeduwe | 0:ae3af7d18c4a | 56 | printf("\n\r"); |
seppeduwe | 0:ae3af7d18c4a | 57 | connectionRead = 1; |
seppeduwe | 0:ae3af7d18c4a | 58 | } |
seppeduwe | 0:ae3af7d18c4a | 59 | socket->close(); |
seppeduwe | 0:ae3af7d18c4a | 60 | return connectionRead; |
seppeduwe | 0:ae3af7d18c4a | 61 | } |
seppeduwe | 0:ae3af7d18c4a | 62 | |
seppeduwe | 1:635e76c52151 | 63 | int Server::close() |
seppeduwe | 0:ae3af7d18c4a | 64 | { |
seppeduwe | 0:ae3af7d18c4a | 65 | socket->close(); |
seppeduwe | 0:ae3af7d18c4a | 66 | return 1; |
seppeduwe | 0:ae3af7d18c4a | 67 | } |
seppeduwe | 1:635e76c52151 | 68 | Server::~Server() |
seppeduwe | 0:ae3af7d18c4a | 69 | { |
seppeduwe | 0:ae3af7d18c4a | 70 | eth->disconnect(); |
seppeduwe | 0:ae3af7d18c4a | 71 | } |
seppeduwe | 1:635e76c52151 | 72 | void Server::resetSocket() |
seppeduwe | 0:ae3af7d18c4a | 73 | { |
seppeduwe | 0:ae3af7d18c4a | 74 | socket->close(); |
seppeduwe | 0:ae3af7d18c4a | 75 | delete socket; |
seppeduwe | 0:ae3af7d18c4a | 76 | socket = new TCPSocketConnection(); |
seppeduwe | 0:ae3af7d18c4a | 77 | socket->set_blocking(false, 10); |
seppeduwe | 0:ae3af7d18c4a | 78 | } |