![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
server/client
Dependencies: mbed NetServicesMin
tcp_server.cpp@0:432b571e11a3, 2012-02-19 (annotated)
- Committer:
- recotana
- Date:
- Sun Feb 19 06:21:50 2012 +0000
- Revision:
- 0:432b571e11a3
- Child:
- 1:6e61ee662fd3
ver0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
recotana | 0:432b571e11a3 | 1 | #include "mbed.h" |
recotana | 0:432b571e11a3 | 2 | #include "config.h" |
recotana | 0:432b571e11a3 | 3 | #include "tcp_server.h" |
recotana | 0:432b571e11a3 | 4 | #include "tcp_client.h" |
recotana | 0:432b571e11a3 | 5 | |
recotana | 0:432b571e11a3 | 6 | TCPSocket listenSock; |
recotana | 0:432b571e11a3 | 7 | TCPSocket* recieveSock; |
recotana | 0:432b571e11a3 | 8 | Host client; |
recotana | 0:432b571e11a3 | 9 | IpAddr inCommingIp; |
recotana | 0:432b571e11a3 | 10 | |
recotana | 0:432b571e11a3 | 11 | extern bool isHostConnected; |
recotana | 0:432b571e11a3 | 12 | int serverInit(void) |
recotana | 0:432b571e11a3 | 13 | { |
recotana | 0:432b571e11a3 | 14 | listenSock.setOnEvent(&onServerListnerEvent) ; |
recotana | 0:432b571e11a3 | 15 | |
recotana | 0:432b571e11a3 | 16 | printf("Bindding...\r\n") ; |
recotana | 0:432b571e11a3 | 17 | |
recotana | 0:432b571e11a3 | 18 | if ( listenSock.bind(Host(IpAddr(), kServerPort)) != ETH_OK ){ |
recotana | 0:432b571e11a3 | 19 | printf("Bindding Error.\r\n") ; |
recotana | 0:432b571e11a3 | 20 | return -1 ; |
recotana | 0:432b571e11a3 | 21 | } |
recotana | 0:432b571e11a3 | 22 | |
recotana | 0:432b571e11a3 | 23 | printf("Listen...\r\n"); |
recotana | 0:432b571e11a3 | 24 | |
recotana | 0:432b571e11a3 | 25 | if ( listenSock.listen() != ETH_OK ){ |
recotana | 0:432b571e11a3 | 26 | printf("Listen Error.\r\n") ; |
recotana | 0:432b571e11a3 | 27 | return -1 ; |
recotana | 0:432b571e11a3 | 28 | } |
recotana | 0:432b571e11a3 | 29 | return 0; |
recotana | 0:432b571e11a3 | 30 | } |
recotana | 0:432b571e11a3 | 31 | |
recotana | 0:432b571e11a3 | 32 | //Listner Event |
recotana | 0:432b571e11a3 | 33 | void onServerListnerEvent(TCPSocketEvent e) |
recotana | 0:432b571e11a3 | 34 | { |
recotana | 0:432b571e11a3 | 35 | printf("server ---IN TCPSocketEvent ---\r\n"); |
recotana | 0:432b571e11a3 | 36 | |
recotana | 0:432b571e11a3 | 37 | if ( e == TCPSOCKET_ACCEPT ){ |
recotana | 0:432b571e11a3 | 38 | printf("Listening: TCP Socket Accepted\r\n"); |
recotana | 0:432b571e11a3 | 39 | |
recotana | 0:432b571e11a3 | 40 | if ( listenSock.accept(&client, &recieveSock) != TCPSOCKET_OK ) { |
recotana | 0:432b571e11a3 | 41 | printf("onTcpSocketEvent Error \r\n"); |
recotana | 0:432b571e11a3 | 42 | return; |
recotana | 0:432b571e11a3 | 43 | } |
recotana | 0:432b571e11a3 | 44 | |
recotana | 0:432b571e11a3 | 45 | recieveSock->setOnEvent(&onServerRecievetEvent); |
recotana | 0:432b571e11a3 | 46 | |
recotana | 0:432b571e11a3 | 47 | //Get Client Side IP Address |
recotana | 0:432b571e11a3 | 48 | inCommingIp = client.getIp(); |
recotana | 0:432b571e11a3 | 49 | printf("Controler IP Address is [%d.%d.%d.%d].\r\n", inCommingIp[0], inCommingIp[1], inCommingIp[2], inCommingIp[3]); |
recotana | 0:432b571e11a3 | 50 | } |
recotana | 0:432b571e11a3 | 51 | |
recotana | 0:432b571e11a3 | 52 | printf("server --- OUT TCPSocketEvent ---\r\n\r\n") ; |
recotana | 0:432b571e11a3 | 53 | } |
recotana | 0:432b571e11a3 | 54 | |
recotana | 0:432b571e11a3 | 55 | void onServerRecievetEvent(TCPSocketEvent e) |
recotana | 0:432b571e11a3 | 56 | { |
recotana | 0:432b571e11a3 | 57 | printf("server --- IN ConnectEvent ---\r\n"); |
recotana | 0:432b571e11a3 | 58 | |
recotana | 0:432b571e11a3 | 59 | |
recotana | 0:432b571e11a3 | 60 | switch(e) |
recotana | 0:432b571e11a3 | 61 | { |
recotana | 0:432b571e11a3 | 62 | case TCPSOCKET_CONNECTED: |
recotana | 0:432b571e11a3 | 63 | printf("Connected to host.\r\n") ; |
recotana | 0:432b571e11a3 | 64 | break; |
recotana | 0:432b571e11a3 | 65 | |
recotana | 0:432b571e11a3 | 66 | case TCPSOCKET_WRITEABLE: |
recotana | 0:432b571e11a3 | 67 | printf("Can write data to buf.\r\n"); |
recotana | 0:432b571e11a3 | 68 | break; |
recotana | 0:432b571e11a3 | 69 | |
recotana | 0:432b571e11a3 | 70 | case TCPSOCKET_READABLE: |
recotana | 0:432b571e11a3 | 71 | printf("Data in buf.\r\n"); |
recotana | 0:432b571e11a3 | 72 | char buf[128] ; |
recotana | 0:432b571e11a3 | 73 | while ( int len = recieveSock->recv(buf,sizeof(buf)) ) { |
recotana | 0:432b571e11a3 | 74 | |
recotana | 0:432b571e11a3 | 75 | int err =tcpSend(buf, len); |
recotana | 0:432b571e11a3 | 76 | |
recotana | 0:432b571e11a3 | 77 | recieveDump(buf,len); |
recotana | 0:432b571e11a3 | 78 | } |
recotana | 0:432b571e11a3 | 79 | break; |
recotana | 0:432b571e11a3 | 80 | |
recotana | 0:432b571e11a3 | 81 | case TCPSOCKET_CONTIMEOUT: |
recotana | 0:432b571e11a3 | 82 | printf("Connection timed out.\r\n"); |
recotana | 0:432b571e11a3 | 83 | break ; |
recotana | 0:432b571e11a3 | 84 | |
recotana | 0:432b571e11a3 | 85 | case TCPSOCKET_CONRST: |
recotana | 0:432b571e11a3 | 86 | printf("Connection was reset by remote host.\r\n"); |
recotana | 0:432b571e11a3 | 87 | break ; |
recotana | 0:432b571e11a3 | 88 | |
recotana | 0:432b571e11a3 | 89 | case TCPSOCKET_CONABRT: |
recotana | 0:432b571e11a3 | 90 | printf("Connection was aborted.\r\n") ; |
recotana | 0:432b571e11a3 | 91 | break ; |
recotana | 0:432b571e11a3 | 92 | |
recotana | 0:432b571e11a3 | 93 | case TCPSOCKET_ERROR: |
recotana | 0:432b571e11a3 | 94 | printf("Unknown error.\r\n") ; |
recotana | 0:432b571e11a3 | 95 | break ; |
recotana | 0:432b571e11a3 | 96 | |
recotana | 0:432b571e11a3 | 97 | case TCPSOCKET_DISCONNECTED: |
recotana | 0:432b571e11a3 | 98 | printf("Tcp Socket Disconnected\r\n") ; |
recotana | 0:432b571e11a3 | 99 | recieveSock->close() ; |
recotana | 0:432b571e11a3 | 100 | break; |
recotana | 0:432b571e11a3 | 101 | } |
recotana | 0:432b571e11a3 | 102 | printf("--- OUT ConnectEvent ---\r\n\r\n"); |
recotana | 0:432b571e11a3 | 103 | } |
recotana | 0:432b571e11a3 | 104 | |
recotana | 0:432b571e11a3 | 105 | void recieveDump(char *data , int length){ |
recotana | 0:432b571e11a3 | 106 | printf("size = %d\n -- -id\n",length); |
recotana | 0:432b571e11a3 | 107 | |
recotana | 0:432b571e11a3 | 108 | for(int i=0;i<16;i++) printf("%d:%0x ",i,data[i]); |
recotana | 0:432b571e11a3 | 109 | printf("\n--- data\n"); |
recotana | 0:432b571e11a3 | 110 | |
recotana | 0:432b571e11a3 | 111 | int count=0; |
recotana | 0:432b571e11a3 | 112 | for(int i=0;i<(length-16);i++){ |
recotana | 0:432b571e11a3 | 113 | printf("%02d:%02x ",i,data[16+i]); |
recotana | 0:432b571e11a3 | 114 | if( count==15 ) {count=0; printf("\n");} |
recotana | 0:432b571e11a3 | 115 | else count++; |
recotana | 0:432b571e11a3 | 116 | } |
recotana | 0:432b571e11a3 | 117 | |
recotana | 0:432b571e11a3 | 118 | printf("\n---frame end\n"); |
recotana | 0:432b571e11a3 | 119 | } |
recotana | 0:432b571e11a3 | 120 |