Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java 6, on linux how can I make sure to use an ipv4 socket?

I'm trying to write a quick little java application to read the contents of a pcap file (from Wireshark) and play the data back on the network on a linux box. The file will only contain UDP broadcast packets, so my application only really needs the timestamp, port number, and data from the packet to do what I need. My problem is that the application that I'm trying to test with this code is listening for IPv4 UDP broadcasts on Windows. My test application keeps opening an IPv6 socket to send the data out.

I'm using netstat -a -u -p to determine that the socket is a udp6 socket. On windows I know it isn't.

What is the easiest or best way to force the test app on linux in java to open a udp or udp4 socket instead? I don't want to be forced in to providing the ipv4 network address each time. I want to be able to move this code to another machine without having to remember that their is some configuration that needs to be changed.

like image 248
Jay R. Avatar asked Nov 14 '08 16:11

Jay R.


2 Answers

Add -Djava.net.preferIPv4Stack=true to your Java application runtime properties.

like image 122
user30682 Avatar answered Nov 15 '22 18:11

user30682


Also you can use this command (as root) and you will not need to add the -Djava.net.preferIPv4Stack=true parameter in every java application:

 # echo 0 > /proc/sys/net/ipv6/bindv6only

If you want to keep this configuration forever you can write it in some initial script.

like image 38
Urizev Avatar answered Nov 15 '22 17:11

Urizev