0

I am trying to establish a client connection to a TCP Socket Server. I have created a Client class which implements the Runnable interface, now within an Activity class im trying to initialize a Client object. The Activity class:


public class ClientApp extends AppCompatActivity {

   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

                clientButtons(); 

    }



    public void clientButtons(){

        final Button buttonOne = (Button) findViewById(R.id.buttonOne);
        final Button buttonTwo = (Button) findViewById(R.id.ButtonTwo);

        buttonOne.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Client client = new Client("10.0.2.2", 5555);
                        client.run();
                        client.getPrintWriter().println("Hi, this is button one");
                        Log.d("client", "run: "+client);
                    }
                }).start();



            }
        });

        buttonTwo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Client client = new Client("10.0.2.2", 5555);
                        client.run();
                        client.getPrintWriter().println("Hi, this is button Two");
                        Log.d("client", "run: "+client);
                    }
                }).start();
            }
        });
    }


}

Now as you can see in this Activity class, based on button click i should write or send data to the server. Right now after each button click either in buttonOne or buttonTwo i am instantiating a new socket connection each time i want to write?

I am going to guess and say i am doing something completely wrong? Is there another way than to have to instantiate a new Client object each time? or is this how you generally have to do it?

SlimD
  • 23
  • 1
  • 7
  • You have the same code for two buttons. Why? You never close your clients. Futher your client does not write/send a message. And does not read/receive a message . Please do. So connect. Send a message. Read the response and close the connection. All in one button handler. After you managed that you can change it to keep the connection open. – blackapps Dec 26 '21 at 11:16
  • @blackapps Hi!, the reason im using the same code twice is because if i instantiate the Client object outside of the inner class method, i will end up with a null printWriter in the inner class method if i call it. This is my main problem. Also, it does work fine, i can write to the server, but yes i havent closed the stream. – SlimD Dec 26 '21 at 11:20

0 Answers0