【问题标题】:Error while connection with local ip address in android application在android应用程序中连接本地IP地址时出错
【发布时间】:2014-01-22 04:39:37
【问题描述】:

我正在制作一个需要与本地 IP 地址建立连接的 Android 应用程序 我搜索了很多并找到了我尝试在我的代码中实现的相同示例代码

http://android-er.blogspot.in/2011/01/simple-communication-using.html

http://www.pixelstech.net/article/1368328614-Android-socket-programming-example

但问题是我无法连接到没有10.19.21.128 的 IP 地址 和端口号8221

我找不到我失踪的地方。如果有人以前做过这个,或者有想法或任何示例,Android 应用程序和 IP 地址之间存在通信,那么我将感谢他。

注意我只需要发送一个测试消息 hi 到 IP 地址。

谢谢 图沙尔

【问题讨论】:

  • 在你的安卓设备上安装connectBot并尝试使用它连接到指定的IP和端口。结果可能会提供提示。
  • 你的意思是说如果我在模拟器上检查它将无法工作。目前我正在 Android 模拟器上进行测试。另外,如果你有相同的示例代码或 URL 实现了相同的东西,请让我知道。谢谢
  • @mani,如果可能的话,给出完整的代码。这是不完整的一个哥们。

标签: android networking ip-address


【解决方案1】:

客户

class ClientThread implements Runnable {

    @Override
    public void run() { 
        try {
            socket = new Socket();
            InetSocketAddress socketAddress = new InetSocketAddress(SERVER_IP, SERVERPORT);
            socket.connect(socketAddress);


            CommunicationThread commThread = new CommunicationThread(socket);
            new Thread(commThread).start();

        } catch (UnknownHostException e1) {

            e1.printStackTrace();
        }catch (IOException e1) {
            e1.printStackTrace();
        }
    }       
}
class CommunicationThread implements Runnable {

    private Socket clientSocket;
    private BufferedReader input;

    public CommunicationThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
        try {
            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void run() {

      while (!Thread.currentThread().isInterrupted()) {


          try {
              final String read1 = input.readLine();
              updateConversationHandler.post(new updateUIThread(read1));                                                                                                    


                      } catch (IOException e) {
                            e.printStackTrace();

                        }
      }
    }
}

class updateUIThread implements Runnable {
    private String msg;

    public updateUIThread(String str) {
        this.msg = str;
    }
    @SuppressLint("SdCardPath")
    @Override
    public void run() {
        text1.setText(text1.getText().toString()+"Server Says: "+ msg + "\n");

        }   

}

服务器

class ServerThread implements Runnable {

    public void run() {
        socket = null;
        try {
            serverSocket = new ServerSocket(SERVERPORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (!Thread.currentThread().isInterrupted()) {
            try {
                socket = serverSocket.accept();

                CommunicationThread commThread = new CommunicationThread(socket);
                new Thread(commThread).start();


                }catch (IOException ioException) {
                    ioException.printStackTrace();                  

                }
        }
    }
}

class CommunicationThread implements Runnable {


    private Socket clientSocket;

    private BufferedReader input;

    public CommunicationThread(Socket clientSocket) {

        this.clientSocket = clientSocket;

        try {
            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));                          

        } catch (IOException e) {
            e.printStackTrace();
            new Thread(new ServerThread()).start();
        }
    }

    public void run() {


        while (!Thread.currentThread().isInterrupted()) {

            try {

                final String read1 = input.readLine();

                    updateConversationHandler.post(new updateUIThread(read1));                                      

            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }

}

class updateUIThread implements Runnable {
    private String msg;

    public updateUIThread(String str) {
        this.msg = str;
    }

    @Override
    public void run() {
            text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");        
    }
}   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2015-01-22
    • 2013-05-23
    • 2014-01-22
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多