Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Problem with UDP Socket: (5 Items)
   
Problem with UDP Socket  
Hi Guys,

I have a problem with the UDP socket.

The code is following:
---------------------------------------------
	if ((sock_broadcast= socket(AF_INET, SOCK_DGRAM, 0)) < 00) {
		printf("socket creation error\n");
	}

	//set broadcasting socket options
	const int opt = 1;
	extern int errno;
	if (setsockopt(sock_broadcast, SOL_SOCKET, SO_REUSEPORT | SO_BROADCAST | SO_KEEPALIVE,
			(char *) &opt, sizeof(opt))  < 0) {
		printf("[set socket options error...%d\n", errno);
	}
---------------------------------------------

I create a UDP socket, then I use setsockopt() to set the options with REUSE and BROADCAST. It always returns -1, but 
the errno number is 0.

Do you have any solution to this problem?

Thanks in advance.
Re: Problem with UDP Socket  
You need to include <errno.h> and get rid of 'extern int errno'.
errno is special.

On Fri, Mar 11, 2016 at 09:31:11AM -0500, Long Peng wrote:
> Hi Guys,
> 
> I have a problem with the UDP socket.
> 
> The code is following:
> ---------------------------------------------
> 	if ((sock_broadcast= socket(AF_INET, SOCK_DGRAM, 0)) < 00) {
> 		printf("socket creation error\n");
> 	}
> 
> 	//set broadcasting socket options
> 	const int opt = 1;
> 	extern int errno;
> 	if (setsockopt(sock_broadcast, SOL_SOCKET, SO_REUSEPORT | SO_BROADCAST | SO_KEEPALIVE,
> 			(char *) &opt, sizeof(opt))  < 0) {
> 		printf("[set socket options error...%d\n", errno);
> 	}
> ---------------------------------------------
> 
> I create a UDP socket, then I use setsockopt() to set the options with REUSE and BROADCAST. It always returns -1, but 
the errno number is 0.
> 
> Do you have any solution to this problem?
> 
> Thanks in advance.
> 
> 
> 
> _______________________________________________
> 
> OSTech
> http://community.qnx.com/sf/go/post115945
> To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
Re: Problem with UDP Socket  
Hi Sean,

Thanks for your response. Now I get the errno.

But Why does setsockopt() return error  ENOPROTOOPT ?    242                /* Protocol not available */.

I try to bind the socket. I also return error EADDRNOTAVAIL   249                /* Can't assign requested address */.

The code is like this:
        bzero(&addr_broadcast, sizeof(struct sockaddr_in));
	addr_broadcast.sin_family = AF_INET;
	addr_broadcast.sin_addr.s_addr = htonl(INADDR_BROADCAST);
	addr_broadcast.sin_port = htons(SOCK_BROADCAST_PORT);

	if (bind(sock_broadcast, (struct sockaddr *) &(addr_broadcast), sizeof(struct sockaddr_in)) < 0) {
		printf("bind sock_broadcast error...%d\n", errno);
	}


Do you have any opinion? I am wondering if QNX UDP supports broadcasting, or I dis something wrong.


best regards,
Long

