Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6683487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:49:38+00:00 2026-05-26T04:49:38+00:00

I’m doing a lan chat app bewteen android (client) and pc (java_server) both running

  • 0

I’m doing a lan chat app bewteen android (client) and pc (java_server) both running on eclipse.The problem is android very untable and very often crash, however, its running very well on my java version of client and server. please help!

there is my code:

package android.client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.PriorityQueue;
import java.util.Queue;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
 import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Chat_client extends Activity implements Runnable {

private Socket socket = null;
//private Thread thread = null;
private DataOutputStream requestOut;
private int clientID;
private String playerName;
private DataInputStream requestIn;
private EditText text;
private ListView msgView;
private ArrayAdapter<String> msgArrayAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        socket = new Socket("192.168.2.2", 5558);
        Toast.makeText(this,
                "Connected to " + this.socket.getInetAddress(),
                Toast.LENGTH_LONG + 10).show();
        this.playerName = "wei";
        this.clientID = socket.getLocalPort();
        requestOut = new DataOutputStream(socket.getOutputStream());
        requestIn = new DataInputStream(socket.getInputStream());
        text = (EditText) findViewById(R.id.editText1);// black textfield
        msgArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
        msgView = (ListView) findViewById(R.id.in);
        msgView.setAdapter(this.msgArrayAdapter);
        new Thread(this).start();
    } catch (UnknownHostException uke) {
        Toast.makeText(this, "Host unknown:" + uke.getMessage(),
                Toast.LENGTH_LONG).show();
    } catch (IOException ioe) {
        Toast.makeText(this, "Unexpected exception: " + ioe.getMessage(),
                Toast.LENGTH_LONG).show();
    }
}


// This method is called at button click because we assigned the name to the
// "On Click property" of the button
public void myClickHandler(View v) {
    Toast.makeText(this, "sending", Toast.LENGTH_LONG/2).show();
    TextView view = (TextView) findViewById(R.id.editText1);
    String message = view.getText().toString();
    text.setText("");
    Packet p;
    if (message.startsWith("#")) {
        p = new Packet(playerName, 1, clientID,message.substring(1,    message.length()));
    } 
    else 
        p = new Packet(playerName, -1, clientID,message);
    try {
        this.requestOut.writeUTF(p.convertToSendFormat());
        requestOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    msgArrayAdapter.add("Me: <" + message + ">");

}

public void run() {
    while (true) {
        String r;
        try {
            r = this.requestIn.readUTF();
            if (r != null) {
                Packet p = convert(r);
                if (p != null)
                    handle(p);
            }
        } catch (IOException ioe) {
            System.exit(0);
        }
    }
}
private synchronized Packet convert(String n) {
    String data = "";
    String[] p = n.split(" ", 4);
    if (p.length < 4)
        return null;
    data = p[3];
    return new Packet(p[0], Integer.parseInt(p[1]), Integer.parseInt(p[2]),
            data);
}
public synchronized void handle(Packet re) {
    if (re.getChannelID() == clientID){
        msgArrayAdapter.add("Me" + re.toString());}
    else{
        msgArrayAdapter.add(re.getSenderName() + re.toString());
        }
    }

}

package android.client;

import java.io.Serializable;
/**
 * This class handles the networking packet
 * @author Wei
 */
public class Packet implements Serializable {
    private static final long serialVersionUID = 1509287973845358190L;
    private String senderName;
    private int actionID;
    private long channelID;
    private int sent;
    private String data;

    public Packet(String name, int actionID,int cid, String action2) {
        this.senderName=name;
        this.channelID = cid;
        this.actionID = actionID;
        this.data = action2;
        this.sent = -1;
    }

    public String getSenderName(){
        return this.senderName;
    }
    public int getActionID(){
      return this.actionID;
    }
    public String getData(){
        return this.data;
    }

    protected void setChannelID(int id){
        this.channelID = id;
    }
    public long getChannelID(){
        return this.channelID;
    }

    protected void setSent(boolean b){
        if(b)
        this.sent = 1;
        else
            this.sent=-1;
    }
    protected int checkSent(){
        return this.sent;
    }

    public String toString(){
        return (" : "+this.data);
    }
    public void addTimeStamp(String time){
        this.data+=time;
    }
    public String convertToSendFormat(){
        return this.senderName+" "+this.actionID+" "+this.channelID+" "+this.data;
    }
    public int getFlag() {
        return 0;
    }

    public void setFlag(boolean response) {
    }


}

there is the Log:

  10-12 12:54:22.763: DEBUG/AndroidRuntime(360): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
10-12 12:54:22.763: DEBUG/AndroidRuntime(360): CheckJNI is ON
10-12 12:54:22.895: DEBUG/AndroidRuntime(360): --- registering native functions ---
10-12 12:54:23.513: INFO/ActivityManager(59): Force stopping package android.client uid=10033
10-12 12:54:23.513: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=android.client/.Chat_client }
10-12 12:54:23.633: DEBUG/AndroidRuntime(360): Shutting down VM
10-12 12:54:23.645: INFO/ActivityManager(59): Start proc android.client for activity android.client/.Chat_client: pid=366 uid=10033 gids={3003}
10-12 12:54:23.664: DEBUG/jdwp(360): Got wake-up signal, bailing out of select
10-12 12:54:23.664: DEBUG/dalvikvm(360): Debugger has detached; object registry had 1 entries
10-12 12:54:24.024: WARN/ActivityThread(366): Application android.client is waiting for the debugger on port 8100...
10-12 12:54:24.044: INFO/System.out(366): Sending WAIT chunk
10-12 12:54:24.054: INFO/dalvikvm(366): Debugger is active
10-12 12:54:24.244: INFO/System.out(366): Debugger has connected
10-12 12:54:24.244: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:24.444: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:24.644: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:24.843: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:25.053: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:25.254: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:25.454: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:25.654: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:25.866: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:26.075: INFO/System.out(366): waiting for debugger to settle...
10-12 12:54:26.284: INFO/System.out(366): debugger has settled (1322)
10-12 12:54:27.544: INFO/ActivityManager(59): Displayed activity android.client/.Chat_client: 3912 ms (total 3912 ms)
10-12 12:54:33.313: DEBUG/dalvikvm(124): GC_EXPLICIT freed 642 objects / 35976 bytes in 203ms
10-12 12:54:38.335: DEBUG/dalvikvm(225): GC_EXPLICIT freed 152 objects / 11144 bytes in 177ms
10-12 12:54:43.384: DEBUG/dalvikvm(261): GC_EXPLICIT freed 249 objects / 11840 bytes in 205ms
10-12 12:55:14.583: WARN/KeyCharacterMap(108): No keyboard for id 0
10-12 12:55:14.583: WARN/KeyCharacterMap(108): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
10-12 12:55:37.295: WARN/dalvikvm(366): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:49:39+00:00Added an answer on May 26, 2026 at 4:49 am

    I have solved the prolbem by adding a handler method:

    private Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            String  str= (String) msg.obj;
            if(msg.what==1){
                msgArrayAdapter.add(str);
            }
        }
    };`
    

    and of course I have no idea why this works, but its just works.lol, thanks again for the help @Knickedi

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.