WiFi shield: client.close() takes a whole second. Too much for me.

Like the titel already explains, I have the WiFi shield and use it as a webserver for my tank. I use the Windows COM to make a GET request. I close the connection after it received the command, but that one step takes a whole second. How can I fasten this up?

Or better: How can I avoid to close the connection?

What the hell is happening here? Did someone waive the rules today about posting code? I didn't get that memo.

I didn't think it's necessary to post code, but here you go:

#include <SPI.h>
#include <WiFi.h>


char ssid[] = "NerdtechCorporation";     //  your network SSID (name)
char pass[] = "the password";    // your network password

char ssid2[]="Arduino Tank"; // My hotspot on my laptop to be able to control the tank everywhere
char pass2[]="wowmuchawesome"; // The password for that

WiFiServer server(3178);

int pin1=3; //LED for the transistor that controls the forward speed of the left motor
int pin2=5; //LED for the transistor that controls the backward speed of the left motor
int pin3=6; //Same like pin1 but for right
int pin4=9; //Same like pin2 but for right

int netz=0; // which network, 0-none, 1-homenetwork, 2-Arduino Tank (hotspot)
boolean go=false; // So it only starts to add chars when I already got a "G" because of "GET"
long time=0; // for timeout
int run=0; // I don't want to check the connection all the time

String relativex=""; //Left speed
String relativey=""; //Right speed (variable names stayed although I changed the way I control it, ignore)
String currentLine=""; //The current received line

int status = WL_IDLE_STATUS;     // the Wifi radio's status

char floatbuf[64]; //For converting chars in flaot
float speedlinks=0.0; //speed left
float speedrechts=0.0; //speed right



void setup() {
  Serial.begin(9600);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT); // I just see that I forgot to add the other two pins, how the hell could the work?!
  tryconnect(); // first connection
}

void loop() {
  if (run>60000) { // only test all 60 seconds
    run=0; //sets the countdown back to 0
    Serial.println("Run");
  if (WiFi.status()!=WL_CONNECTED) { // and only if it's not connected
  tryconnect(); //tries to connect
  }
  }
  run+=1; //countup
  delay(1);
  webserver();  //checks for connection and tries to receive data
  


    
}






void tryconnect() {  // works like a charm
  Serial.println("Trying to connect.");
  if (int num=WiFi.scanNetworks()>(-1)) {
    for (int thisNet = 0; thisNet<num; thisNet++) {
    
    if (WiFi.SSID(thisNet)=="NerdtechCorporation") {
      netz=1;
    } else if (WiFi.SSID(thisNet)=="Arduino Tank") {
      netz=2;
    } else {
      netz=0;
    }
    
  }
  
  
  
  
  if (WiFi.status()!=WL_CONNECTED) {
      Serial.println("Attempting to connect to Arduino Tank...");
      if (netz==2) {
      status = WiFi.begin(ssid2, pass2);
      }

      if ( status != WL_CONNECTED) {
         Serial.println("Couldn't connect to Arduino Tank.");
         Serial.println("Attempting to connect to NerdtechCorporation...");
         
         if (netz=1) {
         status = WiFi.begin(ssid, pass);
         }
         
         if ( status != WL_CONNECTED) {
             Serial.println("Couldn't connect to NerdtechCorporation.");
          } else {

            Serial.println("Connected to NerdtechCorporation.");
            server.begin();
            printWifiStatus();
          }
         
      } else {

        Serial.println("Connected to Arduino Tank.");
        server.begin();
        printWifiStatus();
      }
  } else {
    Serial.println("Already connected");
  }
}
}



void webserver() {

  WiFiClient client = server.available();

  if (client) {
    time=millis();  //stores the beginning time
    Serial.println(time);
    Serial.println(millis());
    Serial.println("new client");
    currentLine = "";
    while (client.connected()) {
      if (millis()-time>5000) {  // if it took 5 seconds timeout so stop everything and go back to loop()
        Serial.println("Timeout.");
        client.flush();
        client.stop();
        return;
      }
      if (client.available()) {
        char c = client.read();  // reads incoming stuff
        if (c == '\n') {

          if (currentLine.length() == 0) {
            client.println();
            client.println();
            break;
          }
          else {
            currentLine = "";
            Serial.println("Blanked current line");
          }
        }     
        else if (c != '\r') {
          currentLine += c;
        }
        
            speedthing();
            
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            client.println("Banana");
            Serial.println("Sent response");
            client.flush();
            Serial.println(millis());
            client.stop();
            Serial.println(millis());
            delay(10);
            Serial.println("Delay over");
            return;
            
            
      }
    }
    Serial.println("Going to stop client manually");
    client.stop();
    Serial.println("client disconnected");
  }
}


