软件架构师何志丹

本程序仅仅是入门级程序。所以不考虑

1。多线程。

2,安全性。

3,不考虑端点下载文件。

4,Keep-Alive。

5,不考虑head。

6,为了简洁,删掉了catch的内容。



exe的祖父目录必须有wwwroot目录,且目录有index.htm,内容不限。 

开发环境: WinXP+VS2010C#



一。新建一个项目TestWeb。项目类型:Windows窗体应用程序。

二。新建类RequestProcessor。

 using System;

using System.IO;

using System.Net;

using System.Net.Sockets;

using System.Text;

using System.Threading;

using System.Diagnostics;



namespace TestWeb

{

    class RequestProcessor

    {

        public bool ParseRequestAndProcess(string[] RequestLines)//解析内容

        {

            for (int i = 0; i < RequestLines.Length; i++)

                System.Diagnostics.Trace.Write(RequestLines[i]);



            char[] sp = new Char[1] { ' ' };

            string[] strs = RequestLines[0].Split(sp);

            if (strs[0] == "GET")

            {

                Send(strs[1], 0, 0);

            }



            return false;

        }



        void Send(string filename, long start, long length)//发送文件(文件头和文件)

        {

            string strFileName = GetPathFileName(filename);

            FileStream fs = null;

            try

            {

                fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);



            }

            catch (IOException)// FileNotFoundException)

            {//不能将 e.Message,发给浏览器,否则会有安全隐患的

                SendHeadrAndStr("打开文件" + filename + "失败。

");

                return;

            }



            if (length == 0)

                length = fs.Length - start;



            SendHeader("text/html", (fs.Length == length), start, length);

            sendContent(fs, start, length);

        }



        public void SendHeadrAndStr(String str)//直接将str的内容发给html

        {

            byte[] sendchars = Encoding.Default.GetBytes((str).ToCharArray());

            SendHeader("text/html", true, 0, sendchars.Length);

            SendStr(Encoding.Default, str);

        }



        private void SendHeader(string fileType, bool bAll, long start, long length)//发送文件头

        {

            try

            {

                Encoding coding = Encoding.Default;

                string strSend;

                string strState = (bAll) ?

"HTTP/1.1 200 OK" : "HTTP/1.1 206 Partial Content";

                SendStr(coding, strState + "\r\n");

                SendStr(coding, "Date: \r\n");

                SendStr(coding, "Server: httpsrv/1.0\r\n");

                SendStr(coding, "MIME-Version: 1.0\r\n");

                SendStr(coding, "Content-Type: " + fileType + "\r\n");





                strSend = "Content-Length: " + length.ToString();

                SendStr(coding, strSend + "\r\n");



                //发送一个空行

                SendStr(coding, "\r\n");

            }

            catch (ArgumentException)//the request is WRONG

            {





            }



        }



        private void sendContent(FileStream fs, long start, long length)//发生文件内容

        {

            try

            {



                //报文头发送完成,開始发送正文

                const int SOCKETWINDOWSIZE = 8192;

                long r = SOCKETWINDOWSIZE;

                int rd = 0;

                Byte[] senddatas = new Byte[SOCKETWINDOWSIZE];

                fs.Seek(start, SeekOrigin.Begin);

                do

                {

                    r = start + length - fs.Position;

                    //fs.BeginRead(s,s,s,s,d) 以后使用的版本号。用以提高读取的效率                

                    if (r >= SOCKETWINDOWSIZE)

                        rd = fs.Read(senddatas, 0, SOCKETWINDOWSIZE);

                    else

                        rd = fs.Read(senddatas, 0, (int)r);

                    mSockSendData.Send(senddatas, 0, rd, SocketFlags.None);

                } while (fs.Position != start + length);



            }

            catch (SocketException e)

            {

                throw e;

            }

            catch (IOException e)

            {

                throw e;

            }

        }







        public Socket mSockSendData;//Notice: get from ClientSocketThread.s











        private string GetPathFileName(string filename)

