Android Client-Server Using Sockets – Server Implementation

In this Android Client-Server Using Sockets post series we will be focusing 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 libraries. It is very easy to create server in android using these inbuilt library.

Implementation Details

In this tutorial we will be using java Sockets to achieve our server-client communication. One side there will be a server which will bind to specified port on device and will be available to client using IP address and port combination. Client will also use some random port for connection. Once connection is established from client side then server will replay to client with “Hello from Server, you are #%d” %d will be a number which will increment with each connection.


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

Note : This tutorial is based on Android Studio 2.2, Java 1.6 and Android 6.0.

Server Implementation


This class contain all the implementation of server. In this class we will create object of “ServerSocket” in a separate thread. accept() function in ServerSocket waits for an incoming request and blocks until the connection is opened. This method returns a socket object representing the just opened connection. IP address and port number of client can be obtained from this socket.


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

17 Responses

  1. cie says:

    thank you 🙂

  2. Cyber says:

    I need a quick solution, I have a p2p communication with a device on 4000 port. I need to write a server to receive the messages from this device, since it’s not broadcasting, but gives feedback on a designated ip address. However the result from this app I receive is more like
    “#322 from 192:168.1.6:28617” <- true this is the ip of the application. But I need the response from the device.

    obviously I don't really understand much about socket servers, additionally I need to run this multithreading with other client sockets I wrote and running. But I have a very limited time.

    Can you explain me how can i see the message from this ip?

    • Cyber says:

      Well it turns out I just need to get the data with datainputstream. Thank you for the tutorial! It was really helpful

  3. Abhinav Bhargav says:


    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 asap

  4. David Audrain says: should call SocketServerReplyThread.start and not

  5. Man>Daan says:

    why both should be on same wifi network ? Can we not use external/public ip ?

  6. waysi89 says:

    Thank you, helped me a lot!

  7. Muhammad Ikhsan says:

    The activity_main.xml source code on the article is wrong, it is for Check the one in the complete source code, for the correct one (

  8. nikhil sani says:

    could you please tell me a method to transfer the data between two clients through server

  9. Yasmine Hamdani says:

    Hello, thank you for the tuto. I’m new to implementing server on android and I would like to know if your code can handle more than one client?
    I mean, the while(true) loop makes it so that each client would have his own socket? Socket that is closed at the end of that loop?
    What happens if two users send a request at the same time?

  10. Thiago says:

    Hello my friend! Thank for your post. I have a question…its possible print the received message on TextView? I can’t do this…Thanks (and sorry my English)!!!!

  11. Salimb53 says:

    Ciao everybody, thank you for your post, i’m using it perfectly but i want to now if it’s possible to the server to replay pictures or videos (not only text !). Merci.

  12. Jacob says:

    Your app sometimes does’t work correctly.
    At the first time, it works properly, but next fime I got message like as “IOException:java,net.NoRouteToHostException: Host unreachable” on client app.
    Can you resolve this problem?

  1. July 1, 2015

    […] Android Client-Server Using Sockets – Server Imple […]

  2. January 30, 2017

    […] Download Complete Source Code[/su_button]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” […]

  3. December 14, 2018

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

Leave a Reply

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