Android Client-Server Using Sockets – Client Implementation

In this Android Client-Server Using Sockets post we will be focusing on client side implementation. We have covered Server side implementation in our previous post “Android Client-Server Using Sockets – Server Implementation”


Implementation Details

In this tutorial we will be using java Sockets to achieve our server-client communication. In client user will fill IP and port details in EditText and then it will press connect. Server will replay to the query. For implementation of Client we will be extending AsyncTask . Socket is created using IP and port detail and replay from server is decoded.

 

[su_button url=”https://github.com/androidsrc/SocketClient” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Full Source Code[/su_b[/su_button]p> 

Note : This tutorial is based on Eclipse 4.3, Java 1.6 and Android 4.4.2.

Client Implementation

Implementing Client.java

This class contain all the implementation of client. This class is created by extending AsyncTask so that multitasking will be easy. We create a socket using IP and port detail which was provided by the user. Next getInputStream() is called on created socket to get InputStream so as to read data from this socket. Connection is blocked untill no data is read. This all must be done in try catch block so that exceptions must be handled.

GuRu

Technology enthusiast. Loves to tinker with things. Always trying to create something wonderful using technology. Loves coding for Android, Raspberry pi, Arduino , Opencv and much more.

You may also like...

25 Responses

  1. Denis says:

    thanks for tutorial!
    why both device should be connected to same wifi network? how can I connect devices via 3G?

  2. Josh says:

    nice tutorial.
    will this work if a pc running .net asynchronous server socket is used instead of the android server?

  3. Abhinav Bhargav says:

    hi,

    I would like to know one thing(client and server are 2 different projects/apps)

    line1 outputStream = hostThreadSocket.getOutputStream();

    line2 PrintStream printStream = new PrintStream(outputStream);

    line3 printStream.print(msgReply);

    line4 printStream.close();

    if line3 printstream.print(msgReply) which is server side code is sending some useful message to client.

    Now if i want to use that message in my client program, how can i ?

    How can i save that message in client project in a variable or anything else? please help.

  4. Roel says:

    I can’t help it but I can’t take examples serious with Eclipse “// TODO Auto-generated catch block” parts.

  5. nikhil sani says:

    which IP address to enter ?
    not able to connect when i entered IP address which is showing in Server app

  6. manoj says:

    hi sir,good tutorial
    how to File transfer via Socket, between Android devices, is there any solution
    please give me answer, how to solve these problem

  7. Darren Close says:

    Hi there, this tutorial has helped me so much so thank you. I am trying to write an app that when it receives a message via the Socket which contains a specific phrase e.g. “SEND” it will invoke some other code (mainly send a text). Can you suggest where I put this code as I need the app to continually listen to the incoming messes and then invoke this new code when the condition is met.

  8. Ishan Mourya says:

    Can we use this for Making a chatting app….means connect 2 devices using server and client and then chat between them

  9. Conrad Addo says:

    How do I decide the port number?

  10. Yeac says:

    Is it possible to have more than one client connection??

  11. Aruna Gamage says:

    How do I decide the port number?

  1. December 12, 2015

    […] Kaynak: Android Client-Server Using Sockets – Client Implementation […]

  2. February 2, 2017

    […] on server side implementation. We have covered Client side implementation in our previous post “Android Client-Server Using Sockets – Client Implementation” Android has very vast set of libraries so that users can implement innovative ideas with provided […]

  3. February 24, 2017

    […] have followed this tutorial to get the basic […]

Leave a Reply

Your email address will not be published. Required fields are marked *