www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.socket with GDC

reply Joseph Bell <josephabell tx.rr.com> writes:
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");
   }

}
Jan 23 2007
next sibling parent kris <foo bar.com> writes:
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
Jan 23 2007
prev sibling parent reply "Chris Miller" <chris dprogramming.com> writes:
On Wed, 24 Jan 2007 01:09:52 -0500, Joseph Bell <josephabell tx.rr.com> =
 =

wrote:
[snip]

    ubyte[] buf;
    Address receiveAddress;
    int bytes;

    bytes =3D sock.receiveFrom(buf, receiveAddress);

    if (bytes) {
      writefln("Received %d bytes", bytes);
    } else {
      writefln("Error or nothing received");
    }

 }
Give it some memory to write into... buf =3D new ubyte[1000]; and then buf[0 .. bytes] is valid, assuming no errors.
Jan 23 2007
parent Joseph Bell <josephabell tx.rr.com> writes:
Thanks Chris - that indeed was the trick - I haven't delved deep enough 
into D to know if my expectation of being able to pass an unbounded 
array has any merit.  Suffice it to say, this works - thank you.

Chris Miller wrote:
 On Wed, 24 Jan 2007 01:09:52 -0500, Joseph Bell <josephabell tx.rr.com> 
 wrote:
 [snip]
 
    ubyte[] buf;
    Address receiveAddress;
    int bytes;

    bytes = sock.receiveFrom(buf, receiveAddress);

    if (bytes) {
      writefln("Received %d bytes", bytes);
    } else {
      writefln("Error or nothing received");
    }

 }
Give it some memory to write into... buf = new ubyte[1000]; and then buf[0 .. bytes] is valid, assuming no errors.
Jan 24 2007