Madhuranga Lakjeewa's Cyberspace

Wednesday, January 7, 2015

Android Client-Server Chat Application

This is a simple Android chat application which communicate with a Java server program. This tutorial will help you to understand socket programming in Android which sends information both ways.

To run this application, first execute the server program. You can run this application in your PC.

Then execute the client program. You can use your Android emulator or your Android device. If you are using Android emulator you can use IP address 10.0.2.2 to connect your sever which is running in the same computer. But if you are using an Android device you may have to change the IP address in the client app code according to your network configuration.

Once you execute the client app, the chat window will be opened in the server program. Type the message on the chat box and press "Send" button. The message will be appeared in the other end.

Android Chat App

Server Program

Following are the source codes of the main components of both programs.

Android Chat ApplicationServer ProgramFollowing are the both Android client and Java server complete projects. You can clone or download them as zips from Git-Hub.

40 comments:

I executed the application but i wanted to know if i received a string from the client and i assigned it to a variable and wanted to apply a function on that variable and then send it back. Is it gonna be easy to do that. I already asigned the string received to a variable in the "simplechatserver.Receiver" class but i don't know how to use that variable in the Sender class.

Yes its easy, you can do that.You should do this in the sever program. Both "Receiver" and "Seder" are initialized in the "ChatWindow" class. Therefore the common place for both "Receiver" and "Seder" is "ChatWindow" class. Keep global references (variables) to both "Receiver" and "Seder". Create method inside "ChatWindow" which does your particular string operation and call receiver to send the message. Pass a reference to ChatWindow class when you create the Receiver class. You should change the Receiver class constructor to do that. From that you may be able to call your particular method when the Receiver receives a message. You should call that method after line number 50 of Receiver.java class. Hope you can understand my guideline. :)

and actually what i want to do is per example : i send a number ( ex : 17 ) to the server and the server sum this number with another number and return the result to the android client.I kinda don't fully understand what you wrote.

String string = message;String[] parts = string.split("-");String part1 = parts[0]; String part2 = parts[1]; i added these lines after "message = bufferedReader.readLine(); // Read the chat message. "so that i split the string that i am receiving.than i want to apply a method to these strings example func ( part1,part2,number1,number2)and then send the result back to the client.

I can present whole answer from a comment, but i will brief and highlight the most important places you should change. Following are the ChatWindow and Receiver classes. This will help you to understand the above comment.

private void initSenderAndReceiver(final Socket clientSocket) { // Using this key word you can pass the reference to ChatWindow to the Receiver. Receiver receiver = new Receiver(clientSocket, chatView, this);

In my first comment i told you to keep global variables for "Sender" and "Receiver" in the ChatWindow class. Let's say Sender's global variable is "sender" (private Sender sender;). You are calling yourMethod(String message) which is in the ChatWindow class. So you can simply call the sender.sendMessage(String message) method inside youMethod.Your method will look like:

public void yourMethod(String message){

// Do whatever you want here. Lets say you are producing// a string called "yourString" after all your manipulations on "message".// Then add the following line to this method.sender.sendMessage(yourString);

Wifi is a network technology. You can create different types of networks using that technology. One simple way is creating a wifi hotspot using your PC and connect the Android device to that network. If you are using a Windows OS, here i have described how to do it.http://lakjeewa.blogspot.com/2014/08/share-laptop-internet-connection-with.html

I need your help to break out some functionality in the client side.Right now when the app is started, the socket is created and listens for incoming server messages.I would like to connect creation of the socket to a button so when I click on the connectButton then socket is being created in the background

Hi Masoud,Sorry for the delay. It is possible, you can simply do it. I will explain.You know that, now the socket is created when the app is started. That is true. Actually the onCreate() method (line 40) is called when the application is started. There a ChatOperator object is created and chatOperator.execute() method create the socket.

Create a new button (like in line 44) for your "Connect" button. Then add the following lines of code instead of lines 47 and 48.

What the above few lines does is, a click action listener is bound so that the socket creation happens when the button is clicked. isConnectButtonClicked boolean variable will keep the status of connect button clicked status. The user should only be able to connect once, otherwise the the socket will create again and again at the each time the user click the button.

Thanks, it worked like a charm.However I have a disconnect button as well. Once I click on that, it releases the socket and enable the connect button. As the connect button now is inside the Oncreate procedure, it will never be called for a second time when I click on it. You can find the code here: http://www.testlead.se/android/MainActivity.java

Local Hot Chat's free phone sex chatline can help you connect with real people from your local calling area. Take a free trial of our chat line and connect to the largest local network. Site: www.freechattingbabes.com

You can do it. You just want to create an android app which does the server functionality. You can use the same code handle server side functionality. But creating the network between the android devices is the trickiest part. :)

Your requirement is bit of advance than the tutorial.If you go through the server program you will see that i am creating only one thread for receiver(Client). Just look at line #87 in ChatWindow class. If you are going to have more client you may have to create threads for each.

Thanks for you suggestion but just i want to know a little bit idea/example.

If i will be implement by using multi thread it will be possible to multiple client communicate with the Server at time simultaneously. Could you please guide me or suggest/idea/ or provide me the code how to implement it.

1. Any webpage open in WebView Intent in application.and set cookies in device. now that stored cookies. can we read from another android application which is another application.(Cookies communicate within two application one application is doing write cookies and second application read that stored cookies.)

Hello,I am getting NullPointerException at Client side. Here is my logcat view that shows exception in detail. Please Help me out for this.

E/AndroidRuntime(12635): java.lang.RuntimeException: An error occured while executing doInBackground()E/AndroidRuntime(12635): at android.os.AsyncTask$3.done(AsyncTask.java:304)E/AndroidRuntime(12635): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)E/AndroidRuntime(12635): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)E/AndroidRuntime(12635): at java.util.concurrent.FutureTask.run(FutureTask.java:242)E/AndroidRuntime(12635): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)E/AndroidRuntime(12635): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)E/AndroidRuntime(12635): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)E/AndroidRuntime(12635): at java.lang.Thread.run(Thread.java:818)E/AndroidRuntime(12635): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.BufferedReader.ready()' on a null object referenceE/AndroidRuntime(12635): at com.example.demochatappclient.MainActivity$Receiver.doInBackground(MainActivity.java:110)E/AndroidRuntime(12635): at com.example.demochatappclient.MainActivity$Receiver.doInBackground(MainActivity.java:1)E/AndroidRuntime(12635): at android.os.AsyncTask$2.call(AsyncTask.java:292)E/AndroidRuntime(12635): at java.util.concurrent.FutureTask.run(FutureTask.java:237)