Goal
I am trying to send data over UDP from a Java server to a Python client on the same machine.
Environment
- Device/OS: Jetson nano with Ubuntu 18.04.3 LTS (Bionic Beaver)
- JavaC version: javac 11.0.4
- Java version:
- openjdk version "11.0.4" 2019-07-16
- OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
- OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode)
Problem
I can send data from a Python test server, see code below, to the Python test client just fine. However, If I try to send data from the Java test server to the Python test client, nothing seems to arrive. The Java server doesn't throw an exception.
Test client in Python (working)
import socket
UDP_IP = "localhost"
UDP_PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
print("listening...")
while True:
data, addr = sock.recvfrom(1024)
print("received message from: ", addr)
print("payload: ", data)
Test server in Python (working)
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto("Hello World", ("localhost", 9999))
Test server in Java (not working or doesn't reach the python client)
import java.io.*;
import java.net.*;
public class TestSender {
public static void main(String[] args) {
try {
byte[] data = "Hello world".getBytes();
int port = 9999;
InetAddress address = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
System.out.println("Data sent");
socket.close();
} catch (Exception e) {
System.out.println("Something went wrong");
}
}
}