We help IT Professionals succeed at work.
Get Started

UDP setsockopt error..

Chizl
Chizl asked
on
2,891 Views
Last Modified: 2007-12-19
I have an small class I trying to implement both TCP messaging and UDP messaging.   We have some messages that are to be directed at one person and needs to be guarenteed, and we have update and status messages that need to be broadcasted and not really care if it gets out or not..  

I have it down to the part of setsocketopt, which fails once it's hit that line, JUST for the UDP though, TCP side is fine..  The socket is setup, no errors shows everything is good and on setsocketopt, it fails..

The error is 10042 which means, "Bad protocol option", but I found if I ignore the error message the message is still sent and received by the clients..   I would like to remove this error and do it right or is this a norm with UDP?

FOR THE UDP part I have this:

ip_mreq mreq;
mreq.imr_multiaddr.s_addr=inet_addr(ipAddress);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);

if( (sckSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP )) == INVALID_SOCKET )
    ...

sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = 0; //automatic assignment: next available
local.sin_addr.s_addr = htonl(INADDR_ANY);

if( bind( sckSocket, (sockaddr*) &local, sizeof(sockaddr_in) ) == SOCKET_ERROR )
   ...

if( setsockopt( sckSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq) ) == SOCKET_ERROR )
   ...

srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons( port );

.......


Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE