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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:26:37+00:00 2026-06-05T15:26:37+00:00

I am writing a small program that sends and receive multicast packets.I need to

  • 0

I am writing a small program that sends and receive multicast packets.I need to set the outgoing interface with its name (e.g. eth0) rather than its address. Therefore I have to use struct ip_mreqn (rather than struct in_addr) so that I could use its imr_ifindex field to set the interface index (which I can get using interface’s name).

However for some reason it doesn’t work. The call to setsockopt() works fine but the following call to sendto() returns “Invalid argument” error. Of course the error disappears if I replace ip_mreqn with in_addr and use interface’s address instead.

Following is my code:

sd = socket(AF_INET, SOCK_DGRAM, 0);

struct ip_mreqn addr;
addr.imr_ifindex = if_nametoindex("eth0");
setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr);

struct sockaddr_in sock_addr;
memset((char *) &sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = destination_port;
sock_addr.sin_addr.s_addr = destination_address;

char msg[] = "abc";

while (sendto(sd, msg, sizeof(msg), 0, reinterpret_cast<const sockaddr*>(&sock_addr),
  sizeof(sock_addr)) < 0)
    throw std::runtime_error(strerror(errno));

Is there any problem in using struct ip_mreqn when setting IP_MULTICAST_IF? Has anyone got any idea? Really appreciate help. Thanks.

Note that destination port and address are already in network byte order.

  • 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-05T15:26:38+00:00Added an answer on June 5, 2026 at 3:26 pm

    From tldp’s multicast howto:

    Usually, the system administrator specifies the default interface multicast datagrams should be sent from. The programmer can override this and choose a concrete outgoing interface for a given socket with this option.

    struct in_addr interface_addr;
    setsockopt (socket, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr));
    

    Just an index to an interface doesn’t suffice according to them.

    Edit:
    You’re maybe confusing IP_MULTICAST_IF with IPV6_MULTICAST_IF, the latter takes an unsigned integer to denote the interface number (see man 7 ipv6).

    Edit:
    As it turns out the example given works as expected. Here’s my full test case:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <net/if.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    
    
    int main()
    {
        int sd = socket(AF_INET, SOCK_DGRAM, 0);
        struct sockaddr_in sa = {0};
        struct ip_mreqn addr = {{0}};
        char msg[] = "abc";
    
        addr.imr_ifindex = if_nametoindex("eth0");
        setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
    
        sa.sin_family = AF_INET;
        sa.sin_port = 8653;
    
        inet_pton(AF_INET, "224.255.255.5", &sa.sin_addr);
        if (sendto(sd, msg, sizeof(msg), 0, (void*)&sa, sizeof(sa)) < 0) {
            perror("bugger");
            return 1;
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small program that gets activated when sms is received.We need
I have a problem with Regular Expressions. I'm writing a small program that matches
I'm pretty new to C# and Visual Studio. I'm writing a small program that
I'm writing a small JavaScript program that makes web calls to pull up weather
I am writing a small program in Visual Studio 11 (beta) that a console
Today I am writing a small program in Haskell. I found that in ghci's
I am writing a small test program that gives the following xml file as
I'm writing a small program that creates a new Windows desktop, switches to it
I'm writing a small C program that uses librt. I'm quite surprised that the
I'm writing a small program in C that will read input from the console.

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.