Satish J
Satish J

Reputation: 7

Client Server communication in android

hi guys im trying to write a code on client and server communication. here im getting unfortunate error in client side. and server is listening correctly. so any one go through my code and find out any error is der.

messsage = textField.getText().toString(); //get the text message on the text field
textField.setText("");      //Reset the text field to blank

try {

 client = new Socket("10.0.2.2", 4444);  //connect to server
 printwriter = new PrintWriter(client.getOutputStream(),true);
 printwriter.write(messsage);  //write the message to output stream

 printwriter.flush();
 printwriter.close();
 client.close();   //closing the connection

} catch (UnknownHostException e) {
 e.printStackTrace();
} catch (IOException e) {
 e.printStackTrace();
}

Upvotes: 0

Views: 296

Answers (1)

MaciejGórski
MaciejGórski

Reputation: 22232

Isn't it network on main thread exception?

You should not be doing network there. Use Thread, AsyncTask or HandlerThread.

Upvotes: 1

Related Questions