std.socket with GDC

kris foo at bar.com
Tue Jan 23 22:15:08 PST 2007


Joseph Bell wrote:
> Howdy.
> 
> I'm currently using gdc (should I switch to dmd? What would that provide 
> me?)
> 
> and the following should be functional but it isn't:
> 
> I create a UDP socket, Address family is INET, I create the address as 
> my loopback on the given port.
> 
> I bind.  Blocking returns true, isAlive returns true.  So I'm blocking, 
> I'm alive, and then I hit receiveFrom which should block until I have 
> something to read.  That falls right through.  :-(
> 
> I searched high and low for working examples of client-server programs 
> (you can refer me to those too if they exist) to no avail.  I sure hope 
> this is a boneheaded mistake on my part.
> 
> Thanks for any insight,
> Joe
> 
> import std.socket;
> import std.stdio;
> 
> void main() {
> 
>   UdpSocket       sock     = new UdpSocket(AddressFamily.INET);
>   InternetAddress sockAddr = new InternetAddress("127.0.0.1", 3001);
> 
>   writefln("%s", sockAddr.toString());
> 
>   sock.bind(sockAddr);
> 
>   if (sock.blocking()) {
>     writefln("Socket is blocking");
>   }
>   if (sock.isAlive()) {
>     writefln("Socket alive");
>   }
> 
>   ubyte[] buf;
>   Address receiveAddress;
>   int bytes;
> 
>   bytes = sock.receiveFrom(buf, receiveAddress);
> 
>   if (bytes) {
>     writefln("Received %d bytes", bytes);
>   } else {
>     writefln("Error or nothing received");
>   }
> 
> }

Joseph,

Here's a great resource for you: 
http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html

- Kris



More information about the Digitalmars-d mailing list