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 8714227
In Process

The Archive Base Latest Questions

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

I’m having some problems receiving a String from a multicast. It’s the first time

  • 0

I’m having some problems receiving a String from a multicast.
It’s the first time i’m using UDP multicast in Java.

I’m making a multiclient application over a LAN.
So i’m using Local Ip addresses.
I need the clients to find the server’s IP address so they can send their data, requests, etc.

I let the server sent out a multicast with his own IP as a string every 5 seconds.
The clients should be able to receive it.
The problem is that they don’t receive anything.
I’m testing with 2 devices so i don’t need to use localhost.

Here’s some of my code:
Server side:

public class MulticastIpSender extends Thread{

private String serverIp;
private int port;
private String multicastAddress;
private long WAITING_TIME = 5000;  // 5 seconden

private DatagramSocket socket;

public MulticastIpSender(String serverIp, int port, String multicastAddress) throws SocketException {
    super();
    this.serverIp = serverIp;
    this.port = port;
    this.multicastAddress = multicastAddress;
    socket = new DatagramSocket(port);
}

public void run() {
    while(true){
        try {
            byte[] buf = new byte[256];
            buf = serverIp.getBytes();
            InetAddress group = InetAddress.getByName(multicastAddress);
            DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);
            socket.send(packet);
            System.out.println("sent IP("+serverIp+") to group("+group+") on port "+port);
            sleep(WAITING_TIME);

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

}

I put this method in the main class:

private void sendIpAddressToListeners() {
    try {
        multicastIpSender = new MulticastIpSender(serverIp,PORT,"230.0.0.1");
        multicastIpSender.run();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

I get the serverIp like this:

    String hostName = InetAddress.getLocalHost().getHostName();
        InetAddress addrs[] = InetAddress.getAllByName(hostName);
if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
        myIp = addr.getHostAddress();
        }
        }
        System.out.println ("\nIP = " + myIp);
        return myIp;

on this device it gives me 192.168.1.2

CLIENT SIDE:

multicastSocket = new MulticastSocket(PORT);
        InetAddress address = InetAddress.getByName(MULTICAST_ADDRESS);
        multicastSocket.joinGroup(address);

        DatagramPacket serverIpPacket;
        byte[] buf = new byte[256];
        serverIpPacket = new DatagramPacket(buf, buf.length);
        while(receivedIp ==null){
            multicastSocket.receive(serverIpPacket);
            receivedIp = new String(serverIpPacket.getData(), 0, serverIpPacket.getLength());
            System.out.println("received server ip: " + receivedIp);
        }

!! PORT = 4445 in both server and client

I hope somebody can help me with this or can explain a better way to do this.

  • 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-13T05:32:45+00:00Added an answer on June 13, 2026 at 5:32 am

    Send a normal DatagramPacket on broadcast address, the packet will be received by all hosts in local network (with same network configuration, important part is mask)

    Use calculator to check your broadcast address, this one works good: http://www.subnet-calculator.com/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.