
ok
Dependencies: EthernetInterfaceAA mbed-rtos mbed
main.cpp
00001 00002 #include "mbed.h" 00003 #include "EthernetInterface.h" 00004 #include "Socket.h" 00005 #include "TCPSocketServer.h" 00006 #include "TCPSocketConnection.h" 00007 00008 DigitalOut myled(LED1); 00009 Serial tty_pc(USBTX, USBRX); 00010 00011 void eth_main(const void *context) 00012 { 00013 puts("Starting " __FILE__ " " __DATE__ " " __TIME__); 00014 00015 Ethernet eth; 00016 eth.set_link(eth.AutoNegotiate); 00017 00018 EthernetInterface ethif; 00019 ethif.init(); 00020 ethif.connect(); 00021 00022 printf("IP %s\n", ethif.getIPAddress()); 00023 00024 TCPSocketServer server; 00025 TCPSocketConnection sock; 00026 bool active = false; 00027 unsigned short port = 8000; 00028 00029 server.bind(port); 00030 server.listen(); 00031 00032 server.set_blocking(false, 500); // SET NON-BLOCKING 00033 00034 printf("listening at %s:%hu\n", ethif.getIPAddress(), port); 00035 00036 while(true) 00037 { 00038 myled = eth.link(); 00039 00040 if (active) 00041 { 00042 00043 #ifdef OLD 00044 char c; 00045 if (sock.receive(&c, 1) <= 0) // NON-BLOCKING (!?) RECEIVE TO CHECK IF SOCKET ACTIVE 00046 Thread::wait(500); 00047 #endif 00048 if (!sock.is_connected()) 00049 { 00050 puts("connection closed"); 00051 sock.close(true); 00052 active = false; 00053 server.bind(port); 00054 server.listen(); 00055 printf("listening at %s:%hd\n", ethif.getIPAddress(), port); 00056 } 00057 } 00058 else 00059 { 00060 if (server.accept(sock) == 0) 00061 { 00062 active = true; 00063 server.close(); 00064 puts("connection established"); 00065 } 00066 } 00067 } 00068 } 00069 00070 int main() 00071 { 00072 Thread eth_thread(eth_main, 0); 00073 00074 while (1) 00075 { 00076 // forever 00077 } 00078 }
Generated on Thu Jul 21 2022 00:39:17 by
