Sheed
Sheed

Reputation: 619

java datagram socket not received (localhost)

This is a common issue, but I can't seem to make this work, it's not due to firewall, I made sure Intellij was authorized.

UDP SENDER:

public static void main(String[] args){
    Timer timer = new Timer();
    try {
        InetAddress ip = InetAddress.getLocalHost();
        int port = 9850;
        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer, 100, ip, port);

        try {
            DatagramSocket socket = new DatagramSocket(port, ip);
            timer.schedule(new TimerTask() {
                               @Override
                               public void run() {
                                   System.out.println("will send !");
                                   try {
                                       socket.send(packet);
                                   }catch (IOException e){
                                       e.printStackTrace();
                                       return;
                                   }
                                   System.out.println("was sent !");
                               }
                           },500, 500);
        } catch (SocketException e) {
            e.printStackTrace();
            return;
        }
    }catch (UnknownHostException e){
        e.printStackTrace();
        return;
    }
}

UDP RECEIVER

    public static void main(String[] args) {
    int port = 8888;
    byte[] buffer = new byte[100];
    DatagramPacket packet = new DatagramPacket(buffer, 100);

    try {
        DatagramSocket socket = new DatagramSocket(port);

        while(true) {
            try {
                System.out.println("ready to receive");
                socket.receive(packet);
                System.out.println("received a packet");
            }catch (IOException e){
                e.printStackTrace();
                return;
            }
        }
    }catch(SocketException e){
        e.printStackTrace();
        return;
    }
}

Packets are sent, the sender does display "will send/was sent" but the receiver doesn't receive anything, it's blocked and only displays "ready to receive"

ps: nevermind that sockets aren't closed...

Upvotes: 2

Views: 1417

Answers (2)

dan.m was user2321368
dan.m was user2321368

Reputation: 1705

Take a look carefully at the various calls to DatagramSocket and DatagramPacket that you are using, as you are misusing them.

In the Sender program, you are "construct[ing] a datagram packet for sending packets of length length to the specified port number on the specified host." You're then "creat[ing] a datagram socket, bound to the specified local address". As you're using the same port and InetAddr, you essentially are sending a packet to the same address you are listing to.

In the Receiver program, you "construct[] a datagram socket and bind[] it to the specified port on the local host machine." This time, you are binding it to a different port than the one you are sending it to. (8888 vs 9850 where you are sending the packet to).

For the Sender, trying creating a socket which is bound to a random port, by calling DatagramSocket(). For the receiver, change the socket so it is bound to the same numbered port that the sender is attempting to send it to (e.g. 9850)

Upvotes: 3

Anouar BAKRI
Anouar BAKRI

Reputation: 335

you don't need to use while(true) because the method DatagramPacket.receive will blocks until a datagram is received.

the cause of the problem may be the port that is different from one side to the other

Upvotes: 0

Related Questions