Re Re: Re: UNIX Sockets - Broadcast address

Patrick Lidstone patrick@lidstone.net
Wed Dec 18 06:12:00 GMT 2002


>On Mon, Dec 17, 2001 at 03:37:13AM +0000, Kay M wrote:
>> So what the bottom line, no broadcasting ?.
>> 
>> Question: Has any one ever written a program on cygwin that uses A
broadcast 
>> address ?. A client program to be more specific.
>
>What about actually debugging the problem?  Or did you try using
>WinSock functions directly instead of the Cygwin wrapper functions
>just for debugging purposes?  Anything which tracks down the cause?
>
>Corinna

I have a similar issue with the use of broadcast sockets.
I have code which binds correctly to a broadcast socket under Linux, but
which won't bind to anything other than INADDR_ANY under Win/32.
Code which performs a similar function under Winsock 2 API and MSVC 6
works just fine. The symptoms are indentical to that described by the
previous poster: the socket is created ok and broadcast options are set
ok, and the bind fails with a return of 1. Although not listed in the
man pages as a valid error number, this looks like it could be related
to permissions going by error.h -- but I'm not aware of any
permissioning mechanisms under Win/32 for this kind of thing?

Here is the test code in full:

gcc -o bs badsock.c for Linux
gcc -o bs badsock.c -D WIN32 for Windows

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#ifndef WIN32
#include <net/route.h>
#endif
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>

int main() {
	int i_optval, i_optlen;
	int i;
        struct sockaddr_in server_address;
        int server_sockfd;
        
        server_sockfd=socket(AF_INET, SOCK_DGRAM, 0);

	i_optval=1;
	i_optlen=sizeof(int);
	if (setsockopt(server_sockfd, SOL_SOCKET, SO_BROADCAST,
(char*)&i_optval, i_optlen)) {
		printf("Cannot set options on broadcast socket\n");
	}
	
	i_optval=1;
	i_optlen=sizeof(int);
	if (setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR,
(char*)&i_optval, i_optlen)) {
		printf("Cannot set reuseaddr options on broadcast
socket\n");
	}

	server_address.sin_family = AF_INET; 	
//	server_address.sin_addr.s_addr=inet_addr("127.255.255.255");
//<-- fails under windows
//	server_address.sin_addr.s_addr=inet_addr("0.0.0.0");
//<-- works under windows
	server_address.sin_addr.s_addr=inet_addr("192.168.0.255");
//<-- fails under windows
//	server_address.sin_addr.s_addr=htonl(INADDR_ANY);
//<-- same as 0.0.0.0
	server_address.sin_port=htons(3639);
	
	if (i=bind(server_sockfd, (struct sockaddr*)&server_address,
sizeof(server_address))!=0) {
		switch (i) {
			case EPERM: printf("Not a superuser\n");
			break;
			case EBADF: printf("Bad sock desc\n");
			break;
			case EINVAL: printf("Sock in use\n");
			break;
			case EACCES: printf("User not entitled\n");
			break;
			case ENOTSOCK: printf("Not a socket!\n");
			break;
			case EROFS: printf("Read only FS\n");
			break;
		} // switch
		printf("Broadcast socket error, bind returned %d\n",i);

	} else printf("All hunky-dory\n");
	return 0;
}


Any assistance most appreciated.

Patrick


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list