Based on Shigeki KOMATSU program
Dependencies: mbed EthernetNetIf
main.cpp
00001 00002 #include "mbed.h" 00003 #include "EthernetNetIf.h" 00004 #include "TCPSocket.h" 00005 00006 DigitalOut led4(LED4, "led4"); 00007 00008 //EthernetNetIf eth; 00009 EthernetNetIf eth( 00010 IpAddr(192,168,1,110), //IP Address 00011 IpAddr(255,255,255,0), //Network Mask 00012 IpAddr(192,168,1,1), //Gateway 00013 IpAddr(192,168,1,1) //DNS 00014 ); 00015 00016 #define TCP_LISTENING_PORT 12345 00017 00018 TCPSocket ListeningSock; 00019 TCPSocket* pConnectedSock; // for ConnectedSock 00020 Host client; 00021 TCPSocketErr err; 00022 00023 void onConnectedTCPSocketEvent(TCPSocketEvent e) 00024 { 00025 switch(e) 00026 { 00027 case TCPSOCKET_CONNECTED: 00028 printf("TCP Socket Connected\r\n"); 00029 break; 00030 case TCPSOCKET_WRITEABLE: 00031 //Can now write some data... 00032 //printf("TCP Socket Writable\r\n"); 00033 break; 00034 case TCPSOCKET_READABLE: 00035 //Can now read dome data... 00036 //printf("TCP Socket Readable\r\n"); 00037 // Read in any available data into the buffer 00038 char buff[128]; 00039 while ( int len = pConnectedSock->recv(buff, 128) ) { 00040 // And send straight back out again 00041 pConnectedSock->send(buff, len); 00042 buff[len]=0; // make terminater 00043 printf("Received from and sent back to client:%s\r\n",buff); 00044 } 00045 break; 00046 case TCPSOCKET_CONTIMEOUT: 00047 printf("TCP Socket Timeout\r\n"); 00048 break; 00049 case TCPSOCKET_CONRST: 00050 printf("TCP Socket CONRST\r\n"); 00051 break; 00052 case TCPSOCKET_CONABRT: 00053 printf("TCP Socket CONABRT\r\n"); 00054 break; 00055 case TCPSOCKET_ERROR: 00056 printf("TCP Socket Error\r\n"); 00057 break; 00058 case TCPSOCKET_DISCONNECTED: 00059 //Close socket... 00060 printf("TCP Socket Disconnected\r\n"); 00061 pConnectedSock->close(); 00062 break; 00063 default: 00064 printf("DEFAULT\r\n"); 00065 } 00066 } 00067 00068 00069 void onListeningTCPSocketEvent(TCPSocketEvent e) 00070 { 00071 switch(e) 00072 { 00073 case TCPSOCKET_ACCEPT: 00074 printf("Listening: TCP Socket Accepted\r\n"); 00075 // Accepts connection from client and gets connected socket. 00076 err=ListeningSock.accept(&client, &pConnectedSock); 00077 if (err) { 00078 printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); 00079 return; //Error in accept, discard connection 00080 } 00081 // Setup the new socket events 00082 pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); 00083 // We can find out from where the connection is coming by looking at the 00084 // Host parameter of the accept() method 00085 IpAddr clientIp = client.getIp(); 00086 printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", 00087 clientIp[0], clientIp[1], clientIp[2], clientIp[3]); 00088 break; 00089 // the following cases will not happen 00090 case TCPSOCKET_CONNECTED: 00091 printf("Listening: TCP Socket Connected\r\n"); 00092 break; 00093 case TCPSOCKET_WRITEABLE: 00094 printf("Listening: TCP Socket Writable\r\n"); 00095 break; 00096 case TCPSOCKET_READABLE: 00097 printf("Listening: TCP Socket Readable\r\n"); 00098 break; 00099 case TCPSOCKET_CONTIMEOUT: 00100 printf("Listening: TCP Socket Timeout\r\n"); 00101 break; 00102 case TCPSOCKET_CONRST: 00103 printf("Listening: TCP Socket CONRST\r\n"); 00104 break; 00105 case TCPSOCKET_CONABRT: 00106 printf("Listening: TCP Socket CONABRT\r\n"); 00107 break; 00108 case TCPSOCKET_ERROR: 00109 printf("Listening: TCP Socket Error\r\n"); 00110 break; 00111 case TCPSOCKET_DISCONNECTED: 00112 //Close socket... 00113 printf("Listening: TCP Socket Disconnected\r\n"); 00114 ListeningSock.close(); 00115 break; 00116 default: 00117 printf("DEFAULT\r\n"); 00118 }; 00119 } 00120 00121 00122 int main() { 00123 printf("\r\n"); 00124 printf("Setting up...\r\n"); 00125 EthernetErr ethErr = eth.setup();//initialisation of ethernet stack 00126 if(ethErr) 00127 { 00128 printf("Error %d in setup.\r\n", ethErr); 00129 return -1; 00130 } 00131 printf("Setup OK\r\n"); 00132 00133 IpAddr ip = eth.getIp(); 00134 printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);//address of server 00135 00136 00137 // Set the callbacks for Listening 00138 ListeningSock.setOnEvent(&onListeningTCPSocketEvent); 00139 00140 // bind and listen on TCP 00141 err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); 00142 printf("Binding..\r\n"); 00143 00144 if(err) 00145 { 00146 //Deal with that error... 00147 printf("Binding Error\n"); 00148 } 00149 err=ListeningSock.listen(); // Starts listening 00150 printf("Listening...\r\n"); 00151 if(err) 00152 { 00153 printf("Listening Error\r\n"); 00154 } 00155 00156 Timer tmr; 00157 tmr.start(); 00158 00159 while(true) 00160 { 00161 Net::poll(); 00162 if(tmr.read() > 4) // sec 00163 { 00164 led4=!led4; //Show that we are alive 00165 tmr.reset(); 00166 } 00167 } 00168 }
Generated on Sun Jul 24 2022 21:43:30 by 1.7.2