Simple connection example part II - TCP communication
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.
- try {
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
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:
- try {
- while(!end){
- //Server is waiting for client here, if needed
- Log.d("Tcp Example", "From client: "+st);
- output.println("Good bye and thanks for all the fish :)");
- s.close();
- if ( STOPPING conditions){ end = true; }
- }
- ss.close();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
Last see the client side:
- try {
- //outgoing stream redirect to socket
- output.println("Hello Android!");
- //read line(s)
- . . .
- //Close connection
- s.close();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
That’s it. Hope you found this article helpful.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
8 years 29 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
8 years 29 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
8 years 29 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
8 years 29 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
8 years 29 weeks ago
Poll
Useful resources
Android Development Projects
- Native app, Android and IoS, both front and back end.
- data entry -- 2
- Taxi booking app like (uber clone- ola clone- careem)
- build an app
- i need an android app built in java that when i scan a qr or barcode its later saved on phpmyadmin
- App para registros de transação p2p
- I want ICar games programmer IOS and Android
- Notification of current location
- Mobile App
- Xamarin iOS keyboard retraction in ListView cell editor