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.

 

 

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

Server Implementation

Implementing Server.java

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.

Next we will create object of  “SocketServerReplyThread” which extends thread and we pass socket and count to the constructor. Next obtain OutputStream from Socket using getOutputStream() function. A PrintStream is now created using OutputStream object as the new print stream does not automatically flush its contents to the target stream when a newline is encountered. After that we print() replay on PrintStream and Stream is closed. Huhh so much of theory.

At last we will need a method to get IP address of our server.

Full implementation of Server.java class

Implementation of MainActivity.java class

The usage of server class is very simple. Just create a object of Server class and pass MainActivity instance in constructor and you are done. 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()

  • cie

    thank you 🙂

  • Cyber

    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

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

  • 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 asap

  • David Audrain

    SocketServerThread.run should call SocketServerReplyThread.start and not SocketServerReplyThread.run.

  • Man>Daan

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

  • waysi89

    Thank you, helped me a lot!

  • Muhammad Ikhsan

    The activity_main.xml source code on the article is wrong, it is for http://androidsrc.net/android-web-crawler-example-multithreaded-implementation/. Check the one in the complete source code, for the correct one (https://github.com/androidsrc/SocketServer/blob/master/app/src/main/res/layout/activity_main.xml).

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

  • nikhil sani

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

  • Yasmine Hamdani

    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?

  • Thiago

    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)!!!!