• 大小: 120KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: C#
  • 标签: Web服务器  

资源简介

Web服务器,实现自定义的Web服务器(C#)

资源截图

代码片段和文件信息

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace WebServer
{
    class Program
    {
        static void Main(string[] args)
        {
            // 获得本机的Ip地址,即127.0.0.1
            IPAddress localaddress = IPAddress.Loopback;

            // 创建可以访问的断点,49155表示端口号,如果这里设置为0,表示使用一个由系统分配的空闲的端口号
            IPEndPoint endpoint = new IPEndPoint(localaddress 49155);

            // 创建Tcp 监听器
            TcpListener tcpListener = new TcpListener(endpoint);

            // 启动监听
            tcpListener.Start();
            Console.WriteLine(“Wait an connect Request...“);
            while (true)
            {
                // 等待客户连接
                TcpClient client = tcpListener.AcceptTcpClient();
                if (client.Connected == true)
                {
                    // 输出已经建立连接
                    Console.WriteLine(“Created connection“);
                }

                // 获得一个网络流对象
                // 该网络流对象封装了Socket的输入和输出操作
                // 此时通过对网络流对象进行写入来返回响应消息
                // 通过对网络流对象进行读取来获得请求消息
                NetworkStream netstream = client.GetStream();
                // 把客户端的请求数据读入保存到一个数组中
                byte[] buffer = new byte[2048];

                int receivelength = netstream.Read(buffer 0 2048);
                string requeststring = Encoding.UTF8.GetString(buffer 0 receivelength);

                // 在服务器端输出请求的消息
                Console.WriteLine(requeststring);

                // 服务器端做出相应内容
                // 响应的状态行
                string statusLine = “HTTP/1.1 200 OK\r\n“;
                byte[] responseStatusLineBytes = Encoding.UTF8.GetBytes(statusLine);
                string responseBody = “tle>Default Pagetle>yle=‘font:bold;font-size:24pt‘>Welcome you

“;
                string responseHeader =
                    string.Format(
                        “Content-Type: text/html; charset=UTf-8\r\nContent-Length: {0}\r\n“ responseBody.Length);

                byte[] responseHeaderBytes = Encoding.UTF8.GetBytes(responseHeader);
                byte[] responseBodyBytes = Encoding.UTF8.GetBytes(responseBody);

                // 写入状态行信息
                netstream.Write(responseStatusLineBytes 0 responseStatusLineBytes.Length);
                // 写入回应的头部
                netstream.Write(responseHeaderBytes 0 responseHeaderBytes.Length);
                // 写入回应头部和内容之间的空行
                netstream.Write(new byte[] { 13 10 } 0 2);

                // 写入回应的内容
                netstream.Write(responseBodyBytes 0 responseBodyBytes.Length);

                // 关闭与客户端的连接
                client.Close();
                Console.ReadKey();
                break;
            }

            // 关闭服务器
            tcpListener.Stop();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件       26768  2014-06-06 09:15  description.html
     文件        1670  2014-06-06 09:15  description\offline.js
     文件           0  2014-06-06 09:15  description\Combined.css
     文件           0  2014-06-06 09:15  description\b79636be-b088-48b5-8a8d-c21645d26d2fCombined.css
     文件       10457  2014-06-06 09:15  description\Galleries.css
     文件        1727  2014-06-06 09:15  description\Layout.css
     文件       71647  2014-06-06 09:15  description\Brand.css
     文件        3369  2014-06-06 09:15  description\iframedescription.css
     文件       17619  2014-06-06 09:15  description\3.png
     文件       37195  2014-06-06 09:15  description\4.png
     文件       22297  2014-06-06 09:15  description\5.png
     文件       14808  2014-06-06 09:15  license.rtf
     文件         869  2014-06-06 09:15  C#\WebServer.sln
     文件       12800  2014-06-06 09:15  C#\WebServer.suo
     目录           0  2014-06-06 09:15  C#\WebServer\
     目录           0  2014-06-06 09:15  C#\WebServer\bin\
     目录           0  2014-06-06 09:15  C#\WebServer\bin\Debug\
     文件        6144  2014-06-06 09:15  C#\WebServer\bin\Debug\WebServer.exe
     文件       13824  2014-06-06 09:15  C#\WebServer\bin\Debug\WebServer.pdb
     文件       11600  2014-06-06 09:15  C#\WebServer\bin\Debug\WebServer.vshost.exe
     文件         490  2014-06-06 09:15  C#\WebServer\bin\Debug\WebServer.vshost.exe.manifest
     目录           0  2014-06-06 09:15  C#\WebServer\obj\
     目录           0  2014-06-06 09:15  C#\WebServer\obj\x86\
     目录           0  2014-06-06 09:15  C#\WebServer\obj\x86\Debug\
     文件        5397  2014-06-06 09:15  C#\WebServer\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2014-06-06 09:15  C#\WebServer\obj\x86\Debug\TempPE\
     文件         340  2014-06-06 09:15  C#\WebServer\obj\x86\Debug\WebServer.csproj.FileListAbsolute.txt
     文件        6144  2014-06-06 09:15  C#\WebServer\obj\x86\Debug\WebServer.exe
     文件       13824  2014-06-06 09:15  C#\WebServer\obj\x86\Debug\WebServer.pdb
     文件        3376  2014-06-06 09:15  C#\WebServer\Program.cs
     目录           0  2014-06-06 09:15  C#\WebServer\Properties\
............此处省略2个文件信息

评论

共有 条评论