delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/12/13/03:09:53

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-Originating-IP: [213.48.208.45]
From: "Kay M" <passhark AT hotmail DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: Re: UNIX Sockets - Broadcast address
Date: Thu, 13 Dec 2001 08:08:15 +0000
Mime-Version: 1.0
Message-ID: <F69j03oHMwVN7miR9Od0000231c@hotmail.com>
X-OriginalArrivalTime: 13 Dec 2001 08:08:16.0974 (UTC) FILETIME=[51E6B6E0:01C183AD]

Yup, Like I said Ok with LINUX, so ?

What could be wrong ?. REMEMBER its the bind()ing function that gives an 
error!. Even with no server just client, bind()ing gives error.


If you can show me an example of a working server/client on the broadcast 
address Id be interested to see the code.

here's the client code from the LINUX Book. That works on linux, but not on 
CYGWIN.

/* gquotes.c:
*
* Get datagram stock market
* quotes from UDP broadcast :
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifndef TRUE
#define TRUE    1
#define FALSE   0
#endif

extern int mkaddr(
    void *addr,
    int *addrlen,
    char *str_addr,
    char *protocol);

/*
* This function reports the error and
* exits back to the shell :
*/
static void
bail(const char *on_what) {
    fputs(strerror(errno),stderr);
    fputs(": ",stderr);
    fputs(on_what,stderr);
    fputc('\n',stderr);
    exit(1);
}

int
main(int argc,char **argv) {
    int z;
    int x;
    struct sockaddr_in adr;     /* AF_INET */
    int len_inet;               /* length  */
    int s;                       /* Socket */
    char dgram[512];        /* Recv buffer */
    static int so_reuseaddr = TRUE;
    static char
        *bc_addr = "127.255.255.255:9097";

    /*
     * Use a server address from the command
     * line, if one has been provided.
     * Otherwise, this program will default
     * to using the arbitrary address
     * 127.0.0.23 :
     */
    if ( argc > 1 )
        /* Broadcast address : */
        bc_addr = argv[1];

    /*
     * Create a UDP socket to use :
     */
    s = socket(AF_INET,SOCK_DGRAM,0);
    if ( s == -1 )
        bail("socket()");

    /*
     * Form the broadcast address :
     */
    len_inet = sizeof adr;

    z = mkaddr(&adr,
        &len_inet,
        bc_addr,
        "udp");

    if ( z == -1 )
        bail("Bad broadcast address");

    /*
     * Allow multiple listeners on the
     * broadcast address :
     */
    z = setsockopt(s,
        SOL_SOCKET,
        SO_REUSEADDR,
        &so_reuseaddr,
        sizeof so_reuseaddr);

    if ( z == -1 )
        bail("setsockopt(SO_REUSEADDR)");

    /*
     * Bind our socket to the broadcast address:
     */
    z = bind(s,
        (struct sockaddr *)&adr,
        len_inet);

    if ( z == -1 )
        bail("bind(2)");

    for (;;) {
        /*
         * Wait for a broadcast message :
         */
        z = recvfrom(s,            /* Socket */
            dgram,       /* Receiving buffer */
            sizeof dgram,/* Max rcv buf size */
            0,          /* Flags: no options */
            (struct sockaddr *)&adr, /* Addr */
            &x);       /* Addr len, in & out */

        if ( z < 0 )
            bail("recvfrom(2)"); /* else err */

        fwrite(dgram,z,1,stdout);
        putchar('\n');

        fflush(stdout);
    }

    return 0;
}


On Wed, Dec 12, 2001 at 02:36:10AM +0000, Kay M wrote:
>OK I didnt mean connecting in the socket API sense.....erm but when bind 
>takes place an error is given. I specified 127.255.255.255 and even 
>INADDR_BROADCAST......still same error.
>
>Even tried some demo files from socket book. #
>
>All ok with Linux.

Did you call setsockopt(..., SO_REUSEADDR, ...) before binding?

Corinna

_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com


--
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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019