UDP broadcast failed

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 
4,524件の閲覧回数
小川崇
Contributor II

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;
}

ラベル(2)
1 解決策
3,313件の閲覧回数
BrilliantovKiri
Senior Contributor I

I think your system havn't routing to destination network.

Because you try send packet on broadcast address I think you solve probleam

after configuration default gateway.

元の投稿で解決策を見る

0 件の賞賛
5 返答(返信)
3,321件の閲覧回数
BrilliantovKiri
Senior Contributor I

Hello!

1. check errno variable after sendto

2. your system should have route to destination network

3,321件の閲覧回数
LeonardoSandova
Specialist I

In addition, if you can use strace to run your app, you will find more insight. To print the error with useful info, use strerror(errno) and do it just after the call to sendto (this variable is global, so any system call modify it).

Leo

3,321件の閲覧回数
小川崇
Contributor II

Hi

I checked errno and error reason by using strerror.

error no. is 101:Network is unreachable.

What does error no. 101 mean?

What is the problem?

takashi

0 件の賞賛
3,314件の閲覧回数
BrilliantovKiri
Senior Contributor I

I think your system havn't routing to destination network.

Because you try send packet on broadcast address I think you solve probleam

after configuration default gateway.

0 件の賞賛
3,321件の閲覧回数
小川崇
Contributor II

Hi

after I set default gateway  I ran the program.

sendto returned 5.

The error did not occur.

Thank you for your advice.

takashi

0 件の賞賛