How to maintain connection between Android Java server client?

Asked

Viewed 275 times

1

When I call the server I should receive a message saying that connected, only when I send a message to the server the application overflows.

This is the code of the button 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 maintain 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 Reset after receiving the message from the server, but it didn’t work. Even now I have declared all variables as global but nothing... always gives the same error:

  • 2

    Edit your question and add the error stack trace. Only with code is it much harder to find the problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.