Problem in C #

1

Does anyone know what the problem is with this code below? You are giving the following error: "The stream is not writable"

     TcpListener tcpl = new TcpListener(500);
        tcpl.Start();
        Socket sock = tcpl.AcceptSocket();
        NetworkStream stream = new NetworkStream(sock);
        BinaryWriter write;


        while (true)
            {


            string Resp = string.Format("{0}\n{1}\n{2}",
                "HTTP/1.1 890 FOUND",
                "",
                "<h1>Worked!</h1>"
                );

            write = new BinaryWriter(stream);
            write.Write(Resp);
            write.Close();
            }
        tcpl.Stop();
        sock.Close();
        stream.Close();
    
asked by anonymous 04.07.2015 / 21:22

1 answer

0

When you run the write.Close(); command, it closes the NetworkStream stream feature and therefore can not write. Try:

using (BinaryWriter writer = new BinaryWriter(stream))){
    writer.Write(Resp);
}

or

using (BinaryWriter writer = new BinaryWriter(stream, Encoding.Default, true))){
    writer.Write(Resp);
}
    
04.07.2015 / 22:04