Solved

Problems with broadcast UDP sockets through winsock

Posted on 1998-04-06
3
559 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:shivers
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
tonp earned 200 total points
ID: 1184080
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
 
LVL 1

Author Comment

by:shivers
ID: 1184081
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
 
LVL 2

Expert Comment

by:tonp
ID: 1184082
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now