# Ethernet sockets not closing on client.stop()

**URL:** <https://forum.arduino.cc/t/ethernet-sockets-not-closing-on-client-stop/508775>\
**Category:** Networking, Protocols, and Devices\
**Created:** [February 16, 2018, 10:22pm UTC](https://forum.arduino.cc/t/ethernet-sockets-not-closing-on-client-stop/508775 "2018-02-16T22:22:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![docdockstader](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@docdockstader](https://forum.arduino.cc/u/docdockstader)\
**Post date:** [February 16, 2018, 10:22pm UTC](https://forum.arduino.cc/t/ethernet-sockets-not-closing-on-client-stop/508775/1 "2018-02-16T22:22:17Z")

</div>

Hey all, I'm using the example WebServer code from the Ethernet library with a genuine Ethernet shield, and the web-server eventually stops responding. Sometimes it's a couple days, sometimes it's a couple of weeks. I ran into a post which had some code to display the state of the sockets, here (post #3):

[http://forum.arduino.cc/index.php?topic=216772.0](http://forum.arduino.cc/index.php?topic=216772.0)

So I added that, and am calling it in each loop. I've let it run for a couple of days now, and have one socket reporting 0x17. The example code calls client.stop(), so why is the socket still connected? The post referenced above also indicates this is a known problem, but doesn't hint at a solution. Is there one?

The shield is directly connected to a switch, and is not available to the internet. Thanks!

```cpp
/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi

 */

#include <SPI.h>
#include <Ethernet.h>

#include <utility/w5100.h>
byte socketStat[MAX_SOCK_NUM];

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 1, 1, 198);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void showSockStatus()
{
 for (int i = 0; i < MAX_SOCK_NUM; i++) {
  Serial.print(F("Socket#"));
  Serial.print(i);
  uint8_t s = W5100.readSnSR(i);
  socketStat[i] = s;
  Serial.print(F(":0x"));
  Serial.print(s,16);
  Serial.print(F(" "));
  Serial.print(W5100.readSnPORT(i));
  Serial.print(F(" D:"));
  uint8_t dip[4];
  W5100.readSnDIPR(i, dip);
  for (int j=0; j<4; j++) {
    Serial.print(dip[j],10);
    if (j<3) Serial.print(".");
  }
  Serial.print(F("("));
  Serial.print(W5100.readSnDPORT(i));
  Serial.println(F(")"));
 }

 Serial.println();
}

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }

  // check sockets
  showSockStatus();

  // small delay to not flood serial monitor
  delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [February 16, 2018, 11:13pm UTC](https://forum.arduino.cc/t/ethernet-sockets-not-closing-on-client-stop/508775/2 "2018-02-16T23:13:41Z")

</div>

Sometimes it goes wrong and the socket stays open.  
I did not have that problem myself, but to be sure I have added the checkSocketStatus() from here: [Arduino Playground - WebServerST](https://playground.arduino.cc/Code/WebServerST). That closes a unused socket that is still open.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 11:09am UTC](https://forum.arduino.cc/t/ethernet-sockets-not-closing-on-client-stop/508775/3 "2021-05-06T11:09:36Z")

</div>

Closed on May 6, 2021, 11:09am UTC


