Java UDP数据包读取失败

发布于 2024-12-06 15:08:25 字数 2590 浏览 1 评论 0原文

我正在尝试使用 java UDP 协议将数据包从一台主机发送到另一台对等主机。

一台主机发送数据,而另一台主机读取数据。然而,相应的读取保持阻塞,因此没有接收到数据。我可以使用wireshark看到发送方数据包发送到正确的目的地,但接收方就是不会接收它。读操作无限期地保持阻塞。

请帮忙。 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);
                    }
                }
            }

        });

    }

更新:我犯了一个错误,我终于找到了。绑定端口并修复该错误后,它现在可以工作了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木落 2024-12-13 15:08:25

您需要指定数据报套接字正在侦听的端口,如下所示:

 this.so = new DatagramSocket(SOME_NUMBER_HERE);

并确保在 send() 方法中将其发送到相同的端口号

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

初雪 2024-12-13 15:08:25

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

Is your receiving DatagramSocket listening at the IP:port the sender is sending to?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文