Thread overview
Listening to UDP sockets
Feb 23, 2007
Burton Radons
Feb 23, 2007
Burton Radons
Feb 24, 2007
Howard Berkey
February 23, 2007
I have this code:

	import std.socket;

	void main ()
	{
		auto address = new InternetAddress (3333);
		auto socket = new UdpSocket ();
		socket.bind (address);
		socket.listen (10);
	}

Which fails during execution on the "listen" line with "Error: Unable to listen on socket". Can anyone help? Replacing "Udp" with "Tcp" lets it work properly so perhaps this is a bug in "std.socket".
February 23, 2007
Burton Radons wrote:
> I have this code:
> 
>     import std.socket;
> 
>     void main ()
>     {
>         auto address = new InternetAddress (3333);
>         auto socket = new UdpSocket ();
>         socket.bind (address);
>         socket.listen (10);
>     }
> 
> Which fails during execution on the "listen" line with "Error: Unable to listen on socket". Can anyone help? Replacing "Udp" with "Tcp" lets it work properly so perhaps this is a bug in "std.socket".

I figured it out - I just have to bind (no listen) and then the receive buffer must be large enough to contain the entire message or it either never returns (since it's waiting for a datagram which is small enough to fit, I guess) or returns -1 in nonblocking mode. I knew there were no socket connections in the TCP sense but I was expecting sockets to simulate it in order to have one unified API.
February 24, 2007

Burton Radons Wrote:

> Burton Radons wrote:
> > I have this code:
> > 
> >     import std.socket;
> > 
> >     void main ()
> >     {
> >         auto address = new InternetAddress (3333);
> >         auto socket = new UdpSocket ();
> >         socket.bind (address);
> >         socket.listen (10);
> >     }
> > 
> > Which fails during execution on the "listen" line with "Error: Unable to listen on socket". Can anyone help? Replacing "Udp" with "Tcp" lets it work properly so perhaps this is a bug in "std.socket".
> 
> I figured it out - I just have to bind (no listen) and then the receive buffer must be large enough to contain the entire message or it either never returns (since it's waiting for a datagram which is small enough to fit, I guess) or returns -1 in nonblocking mode. I knew there were no socket connections in the TCP sense but I was expecting sockets to simulate it in order to have one unified API.

The D API reflects the traditional sockets API here.

There is a really good guide to sockets programming online at:

http://beej.us/guide/bgnet/

Much of what is in that guide is directly applicable to D network programming.

Howard