最简单的web服务器实例

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

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

static void Main(string[] args) 

    // 定义IP地址  
    IPAddress address = IPAddress.Loopback; 
    IPEndPoint endPoint = new IPEndPoint(address, 50000); 
    // 创建socket对象  
    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
    // bind  
    socket.Bind(endPoint); 
    // listen  
    socket.Listen(10); 
    Console.WriteLine("开始监听,端口号:{0}", endPoint.Port); 
    while (true) 
    { 
        // accept  
        Socket clientSocket = socket.Accept(); 
        Console.WriteLine("建立了一个连接,对方端点{0}", clientSocket.RemoteEndPoint); 
 
        // 开始接受对方的信息,并显示  
        byte[] buffer = new byte[4096]; 
        int length = clientSocket.Receive(buffer, 4096, SocketFlags.None); 
        System.Text.Encoding utf8 = System.Text.Encoding.UTF8; 
        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);//  回应头  
        //  发送消息  
        clientSocket.Send(statusLineBytes); 
        clientSocket.Send(responseHeaderBytes); 
        clientSocket.Send(new byte[] { 13, 10 }); 
        clientSocket.Send(responseBodyBytes); 
        // 断开与客户端的连接  
        clientSocket.Close(); 
        if (Console.KeyAvailable) 
        { 
            break; 
        }           
    } 
    socket.Close(); 

        static void Main(string[] args)
        {
            // 定义IP地址 www.2cto.com
            IPAddress address = IPAddress.Loopback;
            IPEndPoint endPoint = new IPEndPoint(address, 50000);
            // 创建socket对象
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // bind
            socket.Bind(endPoint);
            // listen
            socket.Listen(10);
            Console.WriteLine("开始监听,端口号:{0}", endPoint.Port);
            while (true)
            {
                // accept
                Socket clientSocket = socket.Accept();
                Console.WriteLine("建立了一个连接,对方端点{0}", clientSocket.RemoteEndPoint);

                // 开始接受对方的信息,并显示
                byte[] buffer = new byte[4096];
                int length = clientSocket.Receive(buffer, 4096, SocketFlags.None);
                System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
                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);//  回应头
                //  发送消息
                clientSocket.Send(statusLineBytes);
                clientSocket.Send(responseHeaderBytes);
                clientSocket.Send(new byte[] { 13, 10 });
                clientSocket.Send(responseBodyBytes);
                // 断开与客户端的连接
                clientSocket.Close();
                if (Console.KeyAvailable)
                {
                    break;
                }         
            }
            socket.Close();
        }

2.运行该工程

3.打开浏览器在地址栏中输入http://localhost:50000/  ,用httpwatch可大概了解浏览器与服务器的简要流程\



 摘自  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