Can't send UDP packets to co developer [closed]

4 weeks ago 23
ARTICLE AD BOX

I'm working on a peer-to-peer messaging system. Me and my co developer are testing sending messages to each other while using the same WiFi router. The system works when sending messages to localhost but not to each other's IPs.

Our test:

public class SocketTest { private static Config conf; public static void main(String[] args) throws FileNotFoundException { conf = new Config(); try { conf.readConfig(); } catch (FileNotFoundException e) { System.out.println(SocketTest.class.getProtectionDomain().getCodeSource().getLocation()); throw new RuntimeException(e); } Receiver receiver = new Receiver(conf); Sender sender = new Sender(conf); receiver.setOpen(true); receiver.startServer(); InetAddress toSend; Scanner input = new Scanner(System.in); try { //add peer's IP from running ip addr. Google DNS server is an example IP toSend = InetAddress.getByName("8.8.8.8"); } catch (UnknownHostException e) { throw new RuntimeException(e); } while (input.hasNextLine()) { System.out.print(">"); sender.sendMessage(input.nextLine(), toSend); } receiver.stopServer(); } }

Receiver:

public class Receiver { //Whether or not to listen on the port private boolean open = false; private Config conf; private Thread serverThread; public Receiver(Config conf) { this.conf = conf; } /** * Reads the configuration and creates a thread to manage message listening. * <p> * Thread can only be stopped or updated using the appropriate provided methods. * * @return message listener thread * @throws FileNotFoundException when config file cannot be found * @see #startServer() * @see #stopServer() */ private Thread serverFactory() throws FileNotFoundException { //For loading config changes while the program is executing, kill the current server/thread and start a new one conf.readConfig(); return new Thread(() -> { DatagramSocket socket; try { socket = new DatagramSocket(conf.getPort()); } catch (SocketException e) { Logging.log("Failed To Initialize Server Thread", Level.SEVERE, e); throw new RuntimeException(e); } String messageHolder; while (!serverThread.isInterrupted()) { try { byte[] buffer = new byte[conf.getMaxBytesPerMessage()]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); messageHolder = new String(packet.getData(), 0, packet.getLength()); System.out.println(messageHolder); } catch (IOException e) { Logging.log("Failed To Receive Packets", Level.SEVERE, e); throw new RuntimeException(e); } } socket.close(); }); }

Message Sender:

public boolean sendMessage(String user_message, InetAddress IP) { byte[] message = user_message.getBytes(); DatagramSocket outSocket; try { outSocket = new DatagramSocket(); outSocket.setBroadcast(true); } catch (SocketException e) { Logging.log("Failed To Set Up Socket", Level.SEVERE, e); return false; } DatagramPacket packet = new DatagramPacket( message, message.length, IP, conf.getPort() ); try { outSocket.send(packet); } catch (IOException e) { Logging.log("Failed To Send Message", Level.SEVERE, e); return false; } return true; }

We've also allowed both TCP and UDP connections on the port we're using (9090) My ufw firewall:

Status: active To Action From -- ------ ---- 53317/udp ALLOW Anywhere 53317/tcp ALLOW Anywhere 22/tcp ALLOW Anywhere 172.17.0.1 53/udp ALLOW 172.16.0.0/12 # allow-docker-dns 25565 ALLOW Anywhere 9090/udp ALLOW Anywhere 9090 ALLOW Anywhere 53317/udp (v6) ALLOW Anywhere (v6) 53317/tcp (v6) ALLOW Anywhere (v6) 22/tcp (v6) ALLOW Anywhere (v6) 25565 (v6) ALLOW Anywhere (v6) 9090/udp (v6) ALLOW Anywhere (v6) 9090 (v6) ALLOW Anywhere (v6)

His iptable

$ cat /etc/iptables/iptables.rules # Generated by iptables-save v1.8.11 on Thu Feb 12 13:40:20 2026 *filter :INPUT ACCEPT [994:510857] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1568029:240475572] -A INPUT -p udp -m udp --dport 9090 -j ACCEPT -A INPUT -p tcp -m tcp --dport 9090 -j ACCEPT COMMIT # Completed on Thu Feb 12 13:40:20 2026
Read Entire Article