UDP打孔:PC到Android的UDP连接不适用于非本地地址

时间:2014-07-29 18:29:14

标签: android python sockets udp datagram

我在Android App和Python Server之间编写了一个简单的UDP传输。我知道系统正在运行,因为当我尝试连接本地IP地址(192.168.X.X)时,会发送和接收正确的消息。但是,当我尝试使用公共IP地址时,这不起作用。有谁知道我为什么以及如何解决这个问题?

我正在尝试实现UDP打孔,让服务器充当Android的目标客户端,但我无法将第二个客户端的UDP数据包发送到Android,它永远不会在Android方面被接收。让第二台机器作为第二台客户端修复此问题,还是我的代码不完整?

我的提供商(T-Mobile)对UDP数据包通信很重要吗?

客户端(Android):

public class CustomizeGatewayActivity extends ActionBarActivity {

    AsyncUDPReceiver aReceive = null;
    static TextView recieve = null;

    public static class PlaceholderFragment extends Fragment {
        EditText addressText, portText, messageText;
        Button udpsend, tcpsend;
        Socket socket = null;

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(
                    R.layout.fragment_customize_gateway, container, false);

            recieve = (TextView) rootView.findViewById(R.id.textView1);
            addressText = (EditText) rootView.findViewById(R.id.editText1);
            messageText = (EditText) rootView.findViewById(R.id.editText3);

            udpsend = (Button) rootView.findViewById(R.id.UDP);
            udpsend.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    AsyncUDPSend aSend = new AsyncUDPSend(addressText.getText().toString(), messageText.getText().toString());
                    aSend.execute();
                }
            });

        public class AsyncUDPSend extends AsyncTask<Void, Void, Void> {
            String address = "";
            String message = "";
            String response = "";
            AsyncUDPSend(String addr, String mes) {
                address = addr;
                message = mes;
            }

            @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            DatagramSocket dsocket = null;
            try {
                dsocket = new DatagramSocket();
                dsocket.setSoTimeout(10000);
                InetAddress dest = InetAddress.getByName(address);
                DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), dest, 5001);
                dsocket.send(packet);
                System.out.println("Sent");

                byte[] resp = new byte[1024];
                DatagramPacket recv = new DatagramPacket(resp, resp.length);
                System.out.println("Waitng for Response");
                dsocket.receive(recv);
                System.out.println("Received");
                response = new String(recv.getData());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    response = "IOException: " + e.toString();
                    System.out.println(response);
                } finally {
                    if (dsocket != null) {
                        dsocket.close();
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                recieve.setText(response);
                super.onPostExecute(result);
            }
        }

    @Override
    protected void onResume() {
        super.onResume();
        aReceive = new AsyncUDPReceiver();
        aReceive.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        aReceive.kill();
    }

    public class AsyncUDPReceiver extends Thread {
        boolean keepRunning = true;
        String response = "";

        Runnable updateText = new Runnable(){
            public void run() {
                if(aReceive == null && recieve == null)
                    return;
                recieve.setText(response);
            }
        };

        public void run() {
            android.os.Debug.waitForDebugger();
            System.out.println("running");
            DatagramSocket dsock = null;
            byte[] message = new byte[1024];
            DatagramPacket dpack = new DatagramPacket(message, message.length);
            try {
                dsock = new DatagramSocket(5002);
                System.out.println(dsock.toString());
                while(keepRunning) {
                    dsock.receive(dpack);
                    response = new String(dpack.getData());
                    System.out.println(response);
                    runOnUiThread(updateText);
                }
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                response = "SocketException: " + e.toString();
                System.out.println(response);
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                response = "IOException: " + e.toString();
                System.out.println(response);
                e.printStackTrace();
            } finally {
                if(dsock != null)
                    dsock.close();
            }
        }

        public void kill() {
            keepRunning = false;
        }
    }
}

服务器(Python):

class ThreadedUDPRequestHandler(socketserver.BaseRequestHandler):
    def handle(self):
        data = self.request[0].strip().decode("utf-8")
        print("{} Recieved: ".format(self.client_address) + data)
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        response = data.upper()
        sock.sendto(bytes(response, "utf-8"), self.client_address)
        print("{} Sent: {}".format(self.client_address,response))

class ThreadedUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer): 
    pass

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "", 5000

    udpserver = ThreadedUDPServer((HOST,PORT+1), ThreadedUDPRequestHandler)
    udp_thread = threading.Thread(target=udpserver.serve_forever)
    udp_thread.daemon = True
    udp_thread.start()
    print("UDP serving at port", PORT+1)

    while True:
        pass
    udpserver.shutdown()

1 个答案:

答案 0 :(得分:0)

您是否向InetAddress.getByName(地址)提供了预期值;

此外,由于您尝试在后台执行某些操作,因此如果您使用唤醒锁定运行服务以便消除由于终止进程而导致的错误,那将会更好。