Simple connection example part II - TCP communication
Wed, 09/01/2010 - 03:07 | by jozsi
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.
- 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
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 1 day ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 1 day ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 1 day ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 1 day ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 1 day ago
Poll
Useful resources
Android Development Projects
- php,java,android - repost by jasmindestiny
- mobile application: plattform for students to share furniture etc... - repost by teamnumber2
- Google Merchant account holder required by ovia19
- Javascript to include in my mobile apps (Android) by rpgnox
- Developing an "Interactive Whiteboard" App on Android or IOS by ChiBunWong
- Android alarm app by ahsanSBY
- Make An Game For Android by androidev
- Make website responsive and with customised flash by allareasprinting
- transperant video app by coolblue55
- Play an Android Game & Earn Cash by gamerveda




