![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SimpleSocket 1.0 examples
Dependencies: EthernetNetIf SimpleSocket 1.0 mbed
main.cpp
- Committer:
- yamaguch
- Date:
- 2011-08-16
- Revision:
- 1:7350a2598a80
- Parent:
- 0:c41b68a4a296
- Child:
- 2:304672a01127
File content as of revision 1:7350a2598a80:
#include "mbed.h" #include "EthernetNetIf.h" #include "TCPSocket.h" #include "SocketUtil.h" EthernetNetIf eth; DigitalOut led1(LED1); TCPSocket tcp; //The listening port where requests are queued TCPSocket* link; //The port where accepted requests can communicate Host local(IpAddr(), 1234); // mbed Host client; void onLinkSocketEvent(TCPSocketEvent e) { SocketEvent event = e; printf(event); switch (event) { case TCPSOCKET_READABLE: char buf[128]; while (int len = link->recv(buf, 128)) { // And send straight back out again link->send(buf, len); buf[len] = 0; // make terminater printf("Received & Wrote: %s\n",buf); } break; case TCPSOCKET_DISCONNECTED: link->close(); break; } } void onTCPSocketEvent(TCPSocketEvent e) { SocketEvent event = e; printf(event); switch (event) { case TCPSOCKET_ACCEPT: if (SocketError err = tcp.accept(&client, &link)) { printf(err); } else { link->setOnEvent(&onLinkSocketEvent); IpAddr ip = client.getIp(); printf("Incoming TCP connection from %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); } break; case TCPSOCKET_DISCONNECTED: tcp.close(); break; } } int main() { //Basic setup printf("Welcome to wireFUSE\n"); printf("Setting up...\n"); EthernetErr ethErr = eth.setup(); if (ethErr) error("Error %d in setup.\n", ethErr); printf("Setup OK\n"); tcp.setOnEvent(&onTCPSocketEvent); //Bind to local port SocketError bindErr = tcp.bind(local); printf("Init bind... %s", (char *) bindErr); //Listen to local port TCPSocketErr listenErr = tcp.listen(); printf("Init listen... %s", (char *)listenErr); Timer timer; timer.start(); while (true) { Net::poll(); if (timer.read() > 10) { timer.reset(); led1 = !led1; //Show that we are alive printf("waiting for client on port 1234\n"); } } }