套接字不适用于wan

时间:2017-05-01 12:09:15

标签: android sockets

我正在使用套接字(TCP连接)在两个Android设备之间做一个简单的聊天应用程序问题是它只能在局域网上工作,但不能通过WAN工作如何解决这个问题? 客户机侧::

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;
    int q=0;


    @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() {

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


                            try {
                                DOS2 = new DataOutputStream(S.getOutputStream());
                                DOS2.writeUTF(et_message.getText().toString());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            tv_transmit.post(new Runnable() {
                                @Override
                                public void run() {

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



                                try {
                                    DIS = new DataInputStream(S.getInputStream());
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            while (DIS!=null) {
                                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

    }
    @Override
    protected void onStop() {
        super.onStop();
        try {
            if(S != null)
                S.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

服务器端::

public class MainActivity extends AppCompatActivity {
    Thread TH;
    String data;
    Socket S;
    ServerSocket ss;
    DataOutputStream DOS;
    String recieved="GPS";
    String Response;

    TextView et_port; TextView tv_receive; TextView tv_transmit; TextView status;

    @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() {
                status.setText(MyService.getIpAddress());
                try {
                    ss = new ServerSocket(8080);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while (true) {
                    try {
                        S = ss.accept();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    status.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        status.setText("connected");
                                    }
                                }
                    );

                    CommunicationThread commThread = null;

                    try {
                        commThread = new CommunicationThread(S);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    new Thread(commThread).start();
                }
            }


        });
        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)) {
                    sendMessage();
                    try {
                        DOS = new DataOutputStream(S.getOutputStream());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    try {
                        Thread.sleep(8000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                                    if (Response !=null){
                                        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();
                                }
                            }
                try {
                    S.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
            }
        }




    public void sendMessage() {
        Intent i = new Intent(this,Gps.class);
        startActivityForResult(i, 1);
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if(resultCode == RESULT_OK) {
               // Bundle bundle = getIntent().getExtras();
                if (data!=null) {
                    String x = data.getStringExtra("Latitude");
                    String y = data.getStringExtra("Longitude");
                    Response = x + "\n" + y;
                }
            }

            }
        }

}

这里客户端假设在服务器收到它时向服务器发送一个字,它将打开另一个意图并将一些数据“GPS”返还给客户端

0 个答案:

没有答案