www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Listening to UDP sockets

reply Burton Radons <burton-radons smocky.com> writes:
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".
Feb 23 2007
parent reply Burton Radons <burton-radons smocky.com> writes:
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.
Feb 23 2007
parent Howard Berkey <howard well.com> writes:
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
Feb 24 2007