error when using TcpSocket

zsxxsz zhengshuxin at hexun.com
Sat May 16 07:50:06 PDT 2009


I wrote one code as below:

import std.c.string;
import core.stdc.errno;
import std.stdio;
import std.socket;
import std.stream;
import std.socketstream;

int test_socket(string addr, ushort port)
{
        Address address = new InternetAddress(addr, port);
        Socket sock = new TcpSocket(address);
        Stream ss = new SocketStream(sock);

        string host = "127.0.0.1";

        writefln("socket: %d", sock.handle());

        try {
                /*
                int ret = ss.printf(cast(char[]) "GET / HTTP/1.0\r\n"
                        "Host: %s\r\n"
                        "Connection: close\r\n"
                        "\r\n", host.toStringz);
                        */
                ss.writeString("Get / HTTP/1.0\r\n\r\n");
        } catch (Exception e) {
                printf("error: %s\n", strerror(errno));
                throw e;
        }

        while (!ss.eof) {
                char[] line;

                try {
                        line = ss.readLine();
                } catch (Exception e) {
                        printf("error: %s\n", strerror(errno));
                        throw e;
                }
                writefln(">>%s", line);
        }

        return (0);
}

int main(string[] args)
{
        test_socket("127.0.0.1", 80);
        return (0);
}

when I run the program, I got below:
socket: 3
error: Broken pipe
std.stream.WriteException: unable to write to stream

How did this happen and why?
Thank
zsxxsz


More information about the Digitalmars-d-learn mailing list