使用插座通过Wi-Fi连接两个设备

时间:2011-05-22 11:13:57

标签: android multithreading sockets

我已经编写了服务器和客户端套接字的代码。 我使用了使用handler的线程。但是它经常给出异常只有创建视图层次结构的原始线程才能触及其视图 如何设法管理这个?

这是Server Socket的代码

 public class ServerThread implements Runnable {

    public void run() { 
        try {
            Looper.prepare();
            if (SERVERIP != null) {
                handler.post(new Runnable() {
                    public void run() {
                        serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                Toast.makeText(getApplicationContext(), serverSocket.getLocalSocketAddress().toString()
                , Toast.LENGTH_LONG).show();
                serverStatus.append("\n"+serverSocket.getLocalSocketAddress().toString());
                while (true) {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();

                            serverStatus.setText("Connected.");


                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                        String line = null;
                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                public void run() {

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

                }
            });
            e.printStackTrace();
        }
    }
}

客户端套接字代码

  public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr, 8080);
            connected = true;

                try {
                    console.append("\nC: Sending command.");
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                .getOutputStream())), true);
                        // where you issue the commands
                        out.println("Hey Server!");
                        console.append("C: Sent.");
                } catch (Exception e) {
                    console.append("S: Error= "+ e.getMessage());
                }

            socket.close();
            console.append("\nC: Closed.");
        } catch (Exception e) {
            Log.e("Client :", e.getMessage());

            //console.append("\nC: Error= ");
            connected = false;
        }
    }
}

这里控制台是文本视图,我试图放置一些消息。 我可以在日志中看到消息,但不能在文本视图中看到消息

1 个答案:

答案 0 :(得分:1)

  

只有创建视图层次结构的原始线程才能触及其视图如何设法管理它?

使用runOnUiThread()。或者,使用Handler。或者,使用post()