title: Java Android Socket Connection. Wrong ip address causing application to stop responding
body: <p>I have a socket connection from an android phone to a server and the code works fine when i enter the correct IP address and port number but if they are incorrect the application stops responding and tells me to force quit. I would assume that the <strong>catch(UnknownHostException e)</strong> would catch that but the code stops responding at <strong>socket = new Socket(_ip, _portNum);</strong>. _ip and _port are passed into it by what the user enters into the phone. i need to make it so if someone enters the wrong IP or Port it wont crash but tell them to enter again.</p>
  
  <pre><code>    public void Connect(String _ip, int _portNum) throws ParserConfigurationException, SAXException, IOException{  
  
      //CREATES SOCKET
      Socket socket = null;
      DataInputStream input = null;
      DataOutputStream output = null; 
      //CREATE DATA STREAMS   
      try{
          Log.e("trying to connect","trying to connect"); //sends to log
          socket = new Socket(_ip, _portNum); //stops responding here.
          Log.i("Socket Connected", "Socket Connected"); //not sent to log
  
          output = new DataOutputStream(socket.getOutputStream());
          input = new DataInputStream(socket.getInputStream());       
  
          }catch(UnknownHostException e){
          }catch(IOException e){
          }
  
          //IF STREAMS ARE OPEN SEND MESSAGES
      if (socket != null &amp;&amp; output != null &amp;&amp; input != null){          
          try{
  
              //MESSAGES FOR TESTING          
              output.writeBytes("HELLO2 \n");             
  
              String response;
              while((response = input.readLine()) != null){
                  System.out.println("Server: " + response);                  
  
                  //MESSAGE FOR TESTING
                  output.writeBytes("Test \n");
  
                  TextView test = (TextView) findViewById(R.id.TestText);
                  // display text returned from server
                  test.setText(response);            
              }   
  
              //CLOSE STREAMS AND CONNECTION          
              output.close();
              input.close();      
              socket.close(); 
                  Log.i("Connections closed", "Connections closed");
              }catch(UnknownHostException e){
              }catch(IOException e){
              }
      }
  }
  </code></pre>
  
  <p>Any assistance is greatly apprectiated.</p>