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