Why do I get -1 when recvfrom() from a multicast group address?












1














I am writting a LAN device detect test which can send a request to a multicast group 239.255.255.250 and get response from device where in same LAN as well,



Wireshark tells me, join to group(IGMPv2) is good, sending(UDP) is good, reciving(UDP) is good, BUT in my code, It keeps return -1 when recvfrom(), Whatever I tried, it's always -1...
I ever tried a 3rd party app in windows, it can send&recive normally.



Could anybody gives me a clue ? Thx in advance.



    socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(socket, SOL_SOCKET, SO_IFDEVICE, &i8IfIndex, sizeof(i8IfIndex));
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &u8On, sizeof(int));
/*all aboved checked alright*/

/*join to multicast group*/
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = htonl(0xEFFFFFFA); /*239.255.255.250*/
mreq.imr_interface.s_addr = INADDR_ANY;
setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

memset(&socket_address, 0, sizeof(socket_address));
socket_address.sin_addr.s_addr = htonl(0xEFFFFFFA);
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(37020);
//no help:bind(socket, (PSA) &socket_address, sizeof(socket_address));

memset(&socket_address2, 0, sizeof(socket_address2));
socket_address2.sin_addr.s_addr = INADDR_ANY;
socket_address2.sin_family = AF_INET;
socket_address2.sin_port = 0;

/*send & recv*/
int8 i8Result = 0xcc;
char *inquryStr = "<?xml version="1.0" encoding="utf-8"?><Probe><Uuid>232B1234-FBcC-48B5-AC06-AE1F77051280</Uuid><Types>inquiry</Types></Probe>";
if(i8Result = sendto(socket, inquryStr, strlen(inquryStr), 0, (PSA) &socket_address, sizeof(socket_address))) {
//wireshark detected ,i8Result shows 0x7C sent
if(i8Result = recvfrom(socket, g_HjtOsdComm.u8TRBuf, 1024, 0, (PSA) &socket_address2, sizeof(socket_address2))) {
//wireshark detected ,but i8Result get-1
}
}

/*leave group*/
setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));

fdClose(HjtOsdSocket);









share|improve this question






















  • And what does the error code say? strerror(errno)
    – immibis
    Nov 21 at 3:12










  • Thanks for your clue, it really helps, strerror shows "bad address",then I searched online and found a thread says "last arg of recvfrom is the address of the len instead of len it self",so problem solved!
    – jerron
    Nov 21 at 5:15
















1














I am writting a LAN device detect test which can send a request to a multicast group 239.255.255.250 and get response from device where in same LAN as well,



Wireshark tells me, join to group(IGMPv2) is good, sending(UDP) is good, reciving(UDP) is good, BUT in my code, It keeps return -1 when recvfrom(), Whatever I tried, it's always -1...
I ever tried a 3rd party app in windows, it can send&recive normally.



Could anybody gives me a clue ? Thx in advance.



    socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(socket, SOL_SOCKET, SO_IFDEVICE, &i8IfIndex, sizeof(i8IfIndex));
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &u8On, sizeof(int));
/*all aboved checked alright*/

/*join to multicast group*/
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = htonl(0xEFFFFFFA); /*239.255.255.250*/
mreq.imr_interface.s_addr = INADDR_ANY;
setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

memset(&socket_address, 0, sizeof(socket_address));
socket_address.sin_addr.s_addr = htonl(0xEFFFFFFA);
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(37020);
//no help:bind(socket, (PSA) &socket_address, sizeof(socket_address));

memset(&socket_address2, 0, sizeof(socket_address2));
socket_address2.sin_addr.s_addr = INADDR_ANY;
socket_address2.sin_family = AF_INET;
socket_address2.sin_port = 0;

/*send & recv*/
int8 i8Result = 0xcc;
char *inquryStr = "<?xml version="1.0" encoding="utf-8"?><Probe><Uuid>232B1234-FBcC-48B5-AC06-AE1F77051280</Uuid><Types>inquiry</Types></Probe>";
if(i8Result = sendto(socket, inquryStr, strlen(inquryStr), 0, (PSA) &socket_address, sizeof(socket_address))) {
//wireshark detected ,i8Result shows 0x7C sent
if(i8Result = recvfrom(socket, g_HjtOsdComm.u8TRBuf, 1024, 0, (PSA) &socket_address2, sizeof(socket_address2))) {
//wireshark detected ,but i8Result get-1
}
}

/*leave group*/
setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));

fdClose(HjtOsdSocket);









share|improve this question






















  • And what does the error code say? strerror(errno)
    – immibis
    Nov 21 at 3:12










  • Thanks for your clue, it really helps, strerror shows "bad address",then I searched online and found a thread says "last arg of recvfrom is the address of the len instead of len it self",so problem solved!
    – jerron
    Nov 21 at 5:15














1












1








1







I am writting a LAN device detect test which can send a request to a multicast group 239.255.255.250 and get response from device where in same LAN as well,



Wireshark tells me, join to group(IGMPv2) is good, sending(UDP) is good, reciving(UDP) is good, BUT in my code, It keeps return -1 when recvfrom(), Whatever I tried, it's always -1...
I ever tried a 3rd party app in windows, it can send&recive normally.



