Socket.Getinetaddress() Provide Null On Android 5

Posted on

Refer to in conclusion illustration of “Simple HTTP server running on Android“, it function on Xiaomi Redmi 2 running Android 4.4.4. But somebody study computer program stopped on Android v devices. So I re-test on Nexus seven running Android 5.1.1. Yes, the app crash! caused by NullPointerException, because socket.getInetAddress() provide null.

Modify MainActivity.java to capture socket.getInetAddress() earlier together with later close():

package com.blogspot.android_er.androidhttpserver;  import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.EditText; import android.widget.TextView;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Enumeration;  world course of teaching MainActivity extends AppCompatActivity {      EditText welcomeMsg;     TextView infoIp;     TextView infoMsg;     String msgLog = "";      ServerSocket httpServerSocket;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          welcomeMsg = (EditText) findViewById(R.id.welcomemsg);         infoIp = (TextView) findViewById(R.id.infoip);         infoMsg = (TextView) findViewById(R.id.msg);          infoIp.setText(getIpAddress() + ":"                 + HttpServerThread.HttpServerPORT + "\n");          HttpServerThread httpServerThread = novel HttpServerThread();         httpServerThread.start();     }      @Override     protected void onDestroy() {         super.onDestroy();          if (httpServerSocket != null) {             seek {                 httpServerSocket.close();             } grab (IOException e) {                 e.printStackTrace();             }         }     }      someone String getIpAddress() {         String ip = "";         seek {             Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface                     .getNetworkInterfaces();             piece (enumNetworkInterfaces.hasMoreElements()) {                 NetworkInterface networkInterface = enumNetworkInterfaces                         .nextElement();                 Enumeration<InetAddress> enumInetAddress = networkInterface                         .getInetAddresses();                 piece (enumInetAddress.hasMoreElements()) {                     InetAddress inetAddress = enumInetAddress.nextElement();                      if (inetAddress.isSiteLocalAddress()) {                         ip += "SiteLocalAddress: "                                 + inetAddress.getHostAddress() + "\n";                     }                  }              }          } grab (SocketException e) {             // TODO Auto-generated grab block             e.printStackTrace();             ip += "Something Wrong! " + e.toString() + "\n";         }          provide ip;     }      someone course of teaching HttpServerThread extends Thread {          static in conclusion int HttpServerPORT = 8888;          @Override         world void run() {             Socket socket = null;              seek {                 httpServerSocket = novel ServerSocket(HttpServerPORT);                  while(true){                     socket = httpServerSocket.accept();                      HttpResponseThread httpResponseThread =                             novel HttpResponseThread(                                     socket,                                     welcomeMsg.getText().toString());                     httpResponseThread.start();                 }             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }         }      }      someone course of teaching HttpResponseThread extends Thread {          Socket socket;         String h1;          HttpResponseThread(Socket socket, String msg){             this.socket = socket;             h1 = msg;         }          @Override         world void run() {             BufferedReader is;             PrintWriter os;             String request;              seek {                 is = novel BufferedReader(new InputStreamReader(socket.getInputStream()));                 asking = is.readLine();                  bone = novel PrintWriter(socket.getOutputStream(), true);                  String reply =                         "<html><head></head>" +                                 "<body>" +                                 "<h1>" + h1 + "</h1>" +                                 "</body></html>";                  os.print("HTTP/1.0 200" + "\r\n");                 os.print("Content type: text/html" + "\r\n");                 os.print("Content length: " + response.length() + "\r\n");                 os.print("\r\n");                 os.print(response + "\r\n");                 os.flush();                 InetAddress clientInetAddressBeforeClose = socket.getInetAddress();                 socket.close();                 InetAddress clientInetAddressAfterClose = socket.getInetAddress();                  msgLog += "Request: " + asking + "\n";                  if(clientInetAddressBeforeClose == null){                     msgLog += "clientInetAddressBeforeClose == null\n";                 }else{                     msgLog += "clientInetAddressBeforeClose = " + clientInetAddressBeforeClose.toString() + "\n";                 }                  if(clientInetAddressAfterClose == null){                     msgLog += "clientInetAddressAfterClose == null\n";                 }else{                     msgLog += "clientInetAddressAfterClose = " + clientInetAddressAfterClose.toString() + "\n";                 }                  MainActivity.this.runOnUiThread(new Runnable() {                      @Override                     world void run() {                          infoMsg.setText(msgLog);                     }                 });              } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }              return;         }     } }  

It’s works life that on Xiaomi Redmi 2 running Android 4.4.4, socket.getInetAddress() dice on no modify earlier together with later close().

But on Nexus seven running Android 5.1.1, modify to naught later close().

So, nosotros possess got to read socket.getInetAddress() earlier close().