void speedthing() {
            if (currentLine.substring(0,5)=="GET /"&&currentLine.indexOf("HTTP")!=-1) {  // if the beginning is "GET /" and it contains "HTTP", it also contains the speed of the motors
            currentLine=currentLine.substring(5,currentLine.length()-1);
            Serial.println(currentLine);
            currentLine=currentLine.substring(0,currentLine.indexOf(" "));
            Serial.println(currentLine);
            relativex=(currentLine.substring(0,currentLine.indexOf("x")));
            relativey=(currentLine.substring(currentLine.indexOf("x")+1,currentLine.length()));
            Serial.println(relativex);
            Serial.println(relativey);
            
            relativex.toCharArray(floatbuf,sizeof(floatbuf));
            speedlinks=atof(floatbuf)*255;
            relativey.toCharArray(floatbuf,sizeof(floatbuf));
            speedrechts=atof(floatbuf)*255;
            
            Serial.println(speedlinks);
            Serial.println(speedrechts);
            
            if (speedrechts<0) {  // if it's backwards, stop the forward transistor (or so) and start the backwards one
              analogWrite(pin3,0);
              analogWrite(pin4,speedrechts*(-1));
            } else {  //else stop the backwards one and start the forwards one
              analogWrite(pin4,0);
              analogWrite(pin3,speedrechts);
            }
            
            if (speedlinks<0) {  //same for left
              analogWrite(pin1,0);
              analogWrite(pin2,speedlinks*(-1));
            } else {
              analogWrite(pin2,0);
              analogWrite(pin1,speedlinks);
            }
            Serial.println("Finished speed thing");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

Can someone help me with my project now?

char floatbuf[64]; //For converting chars in flaot

A float has 6 or 7 digits of accuracy. Sending 64 digits is pushing the limits of credulity.

  delay(1);

Why?

Speed is not of the essence, I guess.

It's nearly impossible to read your code. Put EVERY { on a new line, use Tools + Auto Format to properly indent it. Then, post it again.

Here:

#include <SPI.h>
#include <WiFi.h>


char ssid[] = "NerdtechCorporation";     //  your network SSID (name)
char pass[] = "the password";    // your network password

char ssid2[]="Arduino Tank"; // My hotspot on my laptop to be able to control the tank everywhere
char pass2[]="wowmuchawesome"; // The password for that

WiFiServer server(3178);

int pin1=3; //LED for the transistor that controls the forward speed of the left motor
int pin2=5; //LED for the transistor that controls the backward speed of the left motor
int pin3=6; //Same like pin1 but for right
int pin4=9; //Same like pin2 but for right

int netz=0; // which network, 0-none, 1-homenetwork, 2-Arduino Tank (hotspot)
boolean go=false; // So it only starts to add chars when I already got a "G" because of "GET"
long time=0; // for timeout
int run=0; // I don't want to check the connection all the time

String relativex=""; //Left speed
String relativey=""; //Right speed (variable names stayed although I changed the way I control it, ignore)
String currentLine=""; //The current received line

int status = WL_IDLE_STATUS;     // the Wifi radio's status

char floatbuf[8]; //For converting chars in flaot
float speedlinks=0.0; //speed left
float speedrechts=0.0; //speed right



void setup() {
  Serial.begin(9600);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT); // I just see that I forgot to add the other two pins, how the hell could the work?!
  tryconnect(); // first connection
}

void loop() {
  if (run>60000) { // only test all 60 seconds
    run=0; //sets the countdown back to 0
    Serial.println("Run");
    if (WiFi.status()!=WL_CONNECTED) { // and only if it's not connected
      tryconnect(); //tries to connect
    }
  }
  run+=1; //countup
  delay(1);
  webserver();  //checks for connection and tries to receive data




}






void tryconnect() {  // works like a charm
  Serial.println("Trying to connect.");
  if (int num=WiFi.scanNetworks()>(-1)) {
    for (int thisNet = 0; thisNet<num; thisNet++) {

      if (WiFi.SSID(thisNet)=="NerdtechCorporation") {
        netz=1;
      } 
      else if (WiFi.SSID(thisNet)=="Arduino Tank") {
        netz=2;
      } 
      else {
        netz=0;
      }

    }




    if (WiFi.status()!=WL_CONNECTED) {
      Serial.println("Attempting to connect to Arduino Tank...");
      if (netz==2) {
        status = WiFi.begin(ssid2, pass2);
      }

      if ( status != WL_CONNECTED) {
        Serial.println("Couldn't connect to Arduino Tank.");
        Serial.println("Attempting to connect to NerdtechCorporation...");

        if (netz=1) {
          status = WiFi.begin(ssid, pass);
        }

        if ( status != WL_CONNECTED) {
          Serial.println("Couldn't connect to NerdtechCorporation.");
        } 
        else {

          Serial.println("Connected to NerdtechCorporation.");
          server.begin();
          printWifiStatus();
        }

      } 
      else {

        Serial.println("Connected to Arduino Tank.");
        server.begin();
        printWifiStatus();
      }
    } 
    else {
      Serial.println("Already connected");
    }
  }
}



void webserver() {

  WiFiClient client = server.available();

  if (client) {
    time=millis();  //stores the beginning time
    Serial.println(time);
    Serial.println(millis());
    Serial.println("new client");
    currentLine = "";
    while (client.connected()) {
      if (millis()-time>5000) {  // if it took 5 seconds timeout so stop everything and go back to loop()
        Serial.println("Timeout.");
        client.flush();
        client.stop();
        return;
      }
      if (client.available()) {
        char c = client.read();  // reads incoming stuff
        if (c == '\n') {

          if (currentLine.length() == 0) {
            client.println();
            client.println();
            break;
          }
          else {
            currentLine = "";
            Serial.println("Blanked current line");
          }
        }     
        else if (c != '\r') {
          currentLine += c;
        }

        speedthing();

        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");
        client.println();
        client.println("Banana");
        Serial.println("Sent response");
        client.flush();
        Serial.println(millis());
        client.stop();
        Serial.println(millis());
        delay(10);
        Serial.println("Delay over");
        return;


      }
    }
    Serial.println("Going to stop client manually");
    client.stop();
    Serial.println("client disconnected");
  }
}


void speedthing() {
  if (currentLine.substring(0,5)=="GET /"&&currentLine.indexOf("HTTP")!=-1) {  // if the beginning is "GET /" and it contains "HTTP", it also contains the speed of the motors
    currentLine=currentLine.substring(5,currentLine.length()-1);
    Serial.println(currentLine);
    currentLine=currentLine.substring(0,currentLine.indexOf(" "));
    Serial.println(currentLine);
    relativex=(currentLine.substring(0,currentLine.indexOf("x")));
    relativey=(currentLine.substring(currentLine.indexOf("x")+1,currentLine.length()));
    Serial.println(relativex);
    Serial.println(relativey);

    relativex.toCharArray(floatbuf,sizeof(floatbuf));
    speedlinks=atof(floatbuf)*255;
    relativey.toCharArray(floatbuf,sizeof(floatbuf));
    speedrechts=atof(floatbuf)*255;

    Serial.println(speedlinks);
    Serial.println(speedrechts);

    if (speedrechts<0) {  // if it's backwards, stop the forward transistor (or so) and start the backwards one
      analogWrite(pin3,0);
      analogWrite(pin4,speedrechts*(-1));
    } 
    else {  //else stop the backwards one and start the forwards one
      analogWrite(pin4,0);
      analogWrite(pin3,speedrechts);
    }

    if (speedlinks<0) {  //same for left
      analogWrite(pin1,0);
      analogWrite(pin2,speedlinks*(-1));
    } 
    else {
      analogWrite(pin2,0);
      analogWrite(pin1,speedlinks);
    }
    Serial.println("Finished speed thing");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

I use the delay so it runs it only 1ms, so my 60000 are 60 seconds. I just realized how dumb that is, the commands need time too...

void setup() {

I guess you and I have a different concept of what EVERY means.

Well, I usually put them on the same line. Does it really make a difference?