套接字:客户端不会通过WAN与服务器连接

时间:2017-05-02 22:05:59

标签: java android sockets

我有两个移动设备之间使用java套接字的聊天程序,当我尝试使用相同的网络(LAN)将客户端连接到服务器时,它工作正常,我能够从客户端发送到服务器,反之亦然 但是当我试图在(WAN)连接它们时它没有用 服务器端::

public class MainActivity extends AppCompatActivity {
    Thread TH;
    ServerSocket ss;
    Socket S;
    DataOutputStream DOS;
    String data;
    String recieved = "GPS";
    String Response="sondos";
    TextView et_port;
    TextView tv_receive;
    TextView tv_transmit;
    TextView status;
    boolean isbind = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_port = (TextView) findViewById(R.id.et_port);
        tv_receive = (TextView) findViewById(R.id.tv_receive);
        tv_transmit = (TextView) findViewById(R.id.tv_transmit);
        status = (TextView) findViewById(R.id.status);

        Intent intent =new Intent(this,MyService.class);
        startService(intent);
      establishconnection();
    }
   public void establishconnection()
 {
    TH=new

    Thread(new Runnable() {
        @Override
        public void run () {

            try {
                status.setText(MyService.getipaddress());
                ss = new ServerSocket(8080);
                while (true) {
                    S = ss.accept();
                    status.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        status.setText("connected");
                                    }
                                }
                    );
                    CommunicationThread commThread = new CommunicationThread(S);
                    new Thread(commThread).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    );
    TH.start();
}
    class CommunicationThread implements Runnable {
        private Socket ClientSocket;
        DataInputStream DIS;
        public CommunicationThread(Socket ClientSocket) throws IOException {
            this.ClientSocket = ClientSocket;
            DIS = new DataInputStream(ClientSocket.getInputStream());
            data = DIS.readUTF();
        }
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                data = data.toString();
                tv_receive.post(new Runnable() {

                    public void run() {
                        tv_receive.setText(data);
                    }

                });
                while (recieved.equals(data)) {
                    try {
                        DOS = new DataOutputStream(S.getOutputStream());
                        DOS.writeUTF(Response);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    tv_transmit.post(new Runnable() {
                        @Override
                        public void run() {
                            tv_transmit.setText(Response);
                        }
                    });
                    try {
                        DOS.flush();
                        DOS.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            S.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
////    public static String getIpAddress() {
////        String ip = "";
////        try {
////            Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
////                    .getNetworkInterfaces();
////            while (enumNetworkInterfaces.hasMoreElements()) {
////                NetworkInterface networkInterface = enumNetworkInterfaces
////                        .nextElement();
////                Enumeration<InetAddress> enumInetAddress = networkInterface
////                        .getInetAddresses();
////                while (enumInetAddress.hasMoreElements()) {
////                    InetAddress inetAddress = enumInetAddress.nextElement();
////
////                    if (inetAddress.isSiteLocalAddress()) {
////                        ip += "Hosted On: "
////                                + inetAddress.getHostAddress() + "\n";
////                    }
////                }
////            }
//        } catch (SocketException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return ip;
//    }

客户端::

public class MainActivity extends AppCompatActivity {

    TextView  tv_device, tv_receive, tv_transmit;
    Button btn_connect, btn_GPS;
    EditText et_IP_Address, et_port, et_message;
    Socket S;
    DataOutputStream DOS;
    DataOutputStream DOS2;
    DataInputStream DIS;
    Thread TH;
    Thread TH2;
    String data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_IP_Address= (EditText) findViewById(R.id.et_IP_Address);
        et_port= (EditText) findViewById(R.id.et_port);
        et_message= (EditText) findViewById(R.id.et_message);
        et_message.setText("GPS");
        btn_connect = (Button) findViewById(R.id.btn_connect);
        btn_GPS= (Button) findViewById(R.id.btn_send);
        tv_transmit=(TextView) findViewById(R.id.tv_transmit);

        tv_receive = (TextView) findViewById(R.id.tv_receive);
        tv_device = (TextView) findViewById(R.id.tv_device);

        // connection
        btn_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TH=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            S = new Socket(et_IP_Address.getText().toString(),Integer.parseInt(et_port.getText().toString()));
                            DOS = new DataOutputStream(S.getOutputStream());

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


                    }
                });// End of thread
                TH.start();

            }
        }); // end of  buutton

        // Sending
        btn_GPS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TH2=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            while(!Thread.currentThread().isInterrupted()) {
                                DIS = new DataInputStream(S.getInputStream());
                                DOS2 = new DataOutputStream(S.getOutputStream());
                                DOS2.writeUTF(et_message.getText().toString());
                                tv_transmit.post(new Runnable() {
                                    @Override
                                    public void run() {

                                        tv_transmit.setText(et_message.getText());
                                    }
                                });
                                try {
                                    DOS2.flush();
                                }
                                catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }
                        }
                        catch (IOException e) {
                            e.printStackTrace();
                        }
//                        CommunicationThread commThread = null;
//                        try {
//                            commThread = new CommunicationThread(S);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                        new Thread(commThread).start();
//
                        try {
                            data = DIS.readUTF();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        data = data.toString();
                        tv_receive.post(new Runnable() {

                            public void run() {
                                tv_receive.setText(data);
                            }

                        });
                            try {
                                DOS2.close();
                                DIS.close();

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

                });  // end of thread

                TH2.start();


            }
        });    // end of button

    }
//    class CommunicationThread implements Runnable{
//        private Socket ClientSocket;
//        DataInputStream DIS;
//        public CommunicationThread(Socket ClientSocket) throws IOException {
//            this.ClientSocket = ClientSocket;
//        }
//        @Override
//        public void run() {
//            while (!Thread.currentThread().isInterrupted()){
//                while(true){
//                    try {
//                    DIS = new DataInputStream(ClientSocket.getInputStream());
//                    data = DIS.readUTF();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                data = data.toString();
//                tv_receive.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        tv_receive.setText(data);
//                    }
//                });
//
//                }
//
//            }
//        }
//    }
    @Override
    protected void onStop() {
        super.onStop();
        try {
            S.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我正在使用服务来保持连接打开

0 个答案:

没有答案