什么是一个Android应用程序监听进来的TCP / IP信息的最佳方式? [英] What's the best way for an Android app to listen for incomming TCP/IP messages?

查看:301
本文介绍了什么是一个Android应用程序监听进来的TCP / IP信息的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个应用程序,将报警消息听时,它是在服务器范围内(在同一WiFi网络)。什么是做到这一点的最好方法是什么?

I want to write an app that will listen for alarm messages whenever it is in the range of a server (on the same WiFi network). What is the best way to do this?

我应该只是写一个服务,将作为一个TCP / IP服务器,当其收到的消息,让他们产生了Android的通知?还是有更好的方法来做到这一点?

Should I simply write a service that will act as a TCP/IP server and when it receives messages, have them generate an Android notification? or is there a better way to do this?

我需要这个程序,而Android运行的运行所有的时间。
我将如何确保我的'听众'的服务始终运行?

I need this app to be running all the time while Android is running. How would I ensure that my 'listener' service is always running?

在此先感谢..

Levster

推荐答案

我同样的问题...我想用彗星,但我还没有研究这件事多的时间。

I had same problem... I wanted to use comet but I havent much time to research about it..

您可以在这里看到一些样本..
http://blogs.webtide.com/dyu/entry/android_chat_using_jetty_cometd

you can see some sample here.. http://blogs.webtide.com/dyu/entry/android_chat_using_jetty_cometd

和我选用无限循环通讯

这是我的code ..

here is my code..

public class ServerActivity extends Activity {
private TextView serverStatus;

// default ip
public static String SERVERIP = "10.0.2.15";

// designate a port
public static final int SERVERPORT = 12345;

private Handler handler = new Handler();

private ServerSocket serverSocket;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.server);
    serverStatus = (TextView) findViewById(R.id.server_status);

   SERVERIP = getLocalIpAddress();

    Thread fst = new Thread(new ServerThread());
    fst.start();
}

public class ServerThread implements Runnable {
    String line = "";

    public void run() {

        try {
            if (SERVERIP != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                while (true) {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Connected.");
                        }
                    });

                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                 //       byte[] resultBuff = new byte[0];
                   //     byte[] buff = new byte[1024];
                     //   int k = -1;
                       // while((k = client.getInputStream().read(buff, 0, buff.length)) > -1) {
                         //   byte[] tbuff = new byte[resultBuff.length + k]; // temp buffer size = bytes already read + bytes last read
                           // System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length); // copy previous bytes
                            //System.arraycopy(buff, 0, tbuff, resultBuff.length, k);  // copy current lot
                            //resultBuff = tbuff; // call the temp buffer as your result buff
                        //}
                        //System.out.println(resultBuff.length + " bytes read.");




                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);


                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                serverStatus.setText(line);
                                }
                            });
                        }
                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                            }
                        });
                        e.printStackTrace();
                    }
                }
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (Exception e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    serverStatus.setText("Error");
                }
            });
            e.printStackTrace();
        }
    }
}

这篇关于什么是一个Android应用程序监听进来的TCP / IP信息的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