【问题标题】:Connecting two device over Wi-fi using sockets使用套接字通过 Wi-fi 连接两个设备
【发布时间】:2011-05-22 11:13:56
【问题描述】:

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

这是服务器套接字的代码

 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;
        }
    }
}

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

【问题讨论】:

    标签: android multithreading sockets


    【解决方案1】:

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

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

    【讨论】:

    • 我使用了 handler。我不确定我是否正确使用了它。你能发布一些处理程序的示例代码,其中 UI therad 是从其他线程修改的。
    • 请在我的代码中提出更正建议或给出一些评论,以便我更好地理解我必须做什么。
    • @CommonsWare 两个安卓设备可以在没有任何网络的情况下使用套接字进行点对点交互吗?
    • @zIronManBox:如果他们支持 WifiDirect,是的。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2011-03-13
    • 2021-05-25
    • 2011-07-11
    • 2018-11-01
    • 1970-01-01
    相关资源
    最近更新 更多