00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "mbed.h"
00031 #include "EthernetNetIf.h"
00032 #include "TCPSocket.h"
00033
00034 DigitalOut led4(LED4, "led4");
00035
00036
00037 EthernetNetIf eth(
00038 IpAddr(192,168,0,25),
00039 IpAddr(255,255,255,0),
00040 IpAddr(192,168,0,1),
00041 IpAddr(192,168,0,1)
00042 );
00043
00044 #define TCP_LISTENING_PORT 12345
00045
00046 TCPSocket ListeningSock;
00047 TCPSocket* pConnectedSock;
00048 Host client;
00049 TCPSocketErr err;
00050
00051 void onConnectedTCPSocketEvent(TCPSocketEvent e)
00052 {
00053 switch(e)
00054 {
00055 case TCPSOCKET_CONNECTED:
00056 printf("TCP Socket Connected\r\n");
00057 break;
00058 case TCPSOCKET_WRITEABLE:
00059
00060 printf("TCP Socket Writable\r\n");
00061 break;
00062 case TCPSOCKET_READABLE:
00063
00064 printf("TCP Socket Readable\r\n");
00065
00066 char buff[128];
00067 while ( int len = pConnectedSock->recv(buff, 128) ) {
00068
00069 pConnectedSock->send(buff, len);
00070 buff[len]=0;
00071 printf("Received&Wrote:%s\r\n",buff);
00072 }
00073 break;
00074 case TCPSOCKET_CONTIMEOUT:
00075 printf("TCP Socket Timeout\r\n");
00076 break;
00077 case TCPSOCKET_CONRST:
00078 printf("TCP Socket CONRST\r\n");
00079 break;
00080 case TCPSOCKET_CONABRT:
00081 printf("TCP Socket CONABRT\r\n");
00082 break;
00083 case TCPSOCKET_ERROR:
00084 printf("TCP Socket Error\r\n");
00085 break;
00086 case TCPSOCKET_DISCONNECTED:
00087
00088 printf("TCP Socket Disconnected\r\n");
00089 pConnectedSock->close();
00090 break;
00091 default:
00092 printf("DEFAULT\r\n");
00093 }
00094 }
00095
00096
00097 void onListeningTCPSocketEvent(TCPSocketEvent e)
00098 {
00099 switch(e)
00100 {
00101 case TCPSOCKET_ACCEPT:
00102 printf("Listening: TCP Socket Accepted\r\n");
00103
00104 err=ListeningSock.accept(&client, &pConnectedSock);
00105 if (err) {
00106 printf("onListeningTcpSocketEvent : Could not accept connection.\r\n");
00107 return;
00108 }
00109
00110 pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent);
00111
00112
00113 IpAddr clientIp = client.getIp();
00114 printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n",
00115 clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
00116 break;
00117
00118 case TCPSOCKET_CONNECTED:
00119 printf("Listening: TCP Socket Connected\r\n");
00120 break;
00121 case TCPSOCKET_WRITEABLE:
00122 printf("Listening: TCP Socket Writable\r\n");
00123 break;
00124 case TCPSOCKET_READABLE:
00125 printf("Listening: TCP Socket Readable\r\n");
00126 break;
00127 case TCPSOCKET_CONTIMEOUT:
00128 printf("Listening: TCP Socket Timeout\r\n");
00129 break;
00130 case TCPSOCKET_CONRST:
00131 printf("Listening: TCP Socket CONRST\r\n");
00132 break;
00133 case TCPSOCKET_CONABRT:
00134 printf("Listening: TCP Socket CONABRT\r\n");
00135 break;
00136 case TCPSOCKET_ERROR:
00137 printf("Listening: TCP Socket Error\r\n");
00138 break;
00139 case TCPSOCKET_DISCONNECTED:
00140
00141 printf("Listening: TCP Socket Disconnected\r\n");
00142 ListeningSock.close();
00143 break;
00144 default:
00145 printf("DEFAULT\r\n");
00146 };
00147 }
00148
00149
00150 int main() {
00151 printf("\r\n");
00152 printf("Setting up...\r\n");
00153 EthernetErr ethErr = eth.setup();
00154 if(ethErr)
00155 {
00156 printf("Error %d in setup.\r\n", ethErr);
00157 return -1;
00158 }
00159 printf("Setup OK\r\n");
00160
00161 IpAddr ip = eth.getIp();
00162 printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
00163
00164
00165
00166 ListeningSock.setOnEvent(&onListeningTCPSocketEvent);
00167
00168
00169 err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT));
00170 printf("Binding..\r\n");
00171 if(err)
00172 {
00173
00174 printf("Binding Error\n");
00175 }
00176 err=ListeningSock.listen();
00177 printf("Listening...\r\n");
00178 if(err)
00179 {
00180 printf("Listening Error\r\n");
00181 }
00182
00183 Timer tmr;
00184 tmr.start();
00185
00186 while(true)
00187 {
00188 Net::poll();
00189 if(tmr.read() > 0.2)
00190 {
00191 led4=!led4;
00192 tmr.reset();
00193 }
00194 }
00195 }
Hello, I´m working with the "TCPSocket.h" I use the function "setOnEvent" and I wait for the event : "TCPSOCKET_READABLE". When this event happens, I try to receive data using "recv" but I don´t receive anything. I don´t understand how the event "TCPSOCKET_READABLE" works. Is supposed that if the events occurs, there are some data in a buffer. But What kind of buffer? I mean, when "the other side" of the communication sends something to my socket, is stored in that buffer and then the event occurs??
Thank you and regards,
María.