Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#http响应_C# - Fatal编程技术网

c#http响应

c#http响应,c#,C#,我正在努力学习和编写这部分代码 虽然在它按我所希望的方式运行前几周进行了测试,但它无法使用msdn中的代码,而且http响应部分是由feroz建议的 现在过了一会儿,它就不起作用了 我本来希望通过使用(StreamWriter sw=newstreamwriter(stream)){sw.Write(“hello there!”);}在代码中获得hello there消息,但它只显示get\ try { // Set the TcpListener on

我正在努力学习和编写这部分代码

虽然在它按我所希望的方式运行前几周进行了测试,但它无法使用msdn中的代码,而且http响应部分是由feroz建议的

现在过了一会儿,它就不起作用了

我本来希望通过使用
(StreamWriter sw=newstreamwriter(stream)){sw.Write(“hello there!”);}
在代码中获得hello there消息,但它只显示get\

     try
     {

        // Set the TcpListener on port 13000.
            Int32 port = 80;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            TcpListener server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;

            // Enter the listening loop.
            while (true)
            {
                Console.Write("Waiting for a connection... ");

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");

                data = null;

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Console.WriteLine(String.Format("Received: {0}", data));

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    Console.WriteLine("Sending message..");


    using(StreamWriter sw = new StreamWriter(stream))
    {
        sw.Write("<html><body>Hello There!</body></html>");
    }

                 }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }

        Console.WriteLine("\nHit enter to continue...");
        Console.Read();
    }
}
试试看
{
//在端口13000上设置TcpListener。
Int32端口=80;
IPAddress localAddr=IPAddress.Parse(“127.0.0.1”);
//TcpListener服务器=新的TcpListener(端口);
TcpListener服务器=新的TcpListener(localAddr,port);
//开始侦听客户端请求。
server.Start();
//用于读取数据的缓冲区
字节[]字节=新字节[256];
字符串数据=null;
//进入监听循环。
while(true)
{
控制台。写入(“等待连接…”);
//执行阻塞调用以接受请求。
//您还可以在此处使用server.AcceptSocket()。
TcpClient client=server.AcceptTcpClient();
控制台。WriteLine(“已连接!”);
数据=空;
//获取用于读写的流对象
NetworkStream=client.GetStream();
int i;
//循环以接收客户端发送的所有数据。
而((i=stream.Read(bytes,0,bytes.Length))!=0)
{
//将数据字节转换为ASCII字符串。
数据=System.Text.Encoding.ASCII.GetString(字节,0,i);
WriteLine(String.Format(“Received:{0}”,data));
//处理客户端发送的数据。
data=data.ToUpper();
byte[]msg=System.Text.Encoding.ASCII.GetBytes(数据);
//发回回复。
stream.Write(msg,0,msg.Length);
Console.WriteLine(“发送消息…”);
使用(StreamWriter sw=新StreamWriter(流))
{
写下“你好!”;
}
}
//停机和端部连接
client.Close();
}
}
捕获(SocketException e)
{
WriteLine(“SocketException:{0}”,e);
}
Console.WriteLine(“\n输入以继续…”);
Console.Read();
}
}

问题中显示的代码在第一次从网络读取后将抛出ObjectDisposedException。您所做的“错误”是处理StreamWriter(隐含在using语句中),它处理底层网络流。一旦处理完毕,你就不能再回去阅读了。此外,您将混合直接向流写入和通过(缓冲)StreamWriter写入

这个问题应该重新措辞如下:

**

为什么这段代码要读/写数据 从客户端发送一次,然后抛出一个 第二次读取尝试时出现异常

**

我将重组代码,如下所示。[注意:实际上并不需要Flush()调用,但您可能希望在演示中使用它。]

try
{
    // Listen for connections on port 13000
    TcpListener server = new TcpListener(IPAddress.Loopback, 13000);
    server.Start();

    // Read up tp 256 bytes at a time 
    Byte[] bytes = new Byte[256];
    String data;

    // Enter the listening loop. 
    while (true)
    {
        Console.WriteLine("Waiting for a connection... ");

        // Wait for a client connection
        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("Connected!");

        // Setup I/O streams
        NetworkStream stream = client.GetStream();
        using (StreamWriter sw = new StreamWriter(stream))
        {
            int i;

            // Loop to receive all the data sent by the client. 
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                // Translate data bytes to a ASCII string. 
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                Console.WriteLine(String.Format("Received: {0}", data));

                // Process the data sent by the client. 
                data = data.ToUpper();

                // Send back a response. 
                Console.WriteLine("Sending message..");
                sw.Write(data);

                // Add a little extra 'response'
                sw.Write("<html><body>Hello There!</body></html>");
                sw.Flush();
            }
        }
        // Close connection 
        client.Close();
    }
}
catch (SocketException e)
{
    Console.WriteLine("SocketException: {0}", e);
}
试试看
{
//侦听端口13000上的连接
TcpListener服务器=新的TcpListener(IPAddress.Loopback,13000);
server.Start();
//一次读取256字节的tp
字节[]字节=新字节[256];
字符串数据;
//进入监听循环。
while(true)
{
Console.WriteLine(“等待连接…”);
//等待客户端连接
TcpClient client=server.AcceptTcpClient();
控制台。WriteLine(“已连接!”);
//设置I/O流
NetworkStream=client.GetStream();
使用(StreamWriter sw=新StreamWriter(流))
{
int i;
//循环以接收客户端发送的所有数据。
而((i=stream.Read(bytes,0,bytes.Length))!=0)
{
//将数据字节转换为ASCII字符串。
数据=System.Text.Encoding.ASCII.GetString(字节,0,i);
WriteLine(String.Format(“Received:{0}”,data));
//处理客户端发送的数据。
data=data.ToUpper();
//发回回复。
Console.WriteLine(“发送消息…”);
软件写入(数据);
//添加一点额外的“响应”
写下“你好!”;
sw.Flush();
}
}
//密切联系
client.Close();
}
}
捕获(SocketException e)
{
WriteLine(“SocketException:{0}”,e);
}

它如何“不起作用”?发生了什么事?有例外吗?您使用什么类型的客户端连接到此服务器?为什么不工作?会发生什么?你有防火墙吗?你遇到了什么错误?没有例外,只是不能像进度条一样继续显示你需要更多的问号。谢谢你的支持。。。。但问题仍然是我的浏览器无法显示消息“Hello There”回来。。。可能是bcoz没有http头响应,但不知道如何实现。。。。总而言之,我想让我的浏览器显示html标记。如果你想让浏览器显示消息,那么你必须使用HTTP协议进行通信,而不仅仅是发回一些html。请参阅web上的HTTP协议文档;写一个web服务器,在internet上搜索如何完成并不难。