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?