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