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.
Asked by: Catherine245 | Posted: 21-01-2022
Answer 1
Add -Djava.net.preferIPv4Stack=true to your Java application runtime properties.
Answered by: Catherine854 | Posted: 22-02-2022Answer 2
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.
Answered by: John857 | Posted: 22-02-2022Similar questions
linux - How to bind socket to IP with java?
I have a JAVA application which runs on Linux. I open a socket that binds to all IP. In production this is not acceptable.
$ netstat -ano | grep 104
tcp 0 0 0.0.0.0:104 0.0.0.0:* LISTEN off (0.00/0/0)
I which to do this:
$ netstat -ano | grep 104
tcp 0 viewer1.com:104 0.0.0.0:* ...
Java tcp socket server with j2me client socket
I want to create java server socket and j2me client socket which can communicate with each other.
Does anyone know how to do this?
java - read bits and not int from a socket
A third party programm allows me to ask data from his ip:port. I ask the stuff via this classic code. Here is the constructor of my connection class:
public TcpConnection(String adress, Integer port) {
this.adress = adress;
this.port = port;
try {
socket = new Socket(adress, port);
System.out.println("Opening connection");
out = new PrintWriter(socket.getOutputStream(), true)...
java socket server, client detect server has died
If I kill the Socket Server process, my Socket client process does not receive any errors, it continues to loop forever on the following code:
public void run() {
while(readData) {
String inputLine = null;
try {
while((inputLine = m_inputStream.readLine()) != null) {
//do stuff
}
} catch (IOException e) {
readData = false;
}
}
}
...
java socket / output stream writes : do they block?
If I am only WRITING to a socket on an output stream, will it ever block? Only reads can block, right? Someone told me writes can block but I only see a timeout feature for the read method of a socket - Socket.setSoTimeout().
It doesn't make sense to me that a write could block.
how to know a Java Socket is dead
I use a Java Socket object in my client application. I need to know when the line to the server is broken, or if any event caused the socket to be dead.
I see two methods:
catching SocketException when writing in or reading from the socket, considering these exceptions kill the socket
when catching these exceptions, checking the Socket.isClosed() method to know if it killed the socket
java socket issue
I am trying to make a simple TCP client and host chat program in java. They are both running in simple GUIs. After I start my server, it waits for a connection from the client then prints out a message in its JtextArea. The server does this succesfully, everything run fines until here, now I send some data to the client which it is supposed to print on its JtextArea, but the client is stuck from the time the connection was...
java - Pass error on socket
I am writing a general Client-Server socket program where the client sends commands to the Server, which executes it and sends the result to the Client.
However if there is an error while executing a command, I want to be able to inform the Client of an error. I know I could send the String "ERROR" or maybe something like -1 etc, but these could also be part of the command output. Is there any better...
java - Does Socket open another thread? Does it return something?
In the client application I call new Socket(serverIP,serverPort). As a result the client application sends a request to the server application to open a socket. Does it start a new thread? I mean which of the following is true?
Client application sends a request and immediately starts to execute following commands (not waiting for the answer).
Client sends the request and waits...
Java: Read POST data from a socket on an HTTP server
I have a website (python/django) that needs to use a load of Java resources that may or may not be on the same server. Therefore I am writing a mini webserver in Java that will receive a request and then when processing is finished, POST some data back to a url on the site.
I have got the java code receiving connections on sockets and responding with some simple HTML.
My problem is that I will POST data to ...
java - Can two different UDP socket in a system bind same port?
I have an application which uses UDP connection, now when i try to run the app more than once its throwing me an exception
java.net.BindException: Address already in use: Cannot bind
but in my another app, which uses tcp connection, i can open two instance of the same app and its working fine. why this error only with UDP connection?
Edit:
TCP socket:
Socket client...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)