Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 07, 2008 [gdc-0.24_3 on freebsd8current/amd64 ]can't run socket.connect | ||||
---|---|---|---|---|
| ||||
import std.socket; import std.socketstream; int main() { Socket sock = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); sock.connect(new InternetAddress("www.digitalmars.com", 80)); SocketStream ss = new SocketStream(sock); ss.writeString("GET /d/intro.html HTTP/1.1\r\n" "Host: www.digitalmars.com\r\n" "\r\n"); while(ss.readLine().length) {} //skip header while(!ss.eof()) { char[] line; printf("%.*s\n", ss.readLine()); } return 0; } ---------------------- I try to run this. I find it compile well.but when I run it., it core dump. |
June 08, 2008 Works fine here. | ||||
---|---|---|---|---|
| ||||
Posted in reply to kevin | (see subject) |
June 08, 2008 Re: [gdc-0.24_3 on freebsd8current/amd64 ]can't run socket.connect | ||||
---|---|---|---|---|
| ||||
Posted in reply to kevin | kevin wrote:
> import std.socket;
> import std.socketstream;
>
> int main()
> {
> Socket sock = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP);
> sock.connect(new InternetAddress("www.digitalmars.com", 80));
> SocketStream ss = new SocketStream(sock);
>
> ss.writeString("GET /d/intro.html HTTP/1.1\r\n"
> "Host: www.digitalmars.com\r\n"
> "\r\n");
>
> while(ss.readLine().length) {} //skip header
> while(!ss.eof())
> {
> char[] line;
> printf("%.*s\n", ss.readLine());
> }
>
> return 0;
> }
> ----------------------
> I try to run this. I find it compile well.but when I run it., it core dump.
Although the D spec says you can pass a string for a "%.*s" format, it really only works on 32-bit targets. The argument for '*' is of type 'int', not 'size_t'. On a 64-bit target, passing a char[] really passes a 'size_t' and 'char*', which is not what printf expects.
To be portable, you have to do this:
string s2 = ss.readline();
printf("%.*s\n", cast(int) s2.length, s2.ptr);
David
|
June 08, 2008 Re: [gdc-0.24_3 on freebsd8current/amd64 ]can't run socket.connect | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Friedman | David Friedman Wrote: > kevin wrote: > > import std.socket; > > import std.socketstream; > > > > int main() > > { > > Socket sock = new Socket(AddressFamily.INET, SocketType.STREAM, > > ProtocolType.IP); > > sock.connect(new InternetAddress("www.digitalmars.com", 80)); > > SocketStream ss = new SocketStream(sock); > > > > ss.writeString("GET /d/intro.html HTTP/1.1\r\n" > > "Host: www.digitalmars.com\r\n" > > "\r\n"); > > > > while(ss.readLine().length) {} //skip header > > while(!ss.eof()) > > { > > char[] line; > > printf("%.*s\n", ss.readLine()); > > } > > > > return 0; > > } > > ---------------------- > > I try to run this. I find it compile well.but when I run it., it core dump. > > Although the D spec says you can pass a string for a "%.*s" format, it really only works on 32-bit targets. The argument for '*' is of type 'int', not 'size_t'. On a 64-bit target, passing a char[] really passes a 'size_t' and 'char*', which is not what printf expects. > > To be portable, you have to do this: > > string s2 = ss.readline(); > printf("%.*s\n", cast(int) s2.length, s2.ptr); > > David Sorry, I mean socket.connect can't work in freebsd 8/amd64. ........ writefln("Befor"); sock.connect(new InternetAddress("www.digitalmars.com", 80)); writefln("After"); I test this ,it just output "Befor" and then core dump. (gdb)s _D3gcx2GC8addRangeMFPvPvZv (this=@0x60e040, pbot=0x5386a8, ptop=0x551980) at ../.././../gcc-4.1-20071105/libphobos/internal/gc/gcx.d:751 751 in ../.././../gcc-4.1-20071105/libphobos/internal/gc/gcx.d (gdb) s _D3gcx2GC8addRangeMFPvPvZv (this=Variable "this" is not available. ) at ../.././../gcc-4.1-20071105/libphobos/internal/gc/gcx.d:748 748 in ../.././../gcc-4.1-20071105/libphobos/internal/gc/gcx.d (gdb) s Befor Program received signal SIGABRT, Aborted. 0x0000000800b7947c in kill () from /lib/libc.so.7 |
Copyright © 1999-2021 by the D Language Foundation