Could anybody gives me a clue ? Thx in advance.



    socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(socket, SOL_SOCKET, SO_IFDEVICE, &i8IfIndex, sizeof(i8IfIndex));
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &u8On, sizeof(int));
/*all aboved checked alright*/

/*join to multicast group*/
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = htonl(0xEFFFFFFA); /*239.255.255.250*/
mreq.imr_interface.s_addr = INADDR_ANY;
setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

memset(&socket_address, 0, sizeof(socket_address));
socket_address.sin_addr.s_addr = htonl(0xEFFFFFFA);
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(37020);
//no help:bind(socket, (PSA) &socket_address, sizeof(socket_address));

memset(&socket_address2, 0, sizeof(socket_address2));
socket_address2.sin_addr.s_addr = INADDR_ANY;
socket_address2.sin_family = AF_INET;
socket_address2.sin_port = 0;

/*send & recv*/
int8 i8Result = 0xcc;
char *inquryStr = "<?xml version="1.0" encoding="utf-8"?><Probe><Uuid>232B1234-FBcC-48B5-AC06-AE1F77051280</Uuid><Types>inquiry</Types></Probe>";
if(i8Result = sendto(socket, inquryStr, strlen(inquryStr), 0, (PSA) &socket_address, sizeof(socket_address))) {
//wireshark detected ,i8Result shows 0x7C sent
if(i8Result = recvfrom(socket, g_HjtOsdComm.u8TRBuf, 1024, 0, (PSA) &socket_address2, sizeof(socket_address2))) {
//wireshark detected ,but i8Result get-1
}
}

/*leave group*/
setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));

fdClose(HjtOsdSocket);









share|improve this question













I am writting a LAN device detect test which can send a request to a multicast group 239.255.255.250 and get response from device where in same LAN as well,



Wireshark tells me, join to group(IGMPv2) is good, sending(UDP) is good, reciving(UDP) is good, BUT in my code, It keeps return -1 when recvfrom(), Whatever I tried, it's always -1...
I ever tried a 3rd party app in windows, it can send&recive normally.



Could anybody gives me a clue ? Thx in advance.



    socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(socket, SOL_SOCKET, SO_IFDEVICE, &i8IfIndex, sizeof(i8IfIndex));
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &u8On, sizeof(int));
/*all aboved checked alright*/

/*join to multicast group*/
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = htonl(0xEFFFFFFA); /*239.255.255.250*/
mreq.imr_interface.s_addr = INADDR_ANY;
setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

memset(&socket_address, 0, sizeof(socket_address));
socket_address.sin_addr.s_addr = htonl(0xEFFFFFFA);
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(37020);
//no help:bind(socket, (PSA) &socket_address, sizeof(socket_address));

memset(&socket_address2, 0, sizeof(socket_address2));
socket_address2.sin_addr.s_addr = INADDR_ANY;
socket_address2.sin_family = AF_INET;
socket_address2.sin_port = 0;

/*send & recv*/
int8 i8Result = 0xcc;
char *inquryStr = "<?xml version="1.0" encoding="utf-8"?><Probe><Uuid>232B1234-FBcC-48B5-AC06-AE1F77051280</Uuid><Types>inquiry</Types></Probe>";
if(i8Result = sendto(socket, inquryStr, strlen(inquryStr), 0, (PSA) &socket_address, sizeof(socket_address))) {
//wireshark detected ,i8Result shows 0x7C sent
if(i8Result = recvfrom(socket, g_HjtOsdComm.u8TRBuf, 1024, 0, (PSA) &socket_address2, sizeof(socket_address2))) {
//wireshark detected ,but i8Result get-1
}
}

/*leave group*/
setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));

fdClose(HjtOsdSocket);






c multicast






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 2:50









jerron

62




62












  • And what does the error code say? strerror(errno)
    – immibis
    Nov 21 at 3:12










  • Thanks for your clue, it really helps, strerror shows "bad address",then I searched online and found a thread says "last arg of recvfrom is the address of the len instead of len it self",so problem solved!
    – jerron
    Nov 21 at 5:15


















  • And what does the error code say? strerror(errno)
    – immibis
    Nov 21 at 3:12










  • Thanks for your clue, it really helps, strerror shows "bad address",then I searched online and found a thread says "last arg of recvfrom is the address of the len instead of len it self",so problem solved!
    – jerron
    Nov 21 at 5:15
















And what does the error code say? strerror(errno)
– immibis
Nov 21 at 3:12




And what does the error code say? strerror(errno)
– immibis
Nov 21 at 3:12












Thanks for your clue, it really helps, strerror shows "bad address",then I searched online and found a thread says "last arg of recvfrom is the address of the len instead of len it self",so problem solved!
– jerron
Nov 21 at 5:15




Thanks for your clue, it really helps, strerror shows "bad address",then I searched online and found a thread says "last arg of recvfrom is the address of the len instead of len it self",so problem solved!
– jerron
Nov 21 at 5:15

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404635%2fwhy-do-i-get-1-when-recvfrom-from-a-multicast-group-address%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404635%2fwhy-do-i-get-1-when-recvfrom-from-a-multicast-group-address%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'