Solved

Problems with broadcast UDP sockets through winsock

Posted on 1998-04-06
3
564 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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