Based on Shigeki KOMATSU program
Dependencies: mbed EthernetNetIf
main.cpp@0:92055da1fc21, 2011-05-03 (annotated)
- Committer:
- nenad
- Date:
- Tue May 03 20:08:27 2011 +0000
- Revision:
- 0:92055da1fc21
- Child:
- 1:8c2bac2ae93d
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nenad | 0:92055da1fc21 | 1 | |
nenad | 0:92055da1fc21 | 2 | // TCP Echo server |
nenad | 0:92055da1fc21 | 3 | // 2010/9/7 |
nenad | 0:92055da1fc21 | 4 | |
nenad | 0:92055da1fc21 | 5 | |
nenad | 0:92055da1fc21 | 6 | /* |
nenad | 0:92055da1fc21 | 7 | |
nenad | 0:92055da1fc21 | 8 | (Execute Sample) |
nenad | 0:92055da1fc21 | 9 | PC side: |
nenad | 0:92055da1fc21 | 10 | telnet 192.168.0.25 12345 |
nenad | 0:92055da1fc21 | 11 | Trying 192.168.0.25... |
nenad | 0:92055da1fc21 | 12 | Connected to 192.168.0.25. |
nenad | 0:92055da1fc21 | 13 | Escape character is '^]'. |
nenad | 0:92055da1fc21 | 14 | |
nenad | 0:92055da1fc21 | 15 | mbed side: |
nenad | 0:92055da1fc21 | 16 | Setup OK |
nenad | 0:92055da1fc21 | 17 | mbed IP Address is 192.168.0.25 |
nenad | 0:92055da1fc21 | 18 | Binding.. |
nenad | 0:92055da1fc21 | 19 | Listening... |
nenad | 0:92055da1fc21 | 20 | Listening: TCP Socket Accepted |
nenad | 0:92055da1fc21 | 21 | Listening: Incoming TCP connection from 192.168.0.7 |
nenad | 0:92055da1fc21 | 22 | TCP Socket Readable |
nenad | 0:92055da1fc21 | 23 | Received&Wrote:test text |
nenad | 0:92055da1fc21 | 24 | |
nenad | 0:92055da1fc21 | 25 | TCP Socket Writable |
nenad | 0:92055da1fc21 | 26 | |
nenad | 0:92055da1fc21 | 27 | |
nenad | 0:92055da1fc21 | 28 | */ |
nenad | 0:92055da1fc21 | 29 | |
nenad | 0:92055da1fc21 | 30 | #include "mbed.h" |
nenad | 0:92055da1fc21 | 31 | #include "EthernetNetIf.h" |
nenad | 0:92055da1fc21 | 32 | #include "TCPSocket.h" |
nenad | 0:92055da1fc21 | 33 | |
nenad | 0:92055da1fc21 | 34 | DigitalOut led4(LED4, "led4"); |
nenad | 0:92055da1fc21 | 35 | |
nenad | 0:92055da1fc21 | 36 | //EthernetNetIf eth; |
nenad | 0:92055da1fc21 | 37 | EthernetNetIf eth( |
nenad | 0:92055da1fc21 | 38 | IpAddr(192,168,1,110), //IP Address |
nenad | 0:92055da1fc21 | 39 | IpAddr(255,255,255,0), //Network Mask |
nenad | 0:92055da1fc21 | 40 | IpAddr(192,168,1,1), //Gateway |
nenad | 0:92055da1fc21 | 41 | IpAddr(192,168,1,1) //DNS |
nenad | 0:92055da1fc21 | 42 | ); |
nenad | 0:92055da1fc21 | 43 | |
nenad | 0:92055da1fc21 | 44 | #define TCP_LISTENING_PORT 12345 |
nenad | 0:92055da1fc21 | 45 | |
nenad | 0:92055da1fc21 | 46 | TCPSocket ListeningSock; |
nenad | 0:92055da1fc21 | 47 | TCPSocket* pConnectedSock; // for ConnectedSock |
nenad | 0:92055da1fc21 | 48 | Host client; |
nenad | 0:92055da1fc21 | 49 | TCPSocketErr err; |
nenad | 0:92055da1fc21 | 50 | |
nenad | 0:92055da1fc21 | 51 | void onConnectedTCPSocketEvent(TCPSocketEvent e) |
nenad | 0:92055da1fc21 | 52 | { |
nenad | 0:92055da1fc21 | 53 | switch(e) |
nenad | 0:92055da1fc21 | 54 | { |
nenad | 0:92055da1fc21 | 55 | case TCPSOCKET_CONNECTED: |
nenad | 0:92055da1fc21 | 56 | printf("TCP Socket Connected\r\n"); |
nenad | 0:92055da1fc21 | 57 | break; |
nenad | 0:92055da1fc21 | 58 | case TCPSOCKET_WRITEABLE: |
nenad | 0:92055da1fc21 | 59 | //Can now write some data... |
nenad | 0:92055da1fc21 | 60 | //printf("TCP Socket Writable\r\n"); |
nenad | 0:92055da1fc21 | 61 | break; |
nenad | 0:92055da1fc21 | 62 | case TCPSOCKET_READABLE: |
nenad | 0:92055da1fc21 | 63 | //Can now read dome data... |
nenad | 0:92055da1fc21 | 64 | //printf("TCP Socket Readable\r\n"); |
nenad | 0:92055da1fc21 | 65 | // Read in any available data into the buffer |
nenad | 0:92055da1fc21 | 66 | char buff[128]; |
nenad | 0:92055da1fc21 | 67 | while ( int len = pConnectedSock->recv(buff, 128) ) { |
nenad | 0:92055da1fc21 | 68 | // And send straight back out again |
nenad | 0:92055da1fc21 | 69 | pConnectedSock->send(buff, len); |
nenad | 0:92055da1fc21 | 70 | buff[len]=0; // make terminater |
nenad | 0:92055da1fc21 | 71 | printf("Received from and sent back to client:%s\r\n",buff); |
nenad | 0:92055da1fc21 | 72 | } |
nenad | 0:92055da1fc21 | 73 | break; |
nenad | 0:92055da1fc21 | 74 | case TCPSOCKET_CONTIMEOUT: |
nenad | 0:92055da1fc21 | 75 | printf("TCP Socket Timeout\r\n"); |
nenad | 0:92055da1fc21 | 76 | break; |
nenad | 0:92055da1fc21 | 77 | case TCPSOCKET_CONRST: |
nenad | 0:92055da1fc21 | 78 | printf("TCP Socket CONRST\r\n"); |
nenad | 0:92055da1fc21 | 79 | break; |
nenad | 0:92055da1fc21 | 80 | case TCPSOCKET_CONABRT: |
nenad | 0:92055da1fc21 | 81 | printf("TCP Socket CONABRT\r\n"); |
nenad | 0:92055da1fc21 | 82 | break; |
nenad | 0:92055da1fc21 | 83 | case TCPSOCKET_ERROR: |
nenad | 0:92055da1fc21 | 84 | printf("TCP Socket Error\r\n"); |
nenad | 0:92055da1fc21 | 85 | break; |
nenad | 0:92055da1fc21 | 86 | case TCPSOCKET_DISCONNECTED: |
nenad | 0:92055da1fc21 | 87 | //Close socket... |
nenad | 0:92055da1fc21 | 88 | printf("TCP Socket Disconnected\r\n"); |
nenad | 0:92055da1fc21 | 89 | pConnectedSock->close(); |
nenad | 0:92055da1fc21 | 90 | break; |
nenad | 0:92055da1fc21 | 91 | default: |
nenad | 0:92055da1fc21 | 92 | printf("DEFAULT\r\n"); |
nenad | 0:92055da1fc21 | 93 | } |
nenad | 0:92055da1fc21 | 94 | } |
nenad | 0:92055da1fc21 | 95 | |
nenad | 0:92055da1fc21 | 96 | |
nenad | 0:92055da1fc21 | 97 | void onListeningTCPSocketEvent(TCPSocketEvent e) |
nenad | 0:92055da1fc21 | 98 | { |
nenad | 0:92055da1fc21 | 99 | switch(e) |
nenad | 0:92055da1fc21 | 100 | { |
nenad | 0:92055da1fc21 | 101 | case TCPSOCKET_ACCEPT: |
nenad | 0:92055da1fc21 | 102 | printf("Listening: TCP Socket Accepted\r\n"); |
nenad | 0:92055da1fc21 | 103 | // Accepts connection from client and gets connected socket. |
nenad | 0:92055da1fc21 | 104 | err=ListeningSock.accept(&client, &pConnectedSock); |
nenad | 0:92055da1fc21 | 105 | if (err) { |
nenad | 0:92055da1fc21 | 106 | printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); |
nenad | 0:92055da1fc21 | 107 | return; //Error in accept, discard connection |
nenad | 0:92055da1fc21 | 108 | } |
nenad | 0:92055da1fc21 | 109 | // Setup the new socket events |
nenad | 0:92055da1fc21 | 110 | pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); |
nenad | 0:92055da1fc21 | 111 | // We can find out from where the connection is coming by looking at the |
nenad | 0:92055da1fc21 | 112 | // Host parameter of the accept() method |
nenad | 0:92055da1fc21 | 113 | IpAddr clientIp = client.getIp(); |
nenad | 0:92055da1fc21 | 114 | printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", |
nenad | 0:92055da1fc21 | 115 | clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
nenad | 0:92055da1fc21 | 116 | break; |
nenad | 0:92055da1fc21 | 117 | // the following cases will not happen |
nenad | 0:92055da1fc21 | 118 | case TCPSOCKET_CONNECTED: |
nenad | 0:92055da1fc21 | 119 | printf("Listening: TCP Socket Connected\r\n"); |
nenad | 0:92055da1fc21 | 120 | break; |
nenad | 0:92055da1fc21 | 121 | case TCPSOCKET_WRITEABLE: |
nenad | 0:92055da1fc21 | 122 | printf("Listening: TCP Socket Writable\r\n"); |
nenad | 0:92055da1fc21 | 123 | break; |
nenad | 0:92055da1fc21 | 124 | case TCPSOCKET_READABLE: |
nenad | 0:92055da1fc21 | 125 | printf("Listening: TCP Socket Readable\r\n"); |
nenad | 0:92055da1fc21 | 126 | break; |
nenad | 0:92055da1fc21 | 127 | case TCPSOCKET_CONTIMEOUT: |
nenad | 0:92055da1fc21 | 128 | printf("Listening: TCP Socket Timeout\r\n"); |
nenad | 0:92055da1fc21 | 129 | break; |
nenad | 0:92055da1fc21 | 130 | case TCPSOCKET_CONRST: |
nenad | 0:92055da1fc21 | 131 | printf("Listening: TCP Socket CONRST\r\n"); |
nenad | 0:92055da1fc21 | 132 | break; |
nenad | 0:92055da1fc21 | 133 | case TCPSOCKET_CONABRT: |
nenad | 0:92055da1fc21 | 134 | printf("Listening: TCP Socket CONABRT\r\n"); |
nenad | 0:92055da1fc21 | 135 | break; |
nenad | 0:92055da1fc21 | 136 | case TCPSOCKET_ERROR: |
nenad | 0:92055da1fc21 | 137 | printf("Listening: TCP Socket Error\r\n"); |
nenad | 0:92055da1fc21 | 138 | break; |
nenad | 0:92055da1fc21 | 139 | case TCPSOCKET_DISCONNECTED: |
nenad | 0:92055da1fc21 | 140 | //Close socket... |
nenad | 0:92055da1fc21 | 141 | printf("Listening: TCP Socket Disconnected\r\n"); |
nenad | 0:92055da1fc21 | 142 | ListeningSock.close(); |
nenad | 0:92055da1fc21 | 143 | break; |
nenad | 0:92055da1fc21 | 144 | default: |
nenad | 0:92055da1fc21 | 145 | printf("DEFAULT\r\n"); |
nenad | 0:92055da1fc21 | 146 | }; |
nenad | 0:92055da1fc21 | 147 | } |
nenad | 0:92055da1fc21 | 148 | |
nenad | 0:92055da1fc21 | 149 | |
nenad | 0:92055da1fc21 | 150 | int main() { |
nenad | 0:92055da1fc21 | 151 | printf("\r\n"); |
nenad | 0:92055da1fc21 | 152 | printf("Setting up...\r\n"); |
nenad | 0:92055da1fc21 | 153 | EthernetErr ethErr = eth.setup();//initialisation of ethernet stack |
nenad | 0:92055da1fc21 | 154 | if(ethErr) |
nenad | 0:92055da1fc21 | 155 | { |
nenad | 0:92055da1fc21 | 156 | printf("Error %d in setup.\r\n", ethErr); |
nenad | 0:92055da1fc21 | 157 | return -1; |
nenad | 0:92055da1fc21 | 158 | } |
nenad | 0:92055da1fc21 | 159 | printf("Setup OK\r\n"); |
nenad | 0:92055da1fc21 | 160 | |
nenad | 0:92055da1fc21 | 161 | IpAddr ip = eth.getIp(); |
nenad | 0:92055da1fc21 | 162 | printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);//address of server |
nenad | 0:92055da1fc21 | 163 | |
nenad | 0:92055da1fc21 | 164 | |
nenad | 0:92055da1fc21 | 165 | // Set the callbacks for Listening |
nenad | 0:92055da1fc21 | 166 | ListeningSock.setOnEvent(&onListeningTCPSocketEvent); |
nenad | 0:92055da1fc21 | 167 | |
nenad | 0:92055da1fc21 | 168 | // bind and listen on TCP |
nenad | 0:92055da1fc21 | 169 | err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); |
nenad | 0:92055da1fc21 | 170 | printf("Binding..\r\n"); |
nenad | 0:92055da1fc21 | 171 | |
nenad | 0:92055da1fc21 | 172 | if(err) |
nenad | 0:92055da1fc21 | 173 | { |
nenad | 0:92055da1fc21 | 174 | //Deal with that error... |
nenad | 0:92055da1fc21 | 175 | printf("Binding Error\n"); |
nenad | 0:92055da1fc21 | 176 | } |
nenad | 0:92055da1fc21 | 177 | err=ListeningSock.listen(); // Starts listening |
nenad | 0:92055da1fc21 | 178 | printf("Listening...\r\n"); |
nenad | 0:92055da1fc21 | 179 | if(err) |
nenad | 0:92055da1fc21 | 180 | { |
nenad | 0:92055da1fc21 | 181 | printf("Listening Error\r\n"); |
nenad | 0:92055da1fc21 | 182 | } |
nenad | 0:92055da1fc21 | 183 | |
nenad | 0:92055da1fc21 | 184 | Timer tmr; |
nenad | 0:92055da1fc21 | 185 | tmr.start(); |
nenad | 0:92055da1fc21 | 186 | |
nenad | 0:92055da1fc21 | 187 | while(true) |
nenad | 0:92055da1fc21 | 188 | { |
nenad | 0:92055da1fc21 | 189 | Net::poll(); |
nenad | 0:92055da1fc21 | 190 | if(tmr.read() > 4) // sec |
nenad | 0:92055da1fc21 | 191 | { |
nenad | 0:92055da1fc21 | 192 | led4=!led4; //Show that we are alive |
nenad | 0:92055da1fc21 | 193 | tmr.reset(); |
nenad | 0:92055da1fc21 | 194 | } |
nenad | 0:92055da1fc21 | 195 | } |
nenad | 0:92055da1fc21 | 196 | } |