Android TCP / IP连接

时间:2013-11-25 08:25:40

标签: android tcp-ip

我有两个Android设备通过TCP / IP进行消息传递的问题。问题是我无法向第二个Android设备发送任何消息。当我输入第一台设备的本地IP时,我可以向自己发送消息。这是我的服务器和客户端类:

服务器类:

公共类ServerThread实现Runnable {

        public void run() {
            try {
                if (SERVERIP != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //serverStatus.setText("Listening on IP: " + SERVERIP);
                        }
                    });
                    serverSocket = new ServerSocket(8080);
                    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()));

                         String line = null;
                          while ((line = in.readLine()) != null) {

                                final String a=line.toString();
                                Log.d("ServerActivity", line);
                                handler.post(new Runnable() {
                                  @Override
                                  public void run() {


                                        thelist.add(a);
                                        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.chat_view, R.id.chat_view_label, thelist);
                                        lvMessage.setAdapter(arrayAdapter);
                                        //onStop();                
                                        try {
                                            serverSocket.close();
                                        } catch (IOException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }


                                    }

                                });

                            }

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



        }
    }

客户端类:

公共类ClientThread实现Runnable {

        public void run() {

            try {


                InetAddress serverAddr = InetAddress.getByName("141.196.41.145");


                Log.d("ClientActivity", "C: Connecting...");
                Socket socket = new Socket(serverAddr,8080);

                //Socket socket = new Socket("141.196.41.145",8080);


               connected=true;

                while (connected) {

                    try {
                      Log.d("ClientActivity", "C: Sending command.");
                       PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                            // where you issue the commands
                            out.println(" MT 02 : " + thevariable);
                            Log.d("ClientActivity", "C: Sent.");
                            break;

                    } catch (Exception e) {
                        Log.e("ClientActivity", "S: Error", e);
                    }


                    }
                socket.close();
                Log.d("ClientActivity", "C: Closed.");
            } catch (Exception e) {
                Log.e("ClientActivity", "C: Error", e);
                connected=false;
            }
        }
    }

“141.196.41.145”是我的第二台设备的本地IP。如果我将此IP更改为我的第一个设备的本地IP,我可以向自己发送消息。

感谢您的关注和帮助

1 个答案:

答案 0 :(得分:1)

你在哪里使用socket.connect();

            InetAddress serverAddr = InetAddress.getByName("141.196.41.145");


            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr,8080);

创建套接字不会产生连接