AnsweredAssumed Answered

UDP broadcast failed

Question asked by 小川 崇 on Jan 20, 2014
Latest reply on Jan 22, 2014 by 小川 崇

Greetings,

 

I made UDP broadcast program as shown below.
I ran the program on i.MX6 board.
but sendto() always reterun -1.

i.MX6 board is setup with IP address 192.168.0.1, subnet 255.255.255.0.

I want to fix this error.
but I don't know how to fix this problem.
I appreciate if someone can help me to solve it

 

Regards,

takashi


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int
main()
{
int sock;
struct sockaddr_in addr;
int yes = 1;
int ret;
int opt;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)printf("socket() failed!!\n");

addr.sin_family = AF_INET;
addr.sin_port = htons(8503);
addr.sin_addr.s_addr = inet_addr("255.255.255.255");

opt = setsockopt(sock,SOL_SOCKET, SO_BROADCAST, (char *)&yes, sizeof(yes));
if(opt < 0)printf("setsockopt() failed!!\n");

ret = sendto(sock, "HELLO", 5, 0, (struct sockaddr *)&addr, sizeof(addr));
if(ret!=5)printf("sendto() failed!!\n");


close(sock);

return 0;
}

Outcomes