# Ethernet Shield Doesn't Close Socket After Client.Connect() Failure

**URL:** <https://forum.arduino.cc/t/ethernet-shield-doesnt-close-socket-after-client-connect-failure/276975>\
**Category:** Programming\
**Created:** [December 19, 2014, 4:57am UTC](https://forum.arduino.cc/t/ethernet-shield-doesnt-close-socket-after-client-connect-failure/276975 "2014-12-19T04:57:47Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [December 20, 2014, 1:33am UTC](https://forum.arduino.cc/t/ethernet-shield-doesnt-close-socket-after-client-connect-failure/276975/7 "2014-12-20T01:33:56Z")

</div>

The server closes the connection. If your server is not closing the connection, you need to timeout the connection on the client end. Here is what I use on the client to timeout a broken connection or stalled server.

```plaintext
  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

```

The full code I use is here.  
[http://playground.arduino.cc/Code/WebClient](http://playground.arduino.cc/Code/WebClient)

edit: You didn't answer my main question. What kind of device is 192.168.0.102? LAMP server? Windows? Arduino?

---

_[View the full topic](https://forum.arduino.cc/t/ethernet-shield-doesnt-close-socket-after-client-connect-failure/276975)._
