简体   繁体   中英

Response is not getting in (android socket programming/ tcp/ip socket programming )

I am developing one Android Application in which I have to send location data on server and getting job data as a response in this way all communication is build but i am stuck of my mind on one thing when i trying to read the response from server the no any output and no any response is coming so please help me out of this stuck. Below is my code. Thanks in advance.

  public static class MyClientTask extends AsyncTask<Void, Void, Void> { String dstAddress; int dstPort; String response = ""; String s; String red; String loc; String msg; Socket socket = null; DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; InputStream is=null; BufferedReader br=null; public MyClientTask(String addr, int port,String msg){ dstAddress = addr; dstPort = port; loc=msg; } @Override protected Void doInBackground(Void... arg0) { try { socket = new Socket(dstAddress, dstPort); socket.setKeepAlive(true); dataOutputStream = new DataOutputStream(socket.getOutputStream()); is=socket.getInputStream(); socket.setSoTimeout(60*1000); dataInputStream = new DataInputStream(is); Log.i("socket connect","socket OK"); dataOutputStream.writeUTF(loc); while (dataInputStream == null) { ////this part is not working Log.i("DEV", "sleep "+is.available()); android.os.SystemClock.sleep(100); } br=new BufferedReader(new InputStreamReader(dataInputStream)); String st = null; while(socket.isConnected()){ st = br.readLine(); } Log.w("server response", "says Server = " + st); Dbase db2=new Dbase(mcontext); db2.addresponse(new info(st)); Log.w("second time server response", "says 2ndTime Server = " + st); } catch (UnknownHostException e) { Log.e("at exception", "at thread unknownHost " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { Log.e("io exception", "at thread IO " + e.getMessage()); e.printStackTrace(); } finally{ Log.i("on finally block", "finally"); if (dataOutputStream != null){ try { dataOutputStream.close(); } catch (IOException e) { Log.e("io eception", "at thread dataoutput IO " + e.getMessage()); e.printStackTrace(); } } if (dataInputStream != null){ try { dataInputStream.close(); } catch (IOException e) { Log.e("data input exception", "at thread datainput IO " + e.getMessage()); e.printStackTrace(); } } if (socket != null){ try { Log.i("socket", "socket closed"); socket.close(); } catch (IOException e) { Log.e("socket exception", "at thread finally IO " + e.getMessage()); e.printStackTrace(); } } } return null; } @Override protected void onPreExecute() { super.onPreExecute(); //displayProgressBar("Downloading..."); } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } } 

You use InputStream.available method incorrectly. This method will not try to retrieve data. Actually most implementations of this method always return 0. See Inputstream available reference . I recommend you just remove available checks and let readLine block as needed. The same goes for BufferedReader.ready - generally this does not show that you will get any data when attempting to read so this call is also not useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM