This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.

Dependents:   EZS

Fork of WIZnet_Library by WIZnet

Revision:
7:7c67a8922ec5
Parent:
6:ca8405b9564d
--- a/WIZnetInterface/Socket/TCPSocketConnection.cpp	Fri Apr 24 08:19:14 2015 +0000
+++ b/WIZnetInterface/Socket/TCPSocketConnection.cpp	Sun May 31 10:06:41 2015 +0000
@@ -15,10 +15,16 @@
  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
+#include "TCPSocketConnection.h"
+#include <cstring>
 
-#include "TCPSocketConnection.h"
+using std::memset;
+using std::memcpy;
 
-TCPSocketConnection::TCPSocketConnection()
+// not a big code.
+// refer from EthernetInterface by mbed official driver
+TCPSocketConnection::TCPSocketConnection() :
+    _is_connected(false)
 {
 }
 
@@ -36,12 +42,17 @@
     if (!eth->connect(_sock_fd, get_address(), port)) {
         return -1;
     }
+    set_blocking(false);
+    // add code refer from EthernetInterface.
+    _is_connected = true;
     return 0;
 }
 
 bool TCPSocketConnection::is_connected(void)
 {
-    return eth->is_connected(_sock_fd);
+    // force update recent state.
+    _is_connected = eth->is_connected(_sock_fd);
+    return _is_connected;
 }
 
 
@@ -52,6 +63,10 @@
 
 int TCPSocketConnection::send(char* data, int length)
 {
+    // add to cover exception.
+    if ((_sock_fd < 0) || !_is_connected)
+        return -1;
+
     int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
     if (size < 0) {
         return -1;
@@ -65,6 +80,7 @@
 // -1 if unsuccessful, else number of bytes written
 int TCPSocketConnection::send_all(char* data, int length)
 {
+
     int writtenLen = 0;
     while (writtenLen < length) {
         int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
@@ -86,6 +102,10 @@
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive(char* data, int length)
 {
+    // add to cover exception.
+    if ((_sock_fd < 0) || !_is_connected)
+        return -1;
+
     int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
     if (size < 0) {
         return -1;