【问题标题】:Java UDP packet read failingJava UDP 数据包读取失败
【发布时间】:2011-09-26 08:41:07
【问题描述】:

我正在尝试使用 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);
                    }
                }
            }

        });

    }

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

【问题讨论】:

  • 防火墙能阻止它吗?你使用什么特别的东西,比如多播地址?
  • 不,我关闭了所有防火墙,并且没有使用多播。
  • 两种简单的可能性:其他东西已经绑定了该端口,或者您在 Unix/Linux 平台上并尝试绑定小于 1024 的端口号而不是 root。

标签: java udp


【解决方案1】:

您需要像这样指定数据报套接字正在侦听的端口:

 this.so = new DatagramSocket(SOME_NUMBER_HERE);

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • DatagramSocket 没有指定。它只读取发送给它的内容?这是我的套接字初始化代码:this.so = new DatagramSocket();
    • @Vort3x,如果它不监听任何 IP 和端口,它将不会听到任何声音。
    • @Vort3x 相反。 Javadoc 明确指出new DatagramSocket() '将其绑定到任何可用端口'。因此,除非您恰好恰好发送到该端口,否则它将不会接收这些传输。这就是绑定和端口号的含义。所以不要那样做。试试new DatagramSocket(port);其中port 与您发送的地址相同。
    • @EJP 谢谢,我误读了文档。我现在已经绑定了端口,它可以在 localhost 上运行。但它不能跨网络工作,给我一个 java.net.BindException。
    • @Vort3x 误读或未读?你太教条了。无论如何 BindException 与“跨网络”无关,它只是意味着其他东西已经在那个端口上监听了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多