1. 程式人生 > >使用Java進行udp-demo程式設計時碰到的consumer和producter無法連線並報出“java.net.SocketException: Can't assign requested address”問題

使用Java進行udp-demo程式設計時碰到的consumer和producter無法連線並報出“java.net.SocketException: Can't assign requested address”問題

在用Java編寫了一個udp生產者和消費者的demo時,在生產者啟動的時候會丟擲異常

java.net.SocketException: Can't assign requested address
    at java.net.PlainDatagramSocketImpl.join(Native Method)
    at java.net.AbstractPlainDatagramSocketImpl.join(AbstractPlainDatagramSocketImpl.java:178)
    at java.net.MulticastSocket.joinGroup(MulticastSocket.java:
323) at com.yanwu.demo.java.socket.udp.Producer.<init>(Producer.java:41) at com.yanwu.demo.java.socket.udp.Producer.main(Producer.java:71)

解決方案:

在jvm的啟動引數中新增-Djava.net.preferIPv4Stack=true.