server/client

Dependencies:   mbed NetServicesMin

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?

UserRevisionLine numberNew contents of line
recotana 0:432b571e11a3 1 #include "mbed.h"
recotana 0:432b571e11a3 2 #include "config.h"
recotana 0:432b571e11a3 3 #include "tcp_error.h"
recotana 0:432b571e11a3 4 #include "tcp_client.h"
recotana 0:432b571e11a3 5
recotana 0:432b571e11a3 6 extern IpAddr inCommingIp;
recotana 0:432b571e11a3 7
recotana 0:432b571e11a3 8 Host server;
recotana 0:432b571e11a3 9 TCPSocket sendSock;
recotana 0:432b571e11a3 10 ConnectStatus clientStatus;
recotana 0:432b571e11a3 11 Timer checkTimer;
recotana 0:432b571e11a3 12 Ticker clientCheck;
recotana 0:432b571e11a3 13
recotana 0:432b571e11a3 14 IpAddr clientIp;
recotana 0:432b571e11a3 15 int clientPort;
recotana 0:432b571e11a3 16
recotana 0:432b571e11a3 17 void onSendEvent (TCPSocketEvent e) {
recotana 0:432b571e11a3 18 printf("... client event arrival!\n");
recotana 0:432b571e11a3 19 switch(e)
recotana 0:432b571e11a3 20 {
recotana 0:432b571e11a3 21 case TCPSOCKET_CONNECTED: // Ú‘±Š®—¹
recotana 0:432b571e11a3 22 {
recotana 0:432b571e11a3 23 char str[]="connect!";
recotana 0:432b571e11a3 24 sendSock.send(str,strlen(str) ); // ‘—M
recotana 0:432b571e11a3 25 clientStatus=connected;
recotana 0:432b571e11a3 26 printf("TCPSOCKET_CONNECTED!!\n");
recotana 0:432b571e11a3 27 break;
recotana 0:432b571e11a3 28 }
recotana 0:432b571e11a3 29
recotana 0:432b571e11a3 30 case TCPSOCKET_WRITEABLE: // ‘—M‰Â”\i‘—MŠ®—¹j
recotana 0:432b571e11a3 31 {
recotana 0:432b571e11a3 32 printf("TCPSOCKET_WRITEABLE!!\n");
recotana 0:432b571e11a3 33 clientStatus=connected;
recotana 0:432b571e11a3 34 break;
recotana 0:432b571e11a3 35 }
recotana 0:432b571e11a3 36
recotana 0:432b571e11a3 37 case TCPSOCKET_READABLE: // ŽóMƒf[ƒ^‚ ‚è
recotana 0:432b571e11a3 38 { printf("TCPSOCKET_READABLE!!\n");
recotana 0:432b571e11a3 39 clientStatus=connected;
recotana 0:432b571e11a3 40 break;
recotana 0:432b571e11a3 41 }
recotana 0:432b571e11a3 42 case TCPSOCKET_CONTIMEOUT:
recotana 0:432b571e11a3 43 {
recotana 0:432b571e11a3 44 printf("TCPSOCKET_CONTIMEOUT!!\n");
recotana 0:432b571e11a3 45 socketClose();
recotana 0:432b571e11a3 46 break;
recotana 0:432b571e11a3 47 }
recotana 0:432b571e11a3 48 case TCPSOCKET_CONRST:
recotana 0:432b571e11a3 49 {
recotana 0:432b571e11a3 50 printf("TCPSOCKET_CONRST!!\n");
recotana 0:432b571e11a3 51 socketClose();
recotana 0:432b571e11a3 52 break;
recotana 0:432b571e11a3 53 }
recotana 0:432b571e11a3 54 case TCPSOCKET_CONABRT:
recotana 0:432b571e11a3 55 {
recotana 0:432b571e11a3 56 printf("TCPSOCKET_CONABRT!!\n");
recotana 0:432b571e11a3 57 socketClose();
recotana 0:432b571e11a3 58 break;
recotana 0:432b571e11a3 59 }
recotana 0:432b571e11a3 60 case TCPSOCKET_ERROR:
recotana 0:432b571e11a3 61 { printf("TCPSOCKET_ERROR\n");
recotana 0:432b571e11a3 62 socketClose();
recotana 0:432b571e11a3 63 break;
recotana 0:432b571e11a3 64
recotana 0:432b571e11a3 65 }
recotana 0:432b571e11a3 66 case TCPSOCKET_DISCONNECTED:
recotana 0:432b571e11a3 67 {
recotana 0:432b571e11a3 68 printf("TCPSOCKET_DISCONNECTED...\n");
recotana 0:432b571e11a3 69 socketClose();
recotana 0:432b571e11a3 70 break;
recotana 0:432b571e11a3 71 }
recotana 0:432b571e11a3 72 }
recotana 0:432b571e11a3 73 printf("...client event out\n");
recotana 0:432b571e11a3 74 }
recotana 0:432b571e11a3 75
recotana 0:432b571e11a3 76 void clientInit(void){
recotana 0:432b571e11a3 77 sendSock.setOnEvent(&onSendEvent);
recotana 0:432b571e11a3 78 clientStatus=close;
recotana 0:432b571e11a3 79 }
recotana 0:432b571e11a3 80
recotana 0:432b571e11a3 81 int clientConnect(IpAddr ip , int port)
recotana 0:432b571e11a3 82 {
recotana 0:432b571e11a3 83 if(clientStatus == connecting){
recotana 0:432b571e11a3 84 printf("socket already connecting..\n");
recotana 0:432b571e11a3 85 return 0;
recotana 0:432b571e11a3 86 }
recotana 0:432b571e11a3 87 else if(clientStatus == connected){
recotana 0:432b571e11a3 88 printf("socket already connected..\n");
recotana 0:432b571e11a3 89 return 0;
recotana 0:432b571e11a3 90 }
recotana 0:432b571e11a3 91
recotana 0:432b571e11a3 92 Host server(ip, port);
recotana 0:432b571e11a3 93
recotana 0:432b571e11a3 94 TCPSocketErr err= sendSock.connect(server);
recotana 0:432b571e11a3 95 if (err != TCPSOCKET_OK) {
recotana 0:432b571e11a3 96 printf("connect Error. ");
recotana 0:432b571e11a3 97 dispTcpError(err);
recotana 0:432b571e11a3 98 clientStatus=close;
recotana 0:432b571e11a3 99 return -1;
recotana 0:432b571e11a3 100 }
recotana 0:432b571e11a3 101 clientStatus=connecting;
recotana 0:432b571e11a3 102 return 0;
recotana 0:432b571e11a3 103 }
recotana 0:432b571e11a3 104
recotana 0:432b571e11a3 105 int tcpSend(char *data,int length){
recotana 0:432b571e11a3 106
recotana 0:432b571e11a3 107 return sendSock.send(data, length);
recotana 0:432b571e11a3 108
recotana 0:432b571e11a3 109 }
recotana 0:432b571e11a3 110
recotana 0:432b571e11a3 111 void checkConnect(void){
recotana 0:432b571e11a3 112
recotana 0:432b571e11a3 113 if(clientStatus != close) return;
recotana 0:432b571e11a3 114
recotana 0:432b571e11a3 115 clientConnect( clientIp,clientPort );
recotana 0:432b571e11a3 116 return;
recotana 0:432b571e11a3 117 }
recotana 0:432b571e11a3 118
recotana 0:432b571e11a3 119 void socketClose(void){
recotana 0:432b571e11a3 120 sendSock.close();
recotana 0:432b571e11a3 121 clientStatus=close;
recotana 0:432b571e11a3 122 }
recotana 0:432b571e11a3 123
recotana 0:432b571e11a3 124
recotana 0:432b571e11a3 125
recotana 0:432b571e11a3 126 void connectCheckStart(IpAddr ip , int port)
recotana 0:432b571e11a3 127 {
recotana 0:432b571e11a3 128 clientIp=ip;
recotana 0:432b571e11a3 129 clientPort=port;
recotana 0:432b571e11a3 130 clientCheck.attach(&checkConnect,3);
recotana 0:432b571e11a3 131
recotana 0:432b571e11a3 132 }
recotana 0:432b571e11a3 133