public class MyClientTask extends AsyncTask<void,> { String dstAddress; int dstPort; String response = ""; MyClientTask(String addr, int port) { dstAddress = addr; dstPort = port; } @Override protected Void doInBackground(Void... arg0) { try { GlobalClass.socket = new Socket(dstAddress, dstPort); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int bytesRead; InputStream inputStream = GlobalClass.socket.getInputStream(); while ((bytesRead = inputStream.read(buffer)) != -1){ byteArrayOutputStream.write(buffer, 0, bytesRead); response += byteArrayOutputStream.toString("UTF-8"); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); response = "UnknownHostException: " + e.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); response = "IOException: " + e.toString(); }finally{ if(GlobalClass.socket != null){ try { GlobalClass.socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; }
MyClientTask myClientTask = new MyClientTask(ip.getText().toString(),Integer.parseInt(port.getText().toString())); myClientTask.execute();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)