I am attempting to create an application which has a edit text view and a button. This button will send whatever is in the edit text through a TCP connection to the server.
I have accomplished the sending of the edit text, but after I click the button once, the application crashes. How can I put this into a loop so I can send multiple messages? Here is my source
public class MainActivity extends Activity {
//Handler h;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText eText = (EditText) findViewById(R.id.address);
final TextView tView = (TextView) findViewById(R.id.pagetext);
final Button button = (Button) findViewById(R.id.ButtonGo);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
Socket s = new Socket("192.168.0.117", 4447);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = ((EditText)findViewById(R.id.address)).getText().toString().trim();
out.write(outMsg);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
} catch (UnknownHostException e) {
tView.setText(e.toString());
Log.v("Tcp",e.toString());
} catch (IOException e) {
tView.setText(e.toString());
Log.v("Tcp",e.toString());
}catch (Exception e) {
tView.setText(e.toString());
}
}
});
}
}
I accomplished this by using two different buttons. A connection button, and a send message button. I think when the server attempted to recreate the socket, the TCP connection broke and the server caught an exception and starting freaking out. Here is the new code.