向不同网络android上的服务器发送和接收UDP数据包

时间:2017-04-05 07:53:51

标签: android sockets network-programming udp

我已经全神贯注地找到了解决这个问题的方法,我还没有想出一个有效的解决方案。

目前,我使用我的loopback / localHost地址在我的Android设备模拟器上发送和接收数据包。这个过程成功地运行,但是当我尝试使用我的计算机的公共地址运行相同的代码时,服务器永远不会收到数据包。

private String hostName = "99.248.222.229";
//private String hostName = "10.0.2.2";
private InetAddress hostAddress;
@Override
protected void onCreate(Bundle savedInstanceState)
{

    CreateAddress addressGetter = new CreateAddress();
    try {
        hostAddress = addressGetter.execute(hostName).get();
    } catch(Exception e){
        e.printStackTrace();
    }

Button btnLock = (Button) findViewById(R.id.btnLock);
    btnLock.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendReceiveTask = new runUdpClient();
            byte[] udpMsg = {(byte)housenumber, (byte)doornumber, LK_MSG, UNLOCK};
            System.out.println(hostAddress);
            sendPacket = new DatagramPacket(udpMsg, udpMsg.length, hostAddress, portnumber);
            DatagramPacket receivePacket = null;
            try {
                receivePacket = sendReceiveTask.execute(sendPacket).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }


            currentDoorState = (receivePacket.getData()[3]==UNLOCK);
            updateDoorStatus(currentDoorState);
            int doorNum = doornumber;
            eventArrayList.add(0, (getCurrentTimeStamp() + eventString.replace("doornum", Integer.toString(doorNum)))+ ((currentDoorState) ? "unlocked." : "locked."));
            adapter.notifyDataSetChanged();

        }
    });
}

private  class runUdpClient extends AsyncTask<DatagramPacket, Void, DatagramPacket>{

    @Override
    protected DatagramPacket doInBackground(DatagramPacket ...params){

        DatagramSocket ds = null;
        //SEND
        try {
            ds = new DatagramSocket();
            DatagramPacket dp;
            //dp = new DatagramPacket(udpMsg.getBytes(), udpMsg.length(), hostAddress, portnumber);
            dp = params[0];
            ds.send(dp);
        } catch (SocketException e) {
            e.printStackTrace();
        }catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //RECEIVE
        DatagramPacket incomingPacket = new DatagramPacket(new byte[100], 100);
        try {
            ds.setSoTimeout(1000);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        try {
            ds.receive(incomingPacket);
        } catch (IOException e){
            e.printStackTrace();
        }finally{
            if(ds != null) ds.close();
        }
        System.out.println("Packet recieved from server" + Arrays.toString(incomingPacket.getData()));
        return incomingPacket;
    }

}

0 个答案:

没有答案