Piet Cordemans
/
tcp-server
Simple TCP server example
Revision 1:b658dfbe2a7c, committed 2020-03-07
- Comitter:
- pcordemans
- Date:
- Sat Mar 07 19:26:48 2020 +0000
- Parent:
- 0:ddb5698aa782
- Commit message:
- Simple TCP server example
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Oct 30 16:59:55 2017 +0000 +++ b/main.cpp Sat Mar 07 19:26:48 2020 +0000 @@ -1,38 +1,38 @@ #include "mbed.h" #include "EthernetInterface.h" -#include "TCPServer.h" -#include "TCPSocket.h" + + +DigitalOut led(LED1); int main() { - printf("TCP server example\n"); + printf("Server example\n\r"); EthernetInterface eth; + eth.set_network("192.168.0.40","255.255.255.0","192.168.0.1"); eth.connect(); - printf("The Server IP address is '%s'\n", eth.get_ip_address()); + printf("The Server IP address is '%s'\n\r", eth.get_ip_address()); + + TCPServer srv(ð); - TCPServer srv; - TCPSocket client_sock; - SocketAddress client_addr; - char *buffer = new char[256]; + srv.bind(4000); - /* Open the server on ethernet stack */ - srv.open(ð); - - /* Bind the HTTP port (TCP 80) to the server */ - srv.bind(eth.get_ip_address(), 23); + srv.listen(); - /* Can handle x simultaneous connections */ - srv.listen(1); - - srv.accept(&client_sock, &client_addr); - printf("Accepted %s:%d\n", client_addr.get_ip_address(), + while(true){ + TCPSocket client; + SocketAddress client_addr; + char *buffer = "Hello TCP client!\r\n"; + + srv.accept(&client, &client_addr); + + printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), client_addr.get_port()); - strcpy(buffer, "Hello \n\r"); - client_sock.send(buffer, strlen(buffer)); - client_sock.recv(buffer, 256); + + client.send(buffer, 256); - client_sock.close(); - delete[] buffer; + client.close(); + + } }