Simple Chat Application(Client-Server) in Android

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.

50 thoughts on “Simple Chat Application(Client-Server) in Android

  1. when i run ChatServer using cmd
    I got the following error

    C:\Documents and Settings\abhi\Desktop\defineChat\definechat\src\define\chat\cmd>java ChatServer
    Exception in thread “main” java.lang.NoClassDefFoundError: ChatServer (wrong name: define/chat/cmd/ChatServer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: ChatServer. Program will exit.

    • @Amit Kumar: First try to run the file using the command : java ChatServer.java
      This will create ChatServer.class file. Then try command : Java ChatServer

  2. I will run the file using command: javac ChatServer.java
    And it will create ChatServer.class file. Then try the command:
    Java ChatServer
    But it will Give the same Error.

    Exception in thread “main” java.lang.NoClassDefFoundError: ChatServer (wrong name: define/chat/cmd/ChatServer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: ChatServer. Program will exit.

  3. Gettin same error as amit kumar, any help? Towhid

    Exception in thread “main” java.lang.NoClassDefFoundError: ChatServer (wrong nam
    e: edu/UTEP/android/ChatServer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

  4. after commanding at command prompt – java chatserver.java

    Exception in thread “main” java.lang.NoClassDefFoundError: chatserver/ja
    Caused by: java.lang.ClassNotFoundException: chatserver.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java:220)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:208)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s