www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - std.socket.Socket - syscalls called with wrong arguments

reply Martin Riedel <riedel.martin gmx.net> writes:
As to my knowledge, the phobos libraries' Socket class calls the linux 
socket send and receive syscalls in the wrong way.

int send(void[] buf, SocketFlags flags)
{
     int sent = .send(sock, buf, buf.length, cast(int)flags);
     return sent;
}

Taking a void[] buf dynamic array of the data to be sent as an argument, 
Socket's send member passes buf.length to the syscall which expects its 
third argument to be the number of bytes to be sent (or in other words 
the size of buf in bytes). buf.length, though, is the number of elements 
in the array. Therefore, in every case where the original array (which 
was passed to Socket.send and has been cast to void[]) has a type which 
is bigger than one byte, .send gets wrong arguments.
Aug 30 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Martin Riedel" <riedel.martin gmx.net> wrote in message 
news:df2127$2ddr$1 digitaldaemon.com...
 As to my knowledge, the phobos libraries' Socket class calls the linux 
 socket send and receive syscalls in the wrong way.

 int send(void[] buf, SocketFlags flags)
 {
     int sent = .send(sock, buf, buf.length, cast(int)flags);
     return sent;
 }

 Taking a void[] buf dynamic array of the data to be sent as an argument, 
 Socket's send member passes buf.length to the syscall which expects its 
 third argument to be the number of bytes to be sent (or in other words the 
 size of buf in bytes). buf.length, though, is the number of elements in 
 the array. Therefore, in every case where the original array (which was 
 passed to Socket.send and has been cast to void[]) has a type which is 
 bigger than one byte, .send gets wrong arguments.
That's expected behavior. What code are you trying to write? When I try int main() { void[] va; int[] ia = new int[10]; va = ia; printf("%d %d\n",ia.length,va.length); return 0; } it prints 10 40 so the void array has the entire int array.
Aug 30 2005
parent Martin Riedel <riedel.martin gmx.net> writes:
Ben Hinkle wrote:
 "Martin Riedel" <riedel.martin gmx.net> wrote in message 
 news:df2127$2ddr$1 digitaldaemon.com...
 
As to my knowledge, the phobos libraries' Socket class calls the linux 
socket send and receive syscalls in the wrong way.

int send(void[] buf, SocketFlags flags)
{
    int sent = .send(sock, buf, buf.length, cast(int)flags);
    return sent;
}

Taking a void[] buf dynamic array of the data to be sent as an argument, 
Socket's send member passes buf.length to the syscall which expects its 
third argument to be the number of bytes to be sent (or in other words the 
size of buf in bytes). buf.length, though, is the number of elements in 
the array. Therefore, in every case where the original array (which was 
passed to Socket.send and has been cast to void[]) has a type which is 
bigger than one byte, .send gets wrong arguments.
That's expected behavior. What code are you trying to write? When I try int main() { void[] va; int[] ia = new int[10]; va = ia; printf("%d %d\n",ia.length,va.length); return 0; } it prints 10 40 so the void array has the entire int array.
My fault. I'm quite new to D and I didn't expect void to have a defined size (which is 1, apparently). Furthermore, I didn't know that casting arrays changes the length of the lvalue array. Thanks for your hint.
Aug 30 2005