        {

            const string strDefaultPage = "index.htm";

            const string strWWWRoot = "..\\..\\wwwroot\\";

            string strFileName = String.Copy(filename);

            if ("/" == strFileName)

                strFileName = strDefaultPage;

            return System.AppDomain.CurrentDomain.BaseDirectory + strWWWRoot + strFileName;

        }



        private void SendStr(Encoding coding, string strSend)//发送一个字符串

        {

            Byte[] sendchars = new Byte[512];

            sendchars = coding.GetBytes((strSend).ToCharArray());

            mSockSendData.Send(sendchars, 0, sendchars.Length, SocketFlags.None);

        }

    }

}



三,新建类ClientSocketThread。

using System;

using System.Net;

using System.Net.Sockets;

using System.Text;

using System.Threading;



namespace TestWeb

{

    class ClientSocketThread

    {

        public TcpListener tcpl;//Notice: get from SrvMain.tcpl

        private static Encoding ASCII = Encoding.ASCII;



        public void HandleThread()

        {

            Thread currentThread = Thread.CurrentThread;

            try

            {



                Socket s = tcpl.AcceptSocket();





                RequestProcessor aRequestProcessor = new RequestProcessor(); //Notice:

                aRequestProcessor.mSockSendData = s;//Notice: so that the processor can work





                const int BUFFERSIZE = 4096;//that's enough???

Byte[] readclientchar = new Byte[BUFFERSIZE];

                char[] sps = new Char[2] { '\r', '\n' };

                string[] RequestLines = new string[32];



                do

                {

                    //use BUFFERSIZE contral the receive data size to avoid the BufferOverflow attack

                    int rc = s.Receive(readclientchar, 0, BUFFERSIZE, SocketFlags.None);



                    string strReceive = ASCII.GetString(readclientchar, 0, rc);



                    RequestLines = strReceive.Split(sps);





                } while (aRequestProcessor.ParseRequestAndProcess(RequestLines));



                s.Close();

            }

            catch (SocketException)

            {



            }

        }





    }

}





四。主对话框中添加button,按键的对应函数加例如以下代码。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



using System.IO;

using System.Net;

using System.Net.Sockets;

using System.Threading;



namespace TestWeb

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

              try

            {

                //启动监听程序                

                TcpListener tcpl;

                IPAddress LocalIP = Dns.Resolve("localhost").AddressList[0];

                tcpl = new TcpListener(LocalIP, 80); // listen on port 80

                tcpl.Start();

                         



               // int ThreadID = 0;

                while (true)

                {

                    while (!tcpl.Pending())

                    {

                        Thread.Sleep(100);

                    }



                    //启动接受线程

                    ClientSocketThread myThreadHandler = new ClientSocketThread();

                    myThreadHandler.tcpl = tcpl;//Notice: dont forget do this

                    ThreadStart myThreadStart = new ThreadStart(myThreadHandler.HandleThread);

                    Thread myWorkerThread = new Thread(myThreadStart);      

                    myWorkerThread.Start();

                }

            }

            catch (SocketException )

            {

           

            }

            catch (FormatException)

            {

               

            }

            catch (Exception )

            {

               

            }

            //  Console.Read();

       

        }

    }

}

五,启动TestWeb.exe,并单击主对话框上的button。在浏览器中输入:http://127.0.0.1/ 或http://127.0.0.1:80。

源代码下载:

http://download.csdn.net/detail/he_zhidan/8884733

