Thread overview
std.socket with GDC
Jan 24, 2007
Joseph Bell
Jan 24, 2007
kris
Jan 24, 2007
Chris Miller
Jan 25, 2007
Joseph Bell
January 24, 2007
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");
  }

}
January 24, 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
January 24, 2007
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.
January 25, 2007
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.