Re Re: Re: UNIX Sockets - Broadcast address

Patrick Lidstone
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
>> 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?

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>
#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);

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

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

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

Any assistance most appreciated.


Unsubscribe info:
Bug reporting:

More information about the Cygwin mailing list