Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:44:48+00:00 2026-06-13T01:44:48+00:00

I have 3 nodes A, B and C with their respective port numbers. I’m

  • 0

https://i.stack.imgur.com/97ERA.png

  1. I have 3 nodes A, B and C with their respective port numbers.
  2. I’m trying to write a java program that takes in 3 arguments:
    its node name and its 2 neighboring node’s ports and broadcasts a string "Hello I'm A" to them (so A would broadcast to B and C). It will do this every 3 seconds.
  3. This program will be run in 3 separate instances.
  4. Upon receiving a string it will print what node it has received it from "Received string" (example for Port B).

I have difficulties implementing this, I have heard of something called multicasting with UDP though. Here is my work so far, what am I doing wrong?

class UDP {
    public static void main(String[] args) throws Exception {
        String nodeName = args[0];
        int neighbourPort1 = Integer.valueOf(args[1]);
        int neighbourPort2 = Integer.valueOf(args[2]);

        while(true) {
            Thread.sleep(3000); //every 3 seconds
            //Continously broadcast and listen to neighbour1
            DatagramSocket socket1 = null;
            try {
                //CREATE SOCKET TO NEIGHBOUR1
                InetAddress host = InetAddress.getByName("localhost");
                socket1 = new DatagramSocket();
                socket1.connect(host, neighbour1);

                //CREATE DATAGRAMS FOR SENDING
                String message = "Hello I'm " + nodeName;
                byte[] sendData = message.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, host, port);
                socket1.send(sendPacket);

                //CREATE DATAGRAMS FOR RECEIVING
                byte[] receiveData = new byte[100]; //is there a way to determine the needed space?
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                socket1.receive(receivePacket);
                System.out.println("Received string");

            } catch(Exception e) { }
            //Do the same for neighbour2, code is basically identical except for variables
            DatagramSocket socket2 = null;
            try {
                //CREATE SOCKET TO NEIGHBOUR2
                InetAddress host = InetAddress.getByName("localhost");
                socket2 = new DatagramSocket();
                socket2.connect(host, neighbour2);

                //FOR SENDING DATAGRAMS
                String message = "Hello I'm " + nodeName;
                byte[] sendData = message.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, host, port);
                socket2.send(sendPacket);

                //FOR RECEIVING DATAGRAMS
                byte[] receiveData = new byte[100]; //is there a way to determine the needed space?
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                socket2.receive(receivePacket);
                System.out.println("Received string");

            } catch(Exception e) { }
        }

    }
}

I know I’m close to the solution. I’m able to broadcast properly but it’s the constantly listening part that gets me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:44:49+00:00Added an answer on June 13, 2026 at 1:44 am

    I think its best to use a separate Thread to listen for data on your own port.

    • A sends data to B and blocks until it gets a packet from B.
    • B sends data to C and blocks until it gets a packet from C.
    • C sends data to A and blocks until it gets a packet from A.

    Every node is waiting for each other. Just send the packets and wait 3 seconds.
    The other thread is going to listen only.

    public class UDP {
    
        public static void main(String[] args) throws Exception {
            final String nodeName = args[0];
            final int ownPort = Integer.valueOf(args[1]);
            final int neighbourPort1 = Integer.valueOf(args[2]);
            final int neighbourPort2 = Integer.valueOf(args[3]);
    
    
            // Don't create a new socket every time
            DatagramSocket neighbour1 = new DatagramSocket();
            DatagramSocket neighbour2 = new DatagramSocket();
    
            neighbour1.connect(InetAddress.getLocalHost(), neighbourPort1);
            neighbour2.connect(InetAddress.getLocalHost(), neighbourPort2);
    
            // You have to LISTEN
            new Thread() {
                @Override
                public void run() {
                    try {
                        DatagramSocket socket = new DatagramSocket(ownPort);
    
                        byte[] buffer = new byte[socket.getReceiveBufferSize()];
                        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    
                        while (true) {
                            // Blocks until it gets a packet
                            socket.receive(packet);
    
                            System.out.println("Received string");
                        }
    
                        // socket.close();
                    } catch (final Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();
    
            while (true) {
                Thread.sleep(3000);
    
                sendPacket(neighbour1, nodeName);
                sendPacket(neighbour2, nodeName);
            }
    
            // If you're not using an infinite loop:
            // neighbour1.close();
            // neighbour2.close();
        }
    
        private static void sendPacket(DatagramSocket to, String from) throws Exception {
            String message = "Hello I'm " + from;
            byte[] data = message.getBytes();
    
            DatagramPacket packet = new DatagramPacket(data, data.length);
            to.send(packet);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a treeView with many nodes. I want that some nodes change their
Assume that I have two AVL trees and that I know their respective sizes.
I have a XmlString which contains multiple elements with their nodes. ie <Element> <AccountName>My
I have the following problem with Tikz/Latex: I have some nodes that contain text.
I have a prefuse application that loads GraphML files where the nodes have a
I am trying to solve the following problem with Puppet: I have multiple nodes.
For my homework assignment, I have a network of Nodes that are passing messages
I was trying to write my own little algorithm for graph layout that only
I have a treeview with several nodes and their child nodes under root node.
I have nodes in a D3 force-directed layout that are set to . fixed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.