ap
Dependencies: DnsQuery
Dependents: WizFi310_TCP_Echo_Server_Example
Fork of NetworkSocketAPI by
Diff: Socket.cpp
- Branch:
- api-changes
- Revision:
- 34:c17745683385
- Parent:
- 32:2c5fc105fc50
- Child:
- 35:838393fbc2ca
--- a/Socket.cpp Tue Feb 23 17:14:20 2016 +0000 +++ b/Socket.cpp Mon Feb 22 19:56:44 2016 -0600 @@ -31,7 +31,7 @@ if (_socket) close(); } -int32_t Socket::setURL(const char *url, uint16_t port) +int32_t Socket::setURL(const char *url) { int32_t err = _iface->getHostByName(url, _ip_address); if (err) return err; @@ -40,24 +40,16 @@ _socket->setIPAddress(_ip_address); } - if (port) { - setPort(port); - } - return 0; } -void Socket::setIPAddress(const char *ip, uint16_t port) +void Socket::setIPAddress(const char *ip) { strcpy(_ip_address, ip); if (_socket) { _socket->setIPAddress(_ip_address); } - - if (port) { - setPort(port); - } } void Socket::setPort(uint16_t port) @@ -79,9 +71,22 @@ return _port; } -int32_t Socket::open() +int32_t Socket::open(const char *url, uint16_t port) { - if (_socket) close(); + if (_socket) { + int32_t err = close(); + if (err) return err; + } + + if (url) { + int32_t err = setURL(url); + if (err) return err; + } + + if (port) { + setPort(port); + } + _socket = _iface->createSocket(_proto); if (!_socket) return -2; @@ -94,8 +99,8 @@ _socket->setPort(_port); } + int32_t err = _socket->open(); - if (err) { _iface->destroySocket(_socket); } @@ -119,14 +124,12 @@ int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms) { if (!_socket) return -2; - return _socket->send(data, len, timeout_ms); } int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms) { if (!_socket) return -2; - return _socket->recv(data, len, timeout_ms); }