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

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

2 Responses

  1. John Hurtado says:

    Thanks. How to make the server always listening?

Leave a Reply

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