I am a beginner in android development,
I have to do the following when a button is clicked using a Async Task.
- connect to a specific TCP server using ip and Port and Check if its connected? on failure show a toast message
- on success send a string to the tcp server
- close the connection.
I had used the code below for connecting
try
{
s= new Socket("192.168.43.205",20108);
out = new BufferedWriter( new OutputStreamWriter(s.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch (UnknownHostException e) {
tv.setText(e.toString());
Log.v("Tcp", e.toString());
}
catch (IOException e) {
tv.setText(e.toString());
Log.v("Tcp",e.toString());
}
catch (Exception e) {
tv.setText(e.toString());
}
but this usually hangs when the server isn't available. Is there a fix for this?