Android UDP Communication

后端 未结 3 1027
温柔的废话
温柔的废话 2020-12-16 04:54

I\'ve read many posts on this site on how to receive UDP packets in Android. However, none of this is working for me!

Some basics:

I am testing on my HTC Inc

3条回答
  •  别那么骄傲
    2020-12-16 05:16

    Sorry for not updating this sooner. The problem was fixed as follows:

    I needed to store the DatagramSocket to each thread. The listening socket should also be the socket used to continue communication between the server and client. Here are the bits of updated code.

    New socket registration code on thread:

    public void setCommSocket(DatagramPacket pkt, int port, DatagramSocket skt)
    {
      comm_ip = pkt.getAddress();
      comm_port = pkt.getPort();
      synchronized(comm_pkt) {
        comm_pkt = pkt;
      }
      comm_skt = skt;
    }
    

    New Server Listening Code:

    public void UDPListen() {
            while(true) {
                synchronized(stop) {
                    if(stop)
                        break;
                }
    
                byte[] recieve_data = new byte[64];
                DatagramPacket rpkt = new DatagramPacket(recieve_data, recieve_data.length);
                try {
                    datagram_server_socket.receive(rpkt);
                    int port = 0;
                    byte[] rdata = rpkt.getData();
                    port += rdata[0]<<24;
                    port += rdata[1]<<16;
                    port += rdata[2]<<8;
                    port += (0XFF)&rdata[3];
                    byte[] tid = new byte[rdata.length];
                    for(int i = 4; i < rdata.length && rdata[i] > 0; i++)
                    {
                        tid[i-4] = rdata[i];
                    }
                    String thread_id = new String(tid).trim();
                    for(int i = 0; i < threads.size(); i++) {
                        ClientThread t = threads.get(i);
                        if(t.getThreadId().compareTo(thread_id) == 0)
                        {
                            t.setCommSocket(rpkt, port, datagram_server_socket);
                        } else {
                            System.err.println("THREAD ID " + thread_id + " COULD NOT BE FOUND");
                        }
                    }
                } catch (IOException e) {
                    if(!(e instanceof SocketException) && !(e instanceof SocketTimeoutException))
                        log.warning("Error while listening for an UDP Packet.");
                } finally {
                    for(int i = 0; i < threads.size(); i++) {
                        ClientThread t = threads.get(i);
                        t.sendKeepAlive();
                    }
                }
            }
        }
    

    There was some update to the structure of the server/threads that I will omitt. The important part here is that the socket in which the packet was recieved with was re-used to send data back to the client. Additionally, the actual packet was re-used to send data back:

    public void sendIdle() {
            if(comm_ip != null) {
                synchronized(comm_pkt) {
                    try {
                        comm_pkt.setData(new byte[]{1, ProtocolWrapper.IDLE});
                        comm_skt.send(comm_pkt);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    

    Here is the relevant parts of my wrapper class that shows what each thread was holding:

    public class PeerWrapper {
    
        private InetAddress ipaddress;
        private Integer port;
        private Socket client_socket;
        private InetAddress comm_ip;
        private DatagramSocket comm_skt;
        private DatagramPacket comm_pkt;
        private int comm_port;
        private byte status;
    

提交回复
热议问题