Datagram Sockets


#1

I’m having some trouble using the DatagramSocket class (is this new? I know I looked for this a while ago with no luck). All I want to do right now is create one, bind to some port, and wait until something connects.

servSock = new DatagramSocket(30000); DatagramSocket* sock = servSock->waitForNextConnection();
When I run this, waitForNextConnection() returns immediately instead of waiting as it should. After some debugging, it seems that bind() is failing, with a WSAENOTSOCK error. I don’t see a valid socket ever being created, and handle is set to -1. So what’s going on?


#2

Well, I’ve made no progress with getting DatagramSocket to work, so I just ended up rolling my own Winsock-based class instead.

Now that I think about it, though, I’m not even sure why there is a waitForNextConnection() method when UDP is connectionless in the first place. All I needed to do in my class was create a socket, bind it, and poll until there’s stuff to read.


#3

If you want to log any connection attempt, you need to monitor/select the socket.

Sure UDP is connection less, so waitForNextConnection() only does a “isReadable” polling, and when a sending is done on that port, it finds out who’s sent that packet.

After looking at the code, it seems like it’s missing a line in the DatagramSocket class constructor.
You can add :
handle = (int) socket (AF_INET, SOCK_DGRAM, 0);
in DatagramSocket::DatagramSocket(const int localPortNumber)

The basic idea of Juce socket is that it doesn’t create a (OS) socket until it’s necessary (so creating Juce’s Socket is cheap).
However, binding a socket requires it’s created, which was not the case if you call the methods that way.

Jules, I’ve missed that line in

  478 DatagramSocket::DatagramSocket (const int localPortNumber)
  479     : portNumber (0),
  480       handle (-1),
  481       connected (false),
  482       serverAddress (0)
  483 {
  484 #if JUCE_WIN32
  485     initWin32Sockets();
  486 #endif
  487 
  488     bindToPort (localPortNumber);
  489 }

which should read:

  478 DatagramSocket::DatagramSocket (const int localPortNumber)
  479     : portNumber (0),
  480       handle (-1),
  481       connected (false),
  482       serverAddress (0)
  483 {
  484 #if JUCE_WIN32
  485     initWin32Sockets();
  486 #endif
  487     handle = (int) socket (AF_INET, SOCK_DGRAM, 0);
  488     bindToPort (localPortNumber);
  489 }

and also need to change:

246     if (handle < 0) handle = (int) socket (AF_INET, isDatagram ? SOCK_DGRAM : SOCK_STREAM, 0);

so it’s possible to actually specify both the client port and the server port in Datagram.

I guess I haven’t enough tested the code as UDP server…


#4

Ok, thanks for the tip there - I’ll get that checked-in…


#5