How to maintain the connection between Android client Java server?

1

When I connect to the server, I should receive a message saying that it is connected, but when I send a message to the server, the application pops up.

This is the button code to connect to the server:

public void connect(View view)
{
    TextView tv=(TextView) findViewById(R.id.textViewOut);
    String ipAddress=((EditText) findViewById(R.id.editTextIP)).getText().toString();
    String port=((EditText) findViewById(R.id.editTextPort)).getText().toString();

    try 
    {
            InetAddress serverAddr = InetAddress.getByName(ipAddress);
            skt = new Socket(serverAddr,Integer.parseInt(port));
            String st="";
             reader = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            st = reader.readLine();
            //reader.close();
            if(skt.isClosed())
            {
                st="is Close";
            }

            Toast toast = Toast.makeText(this,st,Toast.LENGTH_SHORT);
            toast.show();

    }catch (UnknownHostException e1) 
    {
         tv.setText("Error1");
         e1.printStackTrace();
    }catch (IOException e1) 
    {
         tv.setText(""+e1);
         e1.printStackTrace();

         Toast toast = Toast.makeText(this,""+e1,Toast.LENGTH_SHORT);
            toast.show();
    }       
    Toast toast = Toast.makeText(this,ipAddress+" : "+port,Toast.LENGTH_SHORT);
    toast.show();
}

This is to keep the exchange of messages between the server and the client:

public void sendMessage(View view) 
{
    // Do something in response to button
    TextView tv=(TextView) findViewById(R.id.textViewOut);
    EditText editText = (EditText) findViewById(R.id.editTextIn);
    String message = editText.getText().toString();
    int contador=0;
    try 
    {
        String st="";

        if(skt.isClosed())
        {
            st="is Close";
        }

        Toast toast = Toast.makeText(this,st,Toast.LENGTH_SHORT);
        toast.show();

         out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())),true);
        out.flush();

         reader = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        st = reader.readLine();
     toast = Toast.makeText(this, st ,Toast.LENGTH_SHORT);
        toast.show();

        out.println(message);
        Log.d("Client", "Client sent message");
     } 
    catch (UnknownHostException e) 
    {
        tv.setText("Error1");
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        tv.setText("Error2");
        Toast toast = Toast.makeText(this, ""+e,Toast.LENGTH_SHORT);
        toast.show();
        e.printStackTrace();
    } catch (Exception e) 
    {
        tv.setText("Error3");
        e.printStackTrace();
     }

    Toast toast = Toast.makeText(this,""+contador,Toast.LENGTH_SHORT);
    toast.show();                                                  
}

I tried to close the reader after receiving the message from the server, but it did not work. Even now I have declared all the variables as global but nothing ... always gives the same error:

    
asked by anonymous 07.02.2015 / 19:18

0 answers