LWIP STM32CubeF7 to communicate with TCP ethernet to Java URL connection class application

372 Views Asked by At

I am trying to create a Java application that reads data of a server using TCP. The server is anSTM32F4DISCOVERY SOC board from STM32. I am using the LwIP_HTTP_Server_Netconn_RTOS example application as an example.

Here is the important part of the code of the LWIP, that handles the GET requests. The server works well on the browser.

/* Put the connection into LISTEN state */
netconn_listen(conn);

while(1) 
{
  /* accept any icoming connection */
  accept_err = netconn_accept(conn, &newconn);
  if(accept_err == ERR_OK)
  {
    /* serve connection */
    http_server_serve(newconn);
    /* delete connection */
    netconn_delete(newconn);
  }
}


static void http_server_serve(struct netconn *conn) 
{
  .
  .
  
  
  if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0))
  {
                if (strncmp((char const *)buf,"GET /STM32F7xx",27)==0)
            {
              fs_open(&file, "/STM32F7xx.html");
              netconn_write(conn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
              fs_close(&file);
            }
      ... other if stats to handle other /routes
      
      else if (strncmp(buf, "GET /test", 9) == 0)
            {
              DynWebPage2(conn);
            }
   }
   
void DynWebPage2(struct netconn *conn)
{
  portCHAR PAGE_BODY[512];
  portCHAR PAGE_BEGIN[512];
  portCHAR pagehits[10] = {0};

  memset(PAGE_BODY, 0,512);
  memset(PAGE_BEGIN, 0,512);

  /* Update the hit count */
  nPageHits++;
  sprintf(pagehits, "%d", (int)nPageHits);
  strcat(PAGE_BODY, "helloworld");
  strcat((char *)PAGE_BEGIN, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  strcat((char *)PAGE_BEGIN, "<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns=\"http://www.w3.org/TR/REC-html40\">");

  /* Send the dynamically generated page */
  netconn_write(conn, PAGE_BEGIN, strlen((char*)PAGE_BEGIN), NETCONN_COPY);
  netconn_write(conn, PAGE_BODY, strlen(PAGE_BODY), NETCONN_COPY);
}

Here is the part of the Java application I am using to make GET requests.

    try {
          url = new URL("http://192.168.0.26/test");
          con = (HttpURLConnection) url.openConnection();
          con.setConnectTimeout(30000);
          con.setRequestMethod("GET");
          con.setDoOutput(true);
          int status = con.getResponseCode();
          InputStreamReader streamReader;   
          System.out.println(status);
          if (status > 299) {
                streamReader = new InputStreamReader(con.getErrorStream());
          } else {
                BufferedReader in;
                streamReader = new InputStreamReader(con.getInputStream());
                in = new BufferedReader(streamReader);
                String inputLine;
                StringBuffer content = new StringBuffer();
                content.append("test");
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                System.out.println(content);
                in.close();
          }
          con.disconnect();
        } catch (Exception ex) {
          ex.printStackTrace();                                                                                                                                                                                                                                                                                                                                                                                                                        
        }
    }

The board IP is 192.168.0.26. When I set the code to make a GET request to "http://192.168.0.26/STM32F7xx". It does work just like browsers and the HTML file is printed on the java application console. if I switch it to "/test". The browser shows the value of the variable pageHits, and the stm32 server increments the variable every time a request is made. However, when I do "/test" on the Java application. I get this error.

java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:718)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:715)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
at dataconverter.Reader.getConnection(Reader.java.260)

I used Wireshark to analyze the packet for the /test and /STM32F7xx between the browser and Java requesT for each route.

/STM32F7xx from browser

Hypertext Transfer Protocol
GET /STM32F7xx.html HTTP/1.1\r\n
Host: 192.168.0.26\r\n
Connection: keep-alive\r\n
Cache-Control: max-age=0\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: en-US,en;q=0.9\r\n
\r\n
[Full request URI: http://192.168.0.26/STM32F7xx.html]
[HTTP request 1/1]
[Response in frame: 124]

/STM32F7xx from Java Application

Hypertext Transfer Protocol
GET /STM32F7xx.html HTTP/1.1\r\n
User-Agent: Java/1.8.0_271\r\n
Host: 192.168.0.26\r\n
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n
Connection: keep-alive\r\n
\r\n
[Full request URI: http://192.168.0.26/STM32F7xx.html]
[HTTP request 1/1]
[Response in frame: 37915]

/test from the browser

Hypertext Transfer Protocol
GET /test HTTP/1.1\r\n
Host: 192.168.0.26\r\n
Connection: keep-alive\r\n
Cache-Control: max-age=0\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: en-US,en;q=0.9\r\n
\r\n
[Full request URI: http://192.168.0.26/test]
[HTTP request 1/1]

/test from Java Application

Hypertext Transfer Protocol
GET /test HTTP/1.1\r\n
User-Agent: Java/1.8.0_271\r\n
Host: 192.168.0.26\r\n
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n
Connection: keep-alive\r\n
\r\n
[Full request URI: http://192.168.0.26/test]
[HTTP request 1/1]

0

There are 0 best solutions below