简体   繁体   中英

Java UDP packet read failing

I am trying to send packets from one host to another peer host using java UDP protocol.

The one host sends data, while the other reads it. The corresponding read however keeps blocking, thus receiving no data. I can see the sender packets going to the right destination using wireshark, but the receiver just wont pick it up. The read operation keeps blocking indefinitely.

Please help. Code for cient:

//CLIENT CLASS
//Sections ommited....
DatagramSocket so = new DatagramSocket(port);
    protected void send(byte[] buffer,int packetsize){  
        DatagramPacket p;
        try {
            myClient.notifyInform("Sending data to "+inetaddress+" on"+port+"\n");
            p=new DatagramPacket(buffer,buffer.length,InetAddress.getByName(inetaddress),port);
            writeLock.lock();
            try{
                so.send(p);
            }finally{
                writeLock.unlock();
            }
        } catch (UnknownHostException e) {
            myClient.perror("Could not connect to peer:"+e.getMessage()+"\n");
            e.printStackTrace();
        } catch (IOException e) {
            myClient.perror("IOException while sending to peer.\n");
            e.printStackTrace();
        }
    }

    protected DatagramPacket read(){
        byte[] buf=new byte[bufsize];
        DatagramPacket p=new DatagramPacket(buf,buf.length);//TODO check these values, what should buffer be? just made it psize*10 for now
        readLock.lock();
        try{                
            myClient.notifyInform("receiving data\n");
            so.receive(p);
            this.myclient.notifyInform(String.valueOf(p.getData().length)+"\n");
        } catch (IOException e) {
            myClient.perror("IOException while reading from peer.\n");
            e.printStackTrace();
        }finally{
            readLock.unlock();
        }
        return p;
    }

    protected void beginRead() {
        while(active) {

            System.out.println("########################");
            byte[] data=this.read().getData();
            myClient.notifyInform("Receiving data\n");
        }

    }
    protected void beginSend(){
        forkWork(new Runnable(){

            @Override
            public void run() {

                byte[] sendBuffer=new byte[bufsize];
                int cnt;
                while(callActive){
                    try{
                        sourceLock.lock();
                        cnt=dataSource.read(sendBuffer, 0, bufsize);
                    }finally{
                        sourceLock.unlock();
                    }
                    if (cnt >0) {
                        send(sendBuffer, packetsize);
                    }
                }
            }

        });

    }

UPDATE:I made a mistake that I finally tracked down. After binding the port, and fixing that error, it now works.

You need to specify the port that the datagram socket is listening on like this:

 this.so = new DatagramSocket(SOME_NUMBER_HERE);

and make sure you send it to the same port number in the send() method

您的接收DatagramSocket是否正在侦听发送方发送到的IP:端口?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM