Simple connection example part II - TCP communication


SDK Version: 
M3
Last time I wrote about UDP connection. I got some comments about the problem, that UDP packets are not guaranteed to be delivered.
This time I’m going to show you the safer option, the TCP connection.
TCP is probably the most commonly used protocol, simply because it is used for so many applications such as HTTP, POP, SMTP, etc. TCP is a protocol which guarantees that the receiver will receive exactly what the sender sent - there will be no errors, it will be in the correct order, everything will work just fine.

TCP communication time diagram.

  1. try {
  2.         Socket s = new Socket("http://helloandroid.com",80);
  3. } catch (UnknownHostException e) {
  4.         // TODO Auto-generated catch block
  5.         e.printStackTrace();
  6. } catch (IOException e) {
  7.         // TODO Auto-generated catch block
  8.         e.printStackTrace();
  9. }

This is a simple client-side TCP request. If the connection is built up succesfully, you can get the needed proccesses for communication.

Now let’s see the server side example:

  1. try {
  2.         Boolean end = false;
  3.         ServerSocket ss = new ServerSocket(12345);
  4.         while(!end){
  5.                 //Server is waiting for client here, if needed
  6.                 Socket s = ss.accept();
  7.                 BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
  8.                 PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
  9.                 String st = input.readLine();
  10.                 Log.d("Tcp Example", "From client: "+st);
  11.                 output.println("Good bye and thanks for all the fish :)");
  12.                 s.close();
  13.                 if ( STOPPING conditions){ end = true; }
  14.         }
  15. ss.close();
  16.        
  17.        
  18. } catch (UnknownHostException e) {
  19.         // TODO Auto-generated catch block
  20.         e.printStackTrace();
  21. } catch (IOException e) {
  22.         // TODO Auto-generated catch block
  23.         e.printStackTrace();
  24. }

Last see the client side:

  1. try {
  2.         Socket s = new Socket("localhost",12345);
  3.        
  4.         //outgoing stream redirect to socket
  5.         OutputStream out = s.getOutputStream();
  6.        
  7.         PrintWriter output = new PrintWriter(out);
  8.         output.println("Hello Android!");
  9.         BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
  10.        
  11.         //read line(s)
  12.         String st = input.readLine();
  13.         . . .
  14.         //Close connection
  15.         s.close();
  16.        
  17.        
  18. } catch (UnknownHostException e) {
  19.         // TODO Auto-generated catch block
  20.         e.printStackTrace();
  21. } catch (IOException e) {
  22.         // TODO Auto-generated catch block
  23.         e.printStackTrace();
  24. }

That’s it. Hope you found this article helpful.