简单的基于TcpListener的Web服务器实例

来源:未知 责任编辑:智问网络 发表时间:2013-11-08 08:46 点击:

1.新建控制台工程,代码如下

static void Main(string[] args) 

    IPAddress address = IPAddress.Loopback; 
    IPEndPoint endPoint = new IPEndPoint(address, 50000); 
    TcpListener newServer = new TcpListener(endPoint); 
    newServer.Start(10); 
    Console.WriteLine("开始监听。。。"); 
    while (true) 
    { 
        TcpClient newClient = newServer.AcceptTcpClient(); 
        Console.WriteLine("建立了一个连接"); 
        NetworkStream ns = newClient.GetStream(); 
        System.Text.Encoding utf8 = System.Text.Encoding.UTF8; 
        byte [] buffer = new byte [4096]; 
        int length = ns.Read(buffer, 0, 4096); 
        string requestString = utf8.GetString(buffer, 0, length);         // 接收请求信息  
        Console.WriteLine(requestString); 
 
        // 发送响应信息  
        string statusLine = "HTTP/1.1 200 OK\r\n"; 
        byte[] statusLineBytes = utf8.GetBytes(statusLine); // 状态行  
        string responseBody = "<html><head><title>response server</title></head><body>hello world!</body></html>"; 
        byte[] responseBodyBytes = utf8.GetBytes(responseBody);//   内容部分  
        string responseHeader = String.Format("Content-Type: text/html;charset=UTF-8\r\nContent-Length:{0}\r\n", 
                                                                                responseBody.Length); 
        byte[] responseHeaderBytes = utf8.GetBytes(responseHeader);//  回应头  
 
        //输出回应信息  
        ns.Write(statusLineBytes,0, statusLineBytes.Length); 
        ns.Write(responseHeaderBytes, 0, responseHeaderBytes.Length); 
        ns.Write(new byte[] { 13, 10 }, 0, 2); 
        ns.Write(responseBodyBytes, 0, responseBodyBytes.Length); 
 
        newClient.Close(); 
        if (Console.KeyAvailable) 
        { 
            break; 
        } 
    } 
    newServer.Stop(); 

        static void Main(string[] args)
        {
            IPAddress address = IPAddress.Loopback;
            IPEndPoint endPoint = new IPEndPoint(address, 50000);
            TcpListener newServer = new TcpListener(endPoint);
            newServer.Start(10);
            Console.WriteLine("开始监听。。。");
            while (true)
            {
                TcpClient newClient = newServer.AcceptTcpClient();
                Console.WriteLine("建立了一个连接");
                NetworkStream ns = newClient.GetStream();
                System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
                byte [] buffer = new byte [4096];
                int length = ns.Read(buffer, 0, 4096);
                string requestString = utf8.GetString(buffer, 0, length);         // 接收请求信息
                Console.WriteLine(requestString);

                // 发送响应信息
                string statusLine = "HTTP/1.1 200 OK\r\n";
                byte[] statusLineBytes = utf8.GetBytes(statusLine); // 状态行
                string responseBody = "<html><head><title>response server</title></head><body>hello world!</body></html>";
                byte[] responseBodyBytes = utf8.GetBytes(responseBody);//   内容部分
                string responseHeader = String.Format("Content-Type: text/html;charset=UTF-8\r\nContent-Length:{0}\r\n",
                                                                                        responseBody.Length);
                byte[] responseHeaderBytes = utf8.GetBytes(responseHeader);//  回应头

                //输出回应信息 www.2cto.com
                ns.Write(statusLineBytes,0, statusLineBytes.Length);
                ns.Write(responseHeaderBytes, 0, responseHeaderBytes.Length);
                ns.Write(new byte[] { 13, 10 }, 0, 2);
                ns.Write(responseBodyBytes, 0, responseBodyBytes.Length);

                newClient.Close();
                if (Console.KeyAvailable)
                {
                    break;
                }
            }
            newServer.Stop();
        }
2.运行该工程

3.效果图如下

 \



 摘自 hi_dzj的专栏

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • asp.net 设置GridView的选中行的实现代码
    • C#高级编程:数据库连接[1]
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • Asp.net MVC源码分析--Action Filter的链式调用
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1