I'm Building Taskito
I'm Building Taskito
Get it on Google Play Download on the App Store

Transfer data from Client to Server using Sockets

In the previous post - Connect Two Android Devices over WiFi using Network Service Disocvery, we showed how we used Network Service Discovery to connect two android devices over WiFi. In this post, we are going to show how we used this to our advantage to connect and send data to the server client.

We used the most basic thing that we found in the Android API - Socket. Socket class provides a client-side TCP socket. Socket binds destination IP Address and a port to local IP Address and a local port. Read more here.

How to Create Connection between Client and Server

  • Using NSD Manager, Client device should get server/host IP Address.
  • Send data to server using Socket.
  • Client should send its IP Address to server/host for bi-directional communication.

First step is done and we have obtained server’s IP Address. Now, we need to send Client devices’s IP Address to the server. For this we would use JSONObject as JSONObject can be easily converted to String and sent via Socket.

Client Device

Obtain IP Address of Client’s Device

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ipAddress = Formatter.formatIpAddress(

Connect to the Server and send IP Address using Socket

  • Resolve the service and call a function to connect to the server.
  • Create a JSONObject with fields request and ipAddress.
  • Create a new thread using AsyncTask for the Socket connection so that UI Thread doesn’t hang
  • In AsyncTask thread, create Socket connection. Send JSONObject as String and wait for Server’s repsonse.

You may find this helpful

Connect devices using NSD

Use Network Service Discovery and Bonjour to connect devices over WiFi. This article will show you how to connect over WiFi and send files.

Read now

Server Device

After setting up the service using NSD Manager, wait for the client to connect and send request with IP Address. For this we used ServerSocket. ServerSocket represents a server-side socket that waits for incoming client connections. A ServerSocket handles the requests and sends back an appropriate reply.

  • Create a Thread in background.
  • Initialize ServerSocket and wait for client to connect and send request.
  • Once client sends a request, perform some actions and give an appropriate response to the client.

This is how we connected Two Android devices over WiFi without having to enter any IP Address as it gets cumbersome.

We got a lot of help from Android-er.

We plan to create a bi-directional connection and data service between Client and Server and both respond with appropriate data based on the query.

P.S. Configuring and testing all this took a lot of time, but these small things make me really happy. More post coming soon.

Playing around with Android UI

Articles focusing on Android UI - playing around with ViewPagers, CoordinatorLayout, meaningful motions and animations, implementing difficult customized views, etc.

Read next