LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2015, 12:40 PM   #1
hnasr2001
Member
 
Registered: Nov 2012
Posts: 70

Rep: Reputation: Disabled
setsockopt() fails


I am trying to open and set options for socket. I am getting "protocol error" for the setsockopt() function. I did not include the libraries here.
All functions above the setsockopt, return 0.
Any help will be appreciated.

--------------------------------------------------------------
#define MCAST_GROUP "239.255.0.1"
#define MCAST_PORT 30785
#define NONBLOCKING 1

int main(int argc , char *argv[])
{
int sock;
int ctl;
int on = 1;
int mode = 0;
struct sockaddr_in addr;
struct ip_mreq mreq;
mode = NONBLOCKING;
sock = socket(AF_INET , SOCK_STREAM , 0);
ctl = ioctl(sock, FIONBIO, &mode);

setsock = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));

memset(&addr, 0, sizeof(addr)); /* fill memory with 0*/
addr.sin_family=AF_INET;
addr.sin_port=htons(MCAST_PORT);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
mreq.imr_multiaddr.s_addr=inet_addr(MCAST_GROUP);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq));
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
perror("setsockopt");
exit(1);
}
return 0;
}
 
Old 02-10-2015, 01:14 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,988
Blog Entries: 12

Rep: Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046Reputation: 2046
For what it's worth, I did stage this program and see the EXACT same problem you're seeing. I searched a bit for information or examples related to multicast. I don't see what's wrong. I did feel that you ought to have looked at your interim results from socket(), setsockopt(), and so forth.

The other comments are when you perform the setsockopt() for IP_ADD_MEMBERSHIP, you do that twice, I'm guessing because you added debug via copy/paste. Either case, keep the second iteration of it and remove the first one.

Next, in the line where you set up mreg.imr_multiaddr.s_addr I think you need to use addr and perform a memcpy of the structure:
Code:
    memset(&addr, 0, sizeof(addr)); /* fill memory with 0*/
    addr.sin_family=AF_INET;
    addr.sin_port=htons(MCAST_PORT);
    addr.sin_addr.s_addr=MCAST_GROUP;
    memcpy(&mreq.imr_multiaddr.s_addr, &addr, sizeof(struct in_addr));
    mreq.imr_interface.s_addr=htonl(INADDR_ANY);
However this doesn't work either. My editing of your code ended up getting the same errno you're seeing.

Maybe search for some examples of multicast out there in code and copy exactly to see if they work first and then edit them to match what you specifically want.

Here's my total effort, sorry I was not able to find a resolution either.

In the future, it's more helpful if you use [code][/code] tags to put in code, it retains the format and allows for better cut/paste. Further, adding the includes and your entire file is helpful so that other programmers won't have to search for the thread of needed include files. There's a link in my signature to describe how to do that.
Code:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <errno.h>

#define MCAST_GROUP "239.255.0.1"
#define MCAST_PORT 30785
#define NONBLOCKING 1

int main(int argc , char *argv[])
{
    int sock;
    int ctl;
    int on = 1;
    int mode = 0;
    int retval;
    struct sockaddr_in addr;
    struct ip_mreq mreq;

    mode = NONBLOCKING;
    if((sock = socket(AF_INET , SOCK_STREAM , 0)) <= 0) {
        printf("socket create error: %d:%s\n", errno, strerror(errno));
        return -1;
    }

    if((ctl = ioctl(sock, FIONBIO, &mode)) < 0) {
        printf("ioctl error: %d:%s\n", errno, strerror(errno));
        return -1;
    }

    if((retval = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) < 0) {
        printf("set option error(1): %d:%s\n", errno, strerror(errno));
        return -1;
    }

    memset(&addr, 0, sizeof(addr)); /* fill memory with 0*/
    addr.sin_family=AF_INET;
    addr.sin_port=htons(MCAST_PORT);
    addr.sin_addr.s_addr=MCAST_GROUP;
    memcpy(&mreq.imr_multiaddr.s_addr, &addr, sizeof(struct in_addr));
    mreq.imr_interface.s_addr=htonl(INADDR_ANY);

    //setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq));
    // Why was that there?

    if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
        printf("set option error(2): %d:%s\n", errno, strerror(errno));
        return -1;
    }

    return 0;
}

Last edited by rtmistler; 02-10-2015 at 01:16 PM.
 
Old 12-04-2017, 02:40 PM   #3
Upd4ting
LQ Newbie
 
Registered: Dec 2017
Posts: 1

Rep: Reputation: Disabled
Hello,

For future googlers that still have this error.

The error is at this line:
Code:
if((sock = socket(AF_INET , SOCK_STREAM , 0)) <= 0) {
You need to change that by
Code:
if((sock = socket(AF_INET , SOCK_DGRAM, 0)) <= 0) {
To ensure that you are working with UDP socket.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
setsockopt: SO_RCVTIMEO Ephracis Programming 2 03-18-2013 03:43 AM
setsockopt for SCTP_PEER_ADDR_PARAMS oxyjohn Programming 5 05-07-2007 12:29 PM
setsockopt() in kernel? hegdeshashi Linux - Networking 0 01-12-2006 01:17 AM
setsockopt Mercurius Programming 6 12-24-2005 08:16 AM
setsockopt with level IPPROTO_UDP longp Linux - Networking 0 07-12-2005 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration