有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android设备(galaxy note 2除外)无法获取UDP数据包

我正在开发一个客户机-服务器应用程序,其中服务器(用Java编写的桌面程序)通过UDP向同一无线网络中的安卓设备发送屏幕截图。当然,由于数据报的大小大于udp标准大小(65K),我正在创建它们的片段,并使用udp以循环方式发送到我的安卓设备

我的问题是,它在三星Galaxy Note 2(安卓4.2.2)上运行得很好,但在其他安卓设备上(甚至在Galaxy Note 1的前Galaxy tab 3上运行相同安卓版本的设备上)也不起作用。代码卡在socket上。接收(数据报),甚至不接收单个数据包

有人能帮我吗

public ClientListener(int port, int fps, AppDelegate del){
        delegate = del;
        framesPerSecond = fps;

        try{

            serverAddr = getLocalIpAddress();
            dgp = new DatagramPacket(buf, buf.length);

        }catch (Exception e){
            Log.e("ClientActivity", "C: Error", e);
        }
        serverPort = port;
    }



    public void run() {
           try {
               socket = new DatagramSocket(serverPort, serverAddr);
               connected = true;

               Timer timer = new Timer();
               int frames = 1000/framesPerSecond;

               Log.e("FRAMES", ""+frames);
               timer.scheduleAtFixedRate(getImageTask, 0, frames);

               listen();               
           }
           catch (Exception e) {
               Log.e("ClientActivity", "Client Connection Error", e);
           }
     }



private void listen()
    {

        while(connected){

            try{

                socket.receive(dgp); //gets stuck here on devices other than note 2




                byte[] data = dgp.getData();
                System.out.println("data received in datagram clientListener====="+data);
            /* Read header infomation */
            short session = (short) (data[1] & 0xff);
            short slices = (short) (data[2] & 0xff);
            int maxPacketSize = (int) ((data[3] & 0xff) << 8 | (data[4] & 0xff)); // mask

            short slice = (short) (data[5] & 0xff);
            int size = (int) ((data[6] & 0xff) << 8 | (data[7] & 0xff)); // mask



                System.out.println("------------- PACKET -------------");
                System.out.println("SESSION_START = "
                        + ((data[0] & SESSION_START) == SESSION_START));
                System.out.println("SSESSION_END = "
                        + ((data[0] & SESSION_END) == SESSION_END));
                System.out.println("SESSION NR = " + session);
                System.out.println("SLICES = " + slices);
                System.out.println("MAX PACKET SIZE = " + maxPacketSize);
                System.out.println("SLICE NR = " + slice);
                System.out.println("SIZE = " + size);
                System.out.println("------------- PACKET -------------\n");


            /* If SESSION_START falg is set, setup start values */
            if ((data[0] & SESSION_START) == SESSION_START) {
                if (session != currentSession) {
                    currentSession = session;
                    slicesStored = 0;
                    /* Consturct a appropreately sized byte array */
                    imageData = new byte[slices * maxPacketSize];
                    slicesCol = new int[slices];
                    sessionAvailable = true;
                }
            }

            /* If package belogs to current session */
            if (sessionAvailable && session == currentSession) {
                if (slicesCol != null && slicesCol[slice] == 0) {
                    slicesCol[slice] = 1;
                    System.arraycopy(data, HEADER_SIZE, imageData, slice
                            * maxPacketSize, size);
                    slicesStored++;
                }
            }

            /* If image is complete dispay it */
            if (slicesStored == slices) {
                ByteArrayInputStream bis = new ByteArrayInputStream(
                        imageData);
                 Bitmap bp=BitmapFactory.decodeStream(bis);
                         delegate.getController().setImage(bp);




                         Log.e("Testing", "Received image");






            }

共 (1) 个答案

  1. # 1 楼答案

    为我的愚蠢道歉。实际上,出于某种奇怪的原因,服务器无法同时满足来自多个android设备的多个连接。只有一个设备应连接到服务器或请求连接。在我的情况下,我已经通过Note2登录,同时正在尝试使用其他设备。所以我的另一台设备无法接收数据包