I've been playing with the socket API a bit as it seems to be just what I need, but I've run into a brick wall. Can anyone point me in the right direction? So far, I have:
#include "mbed.h"
#include "EthernetNetIf.h"
#include "TcpSocket.h"
#include "UdpSocket.h"
EthernetNetIf eth;
TcpSocket tcpSock;
UdpSocket udpSock;
int main() {
printf("Start\n");
printf("\r\nSetting up...\r\n");
EthernetErr ethErr = eth.setup();
if(ethErr) {
printf("Error %d in setup.\n", ethErr);
return -1;
}
IpAddr ip = eth.getIp();
printf("IP Address: %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); // Turns out the library does this anyway
TcpSocketErr sockErr = tcpSock.bind(Host(IP_ADDR_ANY, 7)); // Trying to make a simple echo server on port 7
if(sockErr) {
printf("Error %d in binding TCP socket\r\n", sockErr);
return -1;
}
sockErr = tcpSock.listen();
if(sockErr) {
printf("Error %d in listening to TCP socket\r\n", sockErr);
return -1;
}
printf("Entering while loop just Net::poll()ing\r\n");
while(1) {
Net::poll();
}
}
But now what, what do I do with the socket? I hooked up a method to be called on events using:
void onNetTcpSocketEvent(TcpSocketEvent e) {
switch (e) {
case TCPSOCKET_CONNECTED:
printf("TCPSOCKET_CONNECTED\r\n");
break;
case TCPSOCKET_ACCEPT:
printf("TCPSOCKET_ACCEPT\r\n");
break;
default:
printf("Socket event %d\r\n", e);
}
}
int main() {
// ...
void (*pt2Function)(TcpSocketEvent) = NULL;
pt2Function = &onNetTcpSocketEvent;
tcpSock.setOnEvent(pt2Function);
(*pt2Function) (TCPSOCKET_CONNECTED);
// ....
}
This calls the onNetTcpSocketEvent function on TCPSOCKET_ACCEPT, but I'm at a loss as to what I do next. I'm guessing something like:
Host *h;
TcpSocket **s;
TcpSocketErr tcpSockErr = tcpSock.accept(h, s);
if(tcpSockErr) {
printf("Error %d in accepting socket\r\n", tcpSockErr);
break;
}
IpAddr clientIp = h->getIp();
printf("Client: %d.%d.%d.%d", clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
But this seems to die a horrible death when I try to call h->getIp(); If I can get this sorted, what I'd like to do is get this working as a basic echo server on TCP and UDP and then document it as some sort of guide to using this excellent socket API. In the meantime I've got my head buried in a C++ book as coming from the sheltered life of Java and C#, pointers are quite scary and as is quite possibly obvious from my last code snippet, I need to do a fair bit of learning.
Hi Donatien,
>If you are ready to help, I would love having some feedback regarding bugs, but also ease of use and customization. So if you are ready to implement that stack in your project, I think it would be a great test!
Yes, I am ready. From what you say the architecture in your implementation fits perfectly the application I have. It seems like I will need the source code for getting started with sockets. Besides, to me source code is almost always better than documentation, so I can start before any documentation is available.
--
Ilya