> 
> You need to include <errno.h> and get rid of 'extern int errno'.
> errno is special.
> 
> On Fri, Mar 11, 2016 at 09:31:11AM -0500, Long Peng wrote:
> > Hi Guys,
> > 
> > I have a problem with the UDP socket.
> > 
> > The code is following:
> > ---------------------------------------------
> > 	if ((sock_broadcast= socket(AF_INET, SOCK_DGRAM, 0)) < 00) {
> > 		printf("socket 
> creation error\n");
> > 	}
> > 
> > 	//set broadcasting socket options
> > 	const int opt = 1;
> > 	extern int errno;
> > 	if (setsockopt(sock_broadcast, SOL_SOCKET, SO_REUSEPORT | SO_BROADCAST | 
> SO_KEEPALIVE,
> > 			(char *) &opt, sizeof(opt))  < 0) {
> > 		printf("[set socket options error...%d\n"
> , errno);
> > 	}
> > ---------------------------------------------
> > 
> > I create a UDP socket, then I use setsockopt() to set the options with REUSE
>  and BROADCAST. It always returns -1, but the errno number is 0.
> > 
> > Do you have any solution to this problem?
> > 
> > Thanks in advance.
> > 
> > 
> > 
> > _______________________________________________
> > 
> > OSTech
> > http://community.qnx.com/sf/go/post115945
> > To cancel your subscription to this discussion, please e-mail ostech-core_os
> -unsubscribe@community.qnx.com


Re: Problem with UDP Socket  
You can't or in options.  They're not flags.

On Fri, Mar 11, 2016 at 10:06:05AM -0500, Long Peng wrote:
> Hi Sean,
> 
> Thanks for your response. Now I get the errno.
> 
> But Why does setsockopt() return error  ENOPROTOOPT ?    242                /* Protocol not available */.
> 
> I try to bind the socket. I also return error EADDRNOTAVAIL   249                /* Can't assign requested address */.

> 
> The code is like this:
>         bzero(&addr_broadcast, sizeof(struct sockaddr_in));
> 	addr_broadcast.sin_family = AF_INET;
> 	addr_broadcast.sin_addr.s_addr = htonl(INADDR_BROADCAST);
> 	addr_broadcast.sin_port = htons(SOCK_BROADCAST_PORT);
> 
> 	if (bind(sock_broadcast, (struct sockaddr *) &(addr_broadcast), sizeof(struct sockaddr_in)) < 0) {
> 		printf("bind sock_broadcast error...%d\n", errno);
> 	}
> 
> 
> Do you have any opinion? I am wondering if QNX UDP supports broadcasting, or I dis something wrong.
> 
> 
> best regards,
> Long
> 
> > 
> > You need to include <errno.h> and get rid of 'extern int errno'.
> > errno is special.
> > 
> > On Fri, Mar 11, 2016 at 09:31:11AM -0500, Long Peng wrote:
> > > Hi Guys,
> > > 
> > > I have a problem with the UDP socket.
> > > 
> > > The code is following:
> > > ---------------------------------------------
> > > 	if ((sock_broadcast= socket(AF_INET, SOCK_DGRAM, 0)) < 00) {
> > > 		printf("socket 
> > creation error\n");
> > > 	}
> > > 
> > > 	//set broadcasting socket options
> > > 	const int opt = 1;
> > > 	extern int errno;
> > > 	if (setsockopt(sock_broadcast, SOL_SOCKET, SO_REUSEPORT | SO_BROADCAST | 
> > SO_KEEPALIVE,
> > > 			(char *) &opt, sizeof(opt))  < 0) {
> > > 		printf("[set socket options error...%d\n"
> > , errno);
> > > 	}
> > > ---------------------------------------------
> > > 
> > > I create a UDP socket, then I use setsockopt() to set the options with REUSE
> >  and BROADCAST. It always returns -1, but the errno number is 0.
> > > 
> > > Do you have any solution to this problem?
> > > 
> > > Thanks in advance.
> > > 
> > > 
> > > 
> > > _______________________________________________
> > > 
> > > OSTech
> > > http://community.qnx.com/sf/go/post115945
> > > To cancel your subscription to this discussion, please e-mail ostech-core_os
> > -unsubscribe@community.qnx.com
> 
> 
> 
> 
> 
> 
> _______________________________________________
> 
> OSTech
> http://community.qnx.com/sf/go/post115947
> To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
Re: Problem with UDP Socket  
You are absolutely correct, Sean. There is no problem if I set the options one by one. They are not flags.

Thanks very much.

> 
> You can't or in options.  They're not flags.
> 
> On Fri, Mar 11, 2016 at 10:06:05AM -0500, Long Peng wrote:
> > Hi Sean,
> > 
> > Thanks for your response. Now I get the errno.
> > 
> > But Why does setsockopt() return error  ENOPROTOOPT ?    242                
> /* Protocol not available */.
> > 
> > I try to bind the socket. I also return error EADDRNOTAVAIL   249           
>      /* Can't assign requested address */.
> > 
> > The code is like this:
> >         bzero(&addr_broadcast, sizeof(struct sockaddr_in));
> > 	addr_broadcast.sin_family = AF_INET;
> > 	addr_broadcast.sin_addr.s_addr = htonl(INADDR_BROADCAST);
> > 	addr_broadcast.sin_port = htons(SOCK_BROADCAST_PORT);
> > 
> > 	if (bind(sock_broadcast, (struct sockaddr *) &(addr_broadcast), 
> sizeof(struct sockaddr_in)) < 0) {
> > 		printf("bind sock_broadcast error...%d\n", 
> errno);
> > 	}
> > 
> > 
> > Do you have any opinion? I am wondering if QNX UDP supports broadcasting, or
>  I dis something wrong.
> > 
> > 
> > best regards,
> > Long
> > 
> > > 
> > > You need to include <errno.h> and get rid of 'extern int errno'.
> > > errno is special.
> > > 
> > > On Fri, Mar 11, 2016 at 09:31:11AM -0500, Long Peng wrote:
> > > > Hi Guys,
> > > > 
> > > > I have a problem with the UDP socket.
> > > > 
> > > > The code is following:
> > > > ---------------------------------------------
> > > > 	if ((sock_broadcast= socket(AF_INET, SOCK_DGRAM, 0)) < 00) {
> > > > 		printf("
> socket 
> > > creation error\n");
> > > > 	}
> > > > 
> > > > 	//set broadcasting socket options
> > > > 	const int opt = 1;
> > > > 	extern int errno;
> > > > 	if (setsockopt(sock_broadcast, SOL_SOCKET, SO_REUSEPORT | SO_BROADCAST 
> | 
> > > SO_KEEPALIVE,
> > > > 			(char *) &opt, sizeof(opt))  < 0) {
> > > > 		printf("[set socket options error
> ...%d\n"
> > > , errno);
> > > > 	}
> > > > ---------------------------------------------
> > > > 
> > > > I create a UDP socket, then I use setsockopt() to set the options with 
> REUSE
> > >  and BROADCAST. It always returns -1, but the errno number is 0.
> > > > 
> > > > Do you have any solution to this problem?
> > > > 
> > > > Thanks in advance.
> > > > 
> > > > 
> > > > 
> > > > _______________________________________________
> > > > 
> > > > OSTech
> > > > http://community.qnx.com/sf/go/post115945
> > > > To cancel your subscription to this discussion, please e-mail ostech-
> core_os
> > > -unsubscribe@community.qnx.com
> > 
> > 
> > 
> > 
> > 
> > 
> > _______________________________________________
> > 
> > OSTech
> > http://community.qnx.com/sf/go/post115947
> > To cancel your subscription to this discussion, please e-mail ostech-core_os
> -unsubscribe@community.qnx.com