C#建立最简单的web服务,无需IIS的更多相关文章

  1. nodejs创建一个简单的web服务

    这是一个突如其来的想法,毕竟做web服务的框架那么多,为什么要选择nodejs,因为玩前端时,偶尔想调用接口获取数据,而不想关注业务逻辑,只是想获取数据,使用java或者.net每次修改更新后还要打包 ...

  2. 使用python命令构建最简单的web服务

    可以使用python自带的包建立最简单的web服务器,使用方法: 1)切换到服务器的根目录下 2)输入命令: python -m SimpleHTTPServer 3)使用wget或者在浏览器访问测试 ...

  3. linux系统下开启一个简单的web服务

    linux 下开启一个简单的web服务: 首先需要linux下安装nodejs 然后创建一个test.js:   vi test.js var http =require("http&quo ...

  4. node创建一个简单的web服务

    本文将如何用node创建一个简单的web服务,过程也很简单呢~ 开始之前要先安装node.js 1.创建一个最简单的服务 // server.js const http = require('http ...

  5. Web服务器之iis,apache,tomcat三者之间的比较

    IIS-Apache-Tomcat的区别 IIS与Tomcat的区别 IIS是微软公司的Web服务器.主要支持ASP语言环境. Tomcat是Java Servlet 2.2和JavaServer P ...

  6. 用Python建立最简单的web服务器

    利用Python自带的包可以建立简单的web服务器.在DOS里cd到准备做服务器根目录的路径下,输入命令: python -m Web服务器模块 [端口号,默认8000] 例如: python -m ...

  7. [转] 用Python建立最简单的web服务器

    [From] http://www.cnblogs.com/xuxn/archive/2011/02/14/build-simple-web-server-with-python.html 利用Pyt ...

  8. 学习用node.js建立一个简单的web服务器

    一.建立简单的Web服务器涉及到Node.js的一些基本知识点: 1.请求模块 在Node.js中,系统提供了许多有用的模块(当然你也可以用JavaScript编写自己的模块,以后的章节我们将详细讲解 ...

  9. 基于gin框架搭建的一个简单的web服务

    刚把go编程基础知识学习完了,学习的时间很短,可能还有的没有完全吸收.不过还是在项目中发现知识,然后在去回顾已学的知识,现在利用gin这个web框架做一个简单的CRUD操作. 1.Go Web框架的技 ...

随机推荐

  1. gcc编译问题

    gcc avl.o hash.o list.o rb.o example.o -o 123.exe 多个.o输出 exe -c和-o都是gcc编译器的可选参数.-c表示只编译(compile)源文件但 ...

  2. 批处理 更新 svn git hg

    @echo off Setlocal enabledelayedexpansion ::CODER BY Administrator POWERD BY iBAT 1.6 ::设置svn默认安装位置以 ...

  3. delphi 新版数组操作

    https://www.cnblogs.com/xalion/p/4283491.html

  4. web中的$多种意思

    $符号在php中是表示变量的特征字符, 在js中它也有很多作用, 一般我们用来命名一个函数名称,获取id的1.首先可以用来表示变量, 比如变量 var s='asdsd'或var $s='asdasd ...

  5. 2011 luogu P1311 选择客栈

    题目描述 丽江河边有 nn 家很有特色的客栈,客栈按照其位置顺序从 1 到 n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖 ...

  6. Informatica抽取SQL Server数据库乱码

    1.首先确认数据库的关系连接所使用的代码页,是否一致 2.如果上述方法不行,在Designer中更改数据类型,将string类型改为nstring类型,中文就没有乱码了 3.SQL Server数据库 ...

  7. 用户管理命令--passwd,usermod,userdel

    用户修改密码命令--passwd 当修改用户的密码时,也要分普通用户和超级用户两种情况 普通用户:修改密码前需要先输入当前密码,确认是否正确 密码设置不可以过于简单 超级用户:权利非常的大,可以设置任 ...

  8. Binlog详解

    一.介绍 binlog, 即二进制文件,他记录了MySQL所有数据的变更,并以二进制的形式存储在磁盘上 二.binlog模式 binlog有三种模式:ROW(行模式),   Statement(语句模 ...

  9. post processing in CFD

    post post Table of Contents 1. Post-processing 1.1. Reverse flow 1.1.1. reasons 1.1.2. solutions 1.2 ...

  10. EmpireofCode文档翻译 https://empireofcode.com/game/

    In Campaign mode, you can check your strategies on already defeated bases. You will not lose your tr ...