Solved

Problems with broadcast UDP sockets through winsock

Posted on 1998-04-06
3
560 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 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

19 Experts available now in Live!

Get 1:1 Help Now