mbed TCP socket with pyhton server
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
main.cpp@9:84f6832f3643, 2018-02-20 (annotated)
- Committer:
- MohamadNazrin
- Date:
- Tue Feb 20 04:53:00 2018 +0000
- Revision:
- 9:84f6832f3643
- Parent:
- 8:23b1fba109b0
TCP echo Server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:38cbb854d85f | 1 | #include "mbed.h" |
emilmont | 1:5cebe0e38cd2 | 2 | #include "EthernetInterface.h" |
mbed_official | 8:23b1fba109b0 | 3 | |
emilmont | 3:36fd3cfad85a | 4 | #define ECHO_SERVER_PORT 7 |
MohamadNazrin | 9:84f6832f3643 | 5 | static const char* mbedIp = "192.168.137.2"; //IP |
MohamadNazrin | 9:84f6832f3643 | 6 | static const char* mbedMask = "255.255.255.0"; // Mask |
MohamadNazrin | 9:84f6832f3643 | 7 | static const char* mbedGateway = "192.168.137.1"; //Gateway |
emilmont | 1:5cebe0e38cd2 | 8 | int main (void) { |
emilmont | 1:5cebe0e38cd2 | 9 | EthernetInterface eth; |
MohamadNazrin | 9:84f6832f3643 | 10 | eth.init(mbedIp,mbedMask,mbedGateway); |
emilmont | 1:5cebe0e38cd2 | 11 | eth.connect(); |
mbedAustin | 7:a5ead1402704 | 12 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
emilmont | 1:5cebe0e38cd2 | 13 | |
emilmont | 1:5cebe0e38cd2 | 14 | TCPSocketServer server; |
emilmont | 3:36fd3cfad85a | 15 | server.bind(ECHO_SERVER_PORT); |
emilmont | 3:36fd3cfad85a | 16 | server.listen(); |
emilmont | 1:5cebe0e38cd2 | 17 | |
emilmont | 1:5cebe0e38cd2 | 18 | while (true) { |
emilmont | 1:5cebe0e38cd2 | 19 | printf("\nWait for new connection...\n"); |
emilmont | 1:5cebe0e38cd2 | 20 | TCPSocketConnection client; |
emilmont | 1:5cebe0e38cd2 | 21 | server.accept(client); |
emilmont | 3:36fd3cfad85a | 22 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
emilmont | 1:5cebe0e38cd2 | 23 | |
emilmont | 1:5cebe0e38cd2 | 24 | printf("Connection from: %s\n", client.get_address()); |
emilmont | 1:5cebe0e38cd2 | 25 | char buffer[256]; |
emilmont | 1:5cebe0e38cd2 | 26 | while (true) { |
emilmont | 3:36fd3cfad85a | 27 | int n = client.receive(buffer, sizeof(buffer)); |
emilmont | 1:5cebe0e38cd2 | 28 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 29 | |
mbedAustin | 7:a5ead1402704 | 30 | // print received message to terminal |
mbedAustin | 7:a5ead1402704 | 31 | buffer[n] = '\0'; |
mbedAustin | 7:a5ead1402704 | 32 | printf("Received message from Client :'%s'\n",buffer); |
mbedAustin | 7:a5ead1402704 | 33 | |
mbedAustin | 7:a5ead1402704 | 34 | // reverse the message |
mbedAustin | 7:a5ead1402704 | 35 | char temp; |
mbedAustin | 7:a5ead1402704 | 36 | for(int f = 0, l = n-1; f<l; f++,l--){ |
mbedAustin | 7:a5ead1402704 | 37 | temp = buffer[f]; |
mbedAustin | 7:a5ead1402704 | 38 | buffer[f] = buffer[l]; |
mbedAustin | 7:a5ead1402704 | 39 | buffer[l] = temp; |
mbedAustin | 7:a5ead1402704 | 40 | } |
mbedAustin | 7:a5ead1402704 | 41 | |
mbedAustin | 7:a5ead1402704 | 42 | // print reversed message to terminal |
mbedAustin | 7:a5ead1402704 | 43 | printf("Sending message to Client: '%s'\n",buffer); |
mbedAustin | 7:a5ead1402704 | 44 | |
mbedAustin | 7:a5ead1402704 | 45 | // Echo received message back to client |
emilmont | 2:ec5ae99791da | 46 | client.send_all(buffer, n); |
emilmont | 1:5cebe0e38cd2 | 47 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 48 | } |
emilmont | 3:36fd3cfad85a | 49 | |
emilmont | 1:5cebe0e38cd2 | 50 | client.close(); |
emilmont | 1:5cebe0e38cd2 | 51 | } |
emilmont | 1:5cebe0e38cd2 | 52 | } |
mbed_official | 8:23b1fba109b0 | 53 |