Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA UDP Server Can't receive Packet

Tags:

java

udp

I have a sample code as below and the socket is bound to IP 10.10.88.11 and port 9876. I tested with the 2 conditions with wireshark as below. Both PCs are in the same subnet.

  1. Send UDP packet from the same pc (10.10.88.11) - UDP Server able to receive
  2. Send UDP packet from another pc ( 10.10.88.10) - UDP Server unable to receive but Wireshark (at 10.10.88.11) able to capture the packets

I have searched the internet but can't find a solution for this. Is there anything i did wrong in creating the InetScoketAddress?

import java.io.*;
import java.net.*;

public class UDPServer {
    public static void main(String args[]) throws Exception {        

     InetSocketAddress address = new InetSocketAddress("10.10.88.11", 9876);

     DatagramSocket serverSocket = new DatagramSocket(address);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];
        while(true)
           {
              DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
              System.out.println("Waiting to receive");
              serverSocket.receive(receivePacket);
              String sentence = new String( receivePacket.getData());
              System.out.println("RECEIVED: " + sentence);
              InetAddress IPAddress = receivePacket.getAddress();
              int port = receivePacket.getPort();
              String capitalizedSentence = sentence.toUpperCase();
              sendData = capitalizedSentence.getBytes();
              DatagramPacket sendPacket =
              new DatagramPacket(sendData, sendData.length, IPAddress, port);
              serverSocket.send(sendPacket);
           }

  }
}
like image 620
Loon Yew Avatar asked Apr 30 '26 19:04

Loon Yew


1 Answers

I believe Wireshark is able to grab packets before they are evaluated by the firewall, meaning that you will detect them but they will never reach the java app. Did you try deactivating your firewall ?

like image 96
An intern has no name Avatar answered May 03 '26 09:05

An intern has no name