Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LPC11U68_NTPClient_HelloWorld_WIZ550io
Fork of WIZ550ioInterface by
Diff: Socket/TCPSocketServer.cpp
- Revision:
- 5:fb15c35d1e28
- Parent:
- 4:0bcec6272784
--- a/Socket/TCPSocketServer.cpp Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/TCPSocketServer.cpp Tue Aug 27 12:50:11 2013 +0000
@@ -17,74 +17,58 @@
*/
#include "TCPSocketServer.h"
-#include <string>
TCPSocketServer::TCPSocketServer() {}
// Server initialization
int TCPSocketServer::bind(int port) {
- char cmd[20];
-
+ if (_sock_fd < 0) {
+ _sock_fd = eth->new_socket();
+ if (_sock_fd < 0) {
+ return -1;
+ }
+ }
// set TCP protocol
- wifi->setProtocol(TCP);
-
+ eth->setProtocol(_sock_fd, TCP);
// set local port
- sprintf(cmd, "set i l %d\r", port);
- if (!wifi->sendCommand(cmd, "AOK"))
- return -1;
-
- // save
- if (!wifi->sendCommand("save\r", "Stor"))
- return -1;
-
- // reboot
- wifi->reboot();
-
+ eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
// connect the network
- if (wifi->isDHCP()) {
- if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000))
- return -1;
- } else {
- if (!wifi->sendCommand("join\r", "Associated", NULL, 10000))
- return -1;
- }
-
- // exit
- wifi->exit();
-
- wait(0.2);
- wifi->flush();
+ eth->scmd(_sock_fd, OPEN);
return 0;
}
int TCPSocketServer::listen(int backlog) {
- if (backlog != 1)
+ if (_sock_fd < 0) {
return -1;
+ }
+ if (backlog != 1) {
+ return -1;
+ }
+ eth->scmd(_sock_fd, LISTEN);
return 0;
}
int TCPSocketServer::accept(TCPSocketConnection& connection) {
- int nb_available = 0, pos = 0;
- char c;
- string str;
- bool o_find = false;
- while (1) {
- while(!wifi->readable());
- nb_available = wifi->readable();
- for (int i = 0; i < nb_available; i++) {
- c = wifi->getc();
- if (c == '*') {
- o_find = true;
- }
- if (o_find && c != '\r' && c != '\n') {
- str += c;
- pos = str.find("*OPEN*");
- if (pos != string::npos) {
- wifi->flush();
- return 0;
- }
- }
+ if (_sock_fd < 0) {
+ return -1;
+ }
+ Timer t;
+ t.reset();
+ t.start();
+ while(1) {
+ if (t.read_ms() > _timeout && _blocking == false) {
+ return -1;
+ }
+ if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
+ break;
}
}
-}
\ No newline at end of file
+ uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
+ char host[16];
+ snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
+ uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
+ connection._sock_fd = _sock_fd;
+ connection.set_address(host, port);
+ return 0;
+}
