![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
no working tcp
main.cpp@0:39a067b5ae32, 2010-12-24 (annotated)
- Committer:
- brleme
- Date:
- Fri Dec 24 00:37:51 2010 +0000
- Revision:
- 0:39a067b5ae32
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brleme | 0:39a067b5ae32 | 1 | #include "mbed.h" |
brleme | 0:39a067b5ae32 | 2 | #include "EthernetNetIf.h" |
brleme | 0:39a067b5ae32 | 3 | #include "TCPSocket.h" |
brleme | 0:39a067b5ae32 | 4 | |
brleme | 0:39a067b5ae32 | 5 | |
brleme | 0:39a067b5ae32 | 6 | |
brleme | 0:39a067b5ae32 | 7 | EthernetNetIf eth; |
brleme | 0:39a067b5ae32 | 8 | TCPSocket tcp; |
brleme | 0:39a067b5ae32 | 9 | Host server(IpAddr(192,168,15,105), 2222); |
brleme | 0:39a067b5ae32 | 10 | |
brleme | 0:39a067b5ae32 | 11 | DigitalOut ledeth(LED1); |
brleme | 0:39a067b5ae32 | 12 | DigitalOut ledhost(LED2); |
brleme | 0:39a067b5ae32 | 13 | DigitalOut ledsendo(LED3); |
brleme | 0:39a067b5ae32 | 14 | DigitalOut ledsende(LED4); |
brleme | 0:39a067b5ae32 | 15 | |
brleme | 0:39a067b5ae32 | 16 | |
brleme | 0:39a067b5ae32 | 17 | struct |
brleme | 0:39a067b5ae32 | 18 | { |
brleme | 0:39a067b5ae32 | 19 | int connected; |
brleme | 0:39a067b5ae32 | 20 | int writeable; |
brleme | 0:39a067b5ae32 | 21 | }flags; |
brleme | 0:39a067b5ae32 | 22 | |
brleme | 0:39a067b5ae32 | 23 | |
brleme | 0:39a067b5ae32 | 24 | void onTCPSocketEvent(TCPSocketEvent e) |
brleme | 0:39a067b5ae32 | 25 | { |
brleme | 0:39a067b5ae32 | 26 | //printf("New TCPSocketEvent: %d",e); |
brleme | 0:39a067b5ae32 | 27 | switch(e) |
brleme | 0:39a067b5ae32 | 28 | { |
brleme | 0:39a067b5ae32 | 29 | case TCPSOCKET_CONNECTED: flags.connected = 1;break; |
brleme | 0:39a067b5ae32 | 30 | |
brleme | 0:39a067b5ae32 | 31 | case TCPSOCKET_WRITEABLE: flags.writeable = 1;break; |
brleme | 0:39a067b5ae32 | 32 | |
brleme | 0:39a067b5ae32 | 33 | case TCPSOCKET_CONTIMEOUT: |
brleme | 0:39a067b5ae32 | 34 | case TCPSOCKET_CONRST: |
brleme | 0:39a067b5ae32 | 35 | case TCPSOCKET_CONABRT: |
brleme | 0:39a067b5ae32 | 36 | case TCPSOCKET_ERROR: |
brleme | 0:39a067b5ae32 | 37 | case TCPSOCKET_DISCONNECTED: |
brleme | 0:39a067b5ae32 | 38 | { |
brleme | 0:39a067b5ae32 | 39 | tcp.close(); |
brleme | 0:39a067b5ae32 | 40 | flags.connected = 0; |
brleme | 0:39a067b5ae32 | 41 | }break; |
brleme | 0:39a067b5ae32 | 42 | } |
brleme | 0:39a067b5ae32 | 43 | } |
brleme | 0:39a067b5ae32 | 44 | |
brleme | 0:39a067b5ae32 | 45 | void sendMessage(Host host, char* message, int msgLen) |
brleme | 0:39a067b5ae32 | 46 | { |
brleme | 0:39a067b5ae32 | 47 | |
brleme | 0:39a067b5ae32 | 48 | TCPSocketErr err = tcp.connect(host); |
brleme | 0:39a067b5ae32 | 49 | if(err) |
brleme | 0:39a067b5ae32 | 50 | ledhost=0; |
brleme | 0:39a067b5ae32 | 51 | else |
brleme | 0:39a067b5ae32 | 52 | ledhost=1; |
brleme | 0:39a067b5ae32 | 53 | |
brleme | 0:39a067b5ae32 | 54 | if(/*flags.connected && flags.writeable*/true) |
brleme | 0:39a067b5ae32 | 55 | { |
brleme | 0:39a067b5ae32 | 56 | // printf("Sending data\n"); |
brleme | 0:39a067b5ae32 | 57 | flags.writeable = 0; |
brleme | 0:39a067b5ae32 | 58 | int transLen = tcp.send(message,msgLen); |
brleme | 0:39a067b5ae32 | 59 | if(transLen != msgLen) |
brleme | 0:39a067b5ae32 | 60 | ledsende=1; |
brleme | 0:39a067b5ae32 | 61 | // printf("Error in transmission\n"); |
brleme | 0:39a067b5ae32 | 62 | } |
brleme | 0:39a067b5ae32 | 63 | tcp.close(); |
brleme | 0:39a067b5ae32 | 64 | } |
brleme | 0:39a067b5ae32 | 65 | |
brleme | 0:39a067b5ae32 | 66 | int main() |
brleme | 0:39a067b5ae32 | 67 | { |
brleme | 0:39a067b5ae32 | 68 | EthernetErr ethErr = eth.setup(); |
brleme | 0:39a067b5ae32 | 69 | if(ethErr) |
brleme | 0:39a067b5ae32 | 70 | ledeth=0; |
brleme | 0:39a067b5ae32 | 71 | else |
brleme | 0:39a067b5ae32 | 72 | ledeth=1; |
brleme | 0:39a067b5ae32 | 73 | |
brleme | 0:39a067b5ae32 | 74 | tcp.setOnEvent(&onTCPSocketEvent); |
brleme | 0:39a067b5ae32 | 75 | while(true) |
brleme | 0:39a067b5ae32 | 76 | { |
brleme | 0:39a067b5ae32 | 77 | Net::poll(); |
brleme | 0:39a067b5ae32 | 78 | sendMessage(server,"Hello Server\n",13); |
brleme | 0:39a067b5ae32 | 79 | wait(5); |
brleme | 0:39a067b5ae32 | 80 | } |
brleme | 0:39a067b5ae32 | 81 | } |