Simple Chat Application(Client-Server) in Android

Posted on March 3, 2012

48


Here is an example of simple chat app in Android I created for an assignment, which was quite a fun . It is a simple client-server based model using java socket programming. It’s also a very good tutorial for java socket programming. From your android device you would be able to send a message to a server. Other clients connected to that server will be able to read your msg. Again they can send message to server,which will be displayed in your android device.I used a ListView to display all the messages.

One hectic part of this code was to design the display.Place listview,button and edittext in way such that listview doesn’t overlap other views.At last I found that we need to add the following line to Listview to make it stable.

android:layout_weight="1.0"

Full main.xml file is like this:

</pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 >
 <ListView
 android:id="@+id/listView"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:layout_weight="1.0"
 ></ListView>

<LinearLayout
 android:id="@+id/llout"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"

android:gravity="bottom">
 <EditText
 android:id="@+id/txt_inputText"
 android:layout_width="125px"
 android:layout_height="wrap_content"
 android:text = "Text here"
 android:layout_weight="0.8"
 />
 <Button
 android:id="@+id/btn_Send"
 android:layout_width="100px"
 android:layout_height="wrap_content"
 android:layout_weight="0.2"
 android:text="Send"
 />
</LinearLayout>
 </LinearLayout>

The client side code which sends msg to server:

<pre>// note localhost is 10.0.2.2, not 127.0.0.1!
      String host = "10.0.2.2"; // or "opuntia.cs.utep.edu"
      Socket socket = new Socket(host, 8008);
      PrintWriter out = new PrintWriter(
        new OutputStreamWriter(socket.getOutputStream()));

      // send data to the server
      out.println("Hello!");
      out.flush();</pre>

Server-Side Code:

<pre>String host = "10.0.2.2"; // or "opuntia.cs.utep.edu"
      Socket socket = new Socket(host, 8008);
      BufferedReader in = new BufferedReader(
        new InputStreamReader(socket.getInputStream()));
      while (true) {
        String msg = in.readLine();
	if (msg == null) {
	    break; // disconnected
        } else {
            ... append msg to the ListView ...
        }
      }</pre>

Note that the code receiving messages from the server must be
run in a new thread, not in the Android UI thread! However,
appending the received messages to the ListView must be
done in the UI thread. How? Use the Handler class class.

// to be executed in the UI thread so that it is associated
// with the UI thread.
Handler handler = new Handler();

// to be executed in a new thread
handler.post(new Runnable() {
public void run() {
… append a message to the ListView …
}
});

You can found the code here https://www.box.net/shared/130ph7fsbgrj1h69pksq

Run the file Chatserver.java using java  from command prompt.This command prompt will act as the server. In another command prompt type “telnet localhost 8008″.The later one is the client.

 

Thanks for visiting the post. Please leave your comment/suggestions below.

About these ads
Posted in: Android