0

A socket that I'm trying to make a http call is taking too long to respond (around 20 seconds). The code below works it makes an service discovery to an esp8266 module that responds instantly when I access it via browser. I've tried solutions mentioned in this thread but nothing worked, I think that the problem lies before in getInputStream() where the synchronous process holds.

class ESP8266 extends Thread
{
    NsdServiceInfo lService;
    boolean lToggle = false;

    public ESP8266(NsdServiceInfo pService)
    {
        lService = pService;
    }

    @Override
    public void run() {
        while (lServiceActive) {
            if (lToggle)
            {
                lToggle = false;

                try {
                    Socket s = new Socket(lService.getHost(), lService.getPort());
                    PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
                    pw.println("GET /toggle HTTP/1.1");
                    pw.println("Host: " + s.getInetAddress().getHostName());
                    pw.println("Connection: close");
                    pw.println("");
                    pw.flush();

                    InputStream stream = s.getInputStream();
                    byte[] buffer = new byte[1024];

                    while (stream.read(buffer) > 0)
                    {
                        Log.d(TAG, buffer.toString());
                    }
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void Toggle()
    {
        lToggle = true;
    }
}

Having the code without the getInputStream (shown below) also around 20 seconds to process.

@Override
public void run() {
    while (lServiceActive) {
        if (lToggle)
        {
            lToggle = false;

            try {
                Socket s = new Socket(lService.getHost(), lService.getPort());
                PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
                pw.println("GET /toggle HTTP/1.1");
                pw.println("Host: " + s.getInetAddress().getHostName());
                pw.println("Connection: close");
                pw.println("");
                pw.flush();
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Community
  • 1
  • 1
Rinaldi Segecin
  • 449
  • 8
  • 23
  • Just so that the issue is clear... you are saying this code runs and exits properly, but that the while loop is too slow? – bremen_matt Feb 05 '17 at 11:36
  • `to an esp8266 module that responds instantly`. What is the response exactly? A string? A line? How many bytes? Does the module code close the socket after the response is sent? – greenapps Feb 05 '17 at 11:41
  • This looks like a socket timeout. Isn't there a SocketTimeoutException? Or another one? Please explain better what happens. How often does the loop run? – greenapps Feb 05 '17 at 11:44
  • `problem lies before in getInputStream() where the synchronous process holds.`. Well on which line exactly? On `pw.println("Host: " + s.getInetAddress().getHostName());`? Is .getHostName() the culprit? – greenapps Feb 05 '17 at 11:45
  • Yes the code runs and exits properly and it's not on the loop that's slow the holds on the line `InputStream stream = s.getInputStream();`. Actually there're no content in the page (code 204) I just have to access the url with GET. – Rinaldi Segecin Feb 05 '17 at 11:47
  • If I remove the part of the code where I read the InputStream just up where I do `pw.flush()` the response still very slow. – Rinaldi Segecin Feb 05 '17 at 11:52
  • Well if the server does not send a response then why are you trying to read a response? Or trying to open an input stream? You probably should only determine the result code then. And what does code 204 mean? – greenapps Feb 05 '17 at 11:52
  • `just up where I do pw.flush() the response still very slow.`. But what do you consider 'the response' then? So then why don't you tell which line causes the problem? I indicated already a line. – greenapps Feb 05 '17 at 11:53
  • If I remove the part of the code where I read the InputStream just up where I do pw.flush() the response still very slow. There are no lines that causes a problem, it's just too slow and when I added the InputStream in there I could see that the s.getInputStream holding up the process. – Rinaldi Segecin Feb 05 '17 at 11:58
  • code 204 means page with no content like 200 means 'OK' and 404 means 'page not found'. – Rinaldi Segecin Feb 05 '17 at 12:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134894/discussion-between-rinaldi-segecin-and-greenapps). – Rinaldi Segecin Feb 05 '17 at 12:05
  • You still have not reacted on what i said about .getHostName(). – greenapps Feb 05 '17 at 13:00
  • The code without the InputStream the part while debugging it doesn't stops anywhere printing everything as expected including the host name but it still delays for 20 sec to the service to react. I know that's not the service's fault because if I access it from the browser entering the service name it responds straight away. I think that the PrinterWriter is not actually flushing. – Rinaldi Segecin Feb 05 '17 at 22:48

0 Answers0