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.

 

 

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.

Implementation of MainActivity.java class

The usage of client class is very simple. Just create a object of Client class and pass IP and port of server and response TextView. Here is what MainActivity.java looks like.

Other supporting components

Implementation of activity_main.xml file.

 AndroidManifest.xml

We will need permission INTERNET to create Sockets. Don’t forget to include that permission in your manifest.

Testing the application

To test this application install it on android device and install client on other device. Both device should be connected to same wifi network. Now just add IP address and port to client and click connect. Server will replay with a message to client. Thanks

Any further queries or custom tutorial please comment or mail us at androidsrcdotnet@gmail.com

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...

  • Pingback: Android Client-Server Using Sockets – Client Implementation | Hasan Özdemir()

  • Denis

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

    • Create a hotspot on your first phone and then connect the second phone to it. Then this will work.

  • Josh

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

  • Abhinav Bhargav

    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.

  • Roel

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

  • Pingback: Android Client-Server Using Sockets - Server Implementation()

  • nikhil sani

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

    • Nitin Trivedi

      I haven’t tried it. But I guess,in Client.java you should enter “return response” instead of “return null”, so that response is set on TextView

      • thanks for pointing out. code has been updated.

        • Eric

          But how can a Void return a String?

          the “return response;” is giving me this error.

          • Aiman Baharum

            You need to change the AsyncTask generic type to and override the correct methods.

          • sheeza

            its not working kindly give me right solution

          • Ian Marks

            I had same issue. When I checked the code in GitHub I saw that “return response” was changed to “return null”

          • Miguel Y.

            @Override
            protected void onPostExecute(String result) {
            textResponse.setText(response);
            super.onPostExecute(result);

            maybe you miss that

            }

      • nikhil sani

        thanks a lot its working now

    • Devesh Pandya

      the IP of the server(PC) on which you want to connect and chat.

  • manoj

    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

  • Darren Close

    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.

  • Pingback: Connecting Android socket client to C socket server – TheInstaPreneur()

  • Ishan Mourya

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

  • Conrad Addo

    How do I decide the port number?

  • Yeac

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