Udp没有从服务器接收数据(android客户端,pc java服务器)

时间:2016-02-04 03:02:07

标签: android server udp client

我正在使用UDP制作Android的聊天应用程序。

在lan设备上一切正常,我可以从客户端和服务器发送和接收消息。

当我用我的android连接到其他网络时,我可以发送消息,服务器接收它们但是从未在android上收到响应。

客户接收代码

 Runnable periodicTask = new Runnable() {
    public void run() {
        // Invoke method(s) to do the work
        System.out.println("Waiting for data");
        byte[] receiveData = new byte[2028];
        byte[] sendData = new byte[2028];

        boolean portTaken=false;
        DatagramSocket serverSocket=null;
        ServerSocket socket = null;
        try {
            socket = new ServerSocket(6667);

        } catch (IOException e) {
            System.out.println("ERRORRRRRRRRRRRRRR");
        } finally {
            if (socket != null)
                try {
                    socket.close();
                    serverSocket= new DatagramSocket(6667);
                } catch (IOException e) {   e.printStackTrace();   }
        }

        DatagramPacket receivePacket = new     DatagramPacket(receiveData,receiveData.length);

        try {
            serverSocket.receive(receivePacket);
            System.out.println("GOT THEM!");
        } catch (IOException e) {

            e.printStackTrace();
        }
        ByteArrayInputStream in = new ByteArrayInputStream(receiveData);

        ObjectInputStream is = null;
        try {
            is = new ObjectInputStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Data student = null;
        try {
            student = (Data) is.readObject();
            l.add(student.toString());

            mHandler.sendEmptyMessage(0);
            System.out.println(student);
            serverSocket.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

};

服务器发送代码

for (DatagramPacket packet:clients){
            if (!packet.getAddress().equals(receivePacket.getAddress())){
                System.out.println("current ip address " + receivePacket.getAddress());
                System.out.println("Sending to " + packet.getAddress());
                DatagramSocket s = new DatagramSocket();
                InetAddress local =packet.getAddress();//editTextAddress.getText().toString());//InetAddress.getByName("192.168.1.74");
                System.out.println(local.toString());
                //int msg_length =student.//sentence.length();



                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                ObjectOutputStream os = new ObjectOutputStream(outputStream);

                os.writeObject(student);

                byte[] data2 = outputStream.toByteArray();
                int msg_length =data2.length;
                DatagramPacket p = new DatagramPacket(data2, msg_length, local,
                        6667);
                s.send(p);
                s.close();

            }

可能是这种情况?

感谢您的时间

0 个答案:

没有答案