Problems with broadcast UDP sockets through winsock

I'm writing a TCP/IP network chat program - and I thought everything was working fine on my computer till I realised that although i was a ble to send packets to myself no other computer on my Win95 network sees them - can anyone see anything wrong wit this code (which i use to open/bind the socket)...

  // create socket
  s = socket(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP);
  if (INVALID_SOCKET == s)
    switch (WSAGetLastError()) {
      case WSAENETDOWN, WSAEAFNOSUPPORT, WSAEINPROGRESS, WSAEMFILE, WSAENOBUFS,
           WSAEPROTONOSUPPORT, WSAEPROTOTYPE, WSAESOCKTNOSUPPORT :
           exterror = err;
           return CHATLIB_INIT_SOCKETERROR;
      default : s = err;
         break;
    };
  reporterror();

  // bind socket
  char myname[256];
  struct hostent *hp;

  memset(&socketdata, 0, sizeof(struct sockaddr_in)); /* clear our address */
  gethostname(myname, sizeof(myname));                /* who are we? */
  hp = gethostbyname(myname);  /* get our address info */
  if (hp == NULL)              /* we don't exist !? */
    return CHATLIB_INIT_BADHOSTNAME;
  socketdata.sin_family = hp->h_addrtype;     /* this is our host address */
  socketdata.sin_port = htons(defaultport);   /* this is our port number */

  strcpy(hostname, hp->h_name);

  /* bind the socket to the internet address */
  if (bind(s, (struct sockaddr *)&socketdata, sizeof(struct sockaddr_in)) ==
      SOCKET_ERROR) {
    MessageBox(NULL, "Couldn't bind socket!", "CHATLIB", MB_OK);
    exterror = WSAGetLastError();
    return CHATLIB_INIT_BINDERROR;
  };
  reporterror();

  // enable broadcasting on the socket!
  int isbroadcast = 250;
  err = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (const char far *) &isbroadcast, sizeof(isbroadcast));
  reporterror();
  if (err == SOCKET_ERROR) {
    MessageBox(NULL, "Couldn't enable broadcasting!", "CHATLIB", MB_OK);
    exterror = WSAGetLastError();
    return CHATLIB_INIT_CANTSETOPT;
  };


  // connect socket
  struct sockaddr_in sa;
  u_int ba = INADDR_BROADCAST;

  memset(&sa,0,sizeof(sa));
  memcpy((char *)&sa.sin_addr, &ba, hp->h_length);   /* set address */
  sa.sin_family = hp->h_addrtype;
  sa.sin_port = htons((u_short)defaultport);

  err = connect(s, (struct sockaddr *)&sa, sizeof sa);
  reporterror();


or, has anyone got any pointers / help on how to achieve this?
LVL 1
shiversAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tonpCommented:
Your trying to set up a permanent connection (using connect() on on the client side and listen() on the server side. NB: you can also use sendto (client) and recvfrom (server) with dgrams)
Anyway, in both cases (on both sockets that is) use INADDR_ANY as the address to bind to, do not use the address INADDR_BROADCAST.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shiversAuthor Commented:
lovely - I shall give that a try, but just to clarify (and if anyone wants to add any further points), the original idea of using broadcast dgrams was to avoid the use of a server (I don't know whether the client/server terminology still applies), but we  (in our LAN) don't have a dedicated server which is guaranteed to be online - hence the creation of a chat program that doesn't require one.

I have seen a few around on the net - but none that allow more than two people to chat in a single room - this should (theoretically) allow any number - but of course there will be limitations...   ...which reminds me , does anyone know if there's a way of increasing the max. number of recieved packets in the buffer (from 5) to a nice high number like 20-30...


0
tonpCommented:
The client-server terminology does not relate to the network topology of a single server hosting services (disk space, printing, etc) to clients (workstations). Instead, for this kind of network programming the client-server specify a certain piece of _software_ that is either client or server (the server offers some kind of service to the client, like a DBMS, faxing capabilities, whatever). In your case, the program should act both as client and as server, meaning that you should be able (possible in multiple threads) to accept incoming connections (listen()) or set up new connections. You can set up (or accept) as many connections as you want from a single application. With a chat-like utility is probably still easier to work 'connection-less' using sendto and recvfrom but it doesn't make a great deal of difference.
I'm not sure what you mean with the max of 5 packets. I don't think there's a limitation like that...

Good luck.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.