![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
server/client
Dependencies: mbed NetServicesMin
Diff: tcp_client.cpp
- Revision:
- 0:432b571e11a3
- Child:
- 1:6e61ee662fd3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tcp_client.cpp Sun Feb 19 06:21:50 2012 +0000 @@ -0,0 +1,133 @@ +#include "mbed.h" +#include "config.h" +#include "tcp_error.h" +#include "tcp_client.h" + +extern IpAddr inCommingIp; + +Host server; +TCPSocket sendSock; +ConnectStatus clientStatus; +Timer checkTimer; +Ticker clientCheck; + +IpAddr clientIp; +int clientPort; + +void onSendEvent (TCPSocketEvent e) { + printf("... client event arrival!\n"); + switch(e) + { + case TCPSOCKET_CONNECTED: // Ú±®¹ + { + char str[]="connect!"; + sendSock.send(str,strlen(str) ); // M + clientStatus=connected; + printf("TCPSOCKET_CONNECTED!!\n"); + break; + } + + case TCPSOCKET_WRITEABLE: // MÂ\iM®¹j + { + printf("TCPSOCKET_WRITEABLE!!\n"); + clientStatus=connected; + break; + } + + case TCPSOCKET_READABLE: // óMf[^ è + { printf("TCPSOCKET_READABLE!!\n"); + clientStatus=connected; + break; + } + case TCPSOCKET_CONTIMEOUT: + { + printf("TCPSOCKET_CONTIMEOUT!!\n"); + socketClose(); + break; + } + case TCPSOCKET_CONRST: + { + printf("TCPSOCKET_CONRST!!\n"); + socketClose(); + break; + } + case TCPSOCKET_CONABRT: + { + printf("TCPSOCKET_CONABRT!!\n"); + socketClose(); + break; + } + case TCPSOCKET_ERROR: + { printf("TCPSOCKET_ERROR\n"); + socketClose(); + break; + + } + case TCPSOCKET_DISCONNECTED: + { + printf("TCPSOCKET_DISCONNECTED...\n"); + socketClose(); + break; + } + } + printf("...client event out\n"); +} + +void clientInit(void){ + sendSock.setOnEvent(&onSendEvent); + clientStatus=close; +} + +int clientConnect(IpAddr ip , int port) +{ + if(clientStatus == connecting){ + printf("socket already connecting..\n"); + return 0; + } + else if(clientStatus == connected){ + printf("socket already connected..\n"); + return 0; + } + + Host server(ip, port); + + TCPSocketErr err= sendSock.connect(server); + if (err != TCPSOCKET_OK) { + printf("connect Error. "); + dispTcpError(err); + clientStatus=close; + return -1; + } + clientStatus=connecting; + return 0; +} + +int tcpSend(char *data,int length){ + + return sendSock.send(data, length); + +} + +void checkConnect(void){ + + if(clientStatus != close) return; + + clientConnect( clientIp,clientPort ); + return; +} + +void socketClose(void){ + sendSock.close(); + clientStatus=close; +} + + + +void connectCheckStart(IpAddr ip , int port) +{ + clientIp=ip; + clientPort=port; + clientCheck.attach(&checkConnect,3); + +} +