对于爱折腾的人来讲,可能需要经常用到网页服务器,用过node.js创建过的服务器,也用过IIS Express搭设的服务器,不过还是一直想要创建一个自己的服务器程序,测试会方便一些。下面是网页服务器的源码。
编译过程讲下,首先下载一个SharpDevelop,国外的IDE,只有40多兆,支持窗体控件,学习C#很方便,也可以制作一些简单的程序,不过64位程序编译有问题。现在没有人维护了。不过编译32位没问题。
先创建一个C#窗体应用程序,然后将下面的源代码复制粘帖到窗体代码页中,注意命名空间不要改,如果不想改,那你直接建项目的时候,命名为JS_Server也可以。
/*
* Created by SharpDevelop.
* User: yaof
* Date: 2022/12/11
* Time: 15:46
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Globalization;
using System.Web;
using System.Web.Hosting;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
namespace JS_Server
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
int port;
Thread td;
WebServer server;
string rootpath=string.Empty;
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 12F);
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 0;
this.label1.Text = "端口:";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 12F);
this.label2.Location = new System.Drawing.Point(12, 41);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(115, 23);
this.label2.TabIndex = 1;
this.label2.Text = "文件夹位置:";
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("宋体", 12F);
this.textBox1.Location = new System.Drawing.Point(144, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 26);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "8088";
//
// textBox2
//
this.textBox2.Font = new System.Drawing.Font("宋体", 12F);
this.textBox2.Location = new System.Drawing.Point(144, 38);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(237, 26);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "D:\\\\0.Projects\\\\WebGL";
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 12F);
this.button1.Location = new System.Drawing.Point(387, 41);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "浏览...";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 12F);
this.button2.Location = new System.Drawing.Point(12, 82);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(100, 33);
this.button2.TabIndex = 5;
this.button2.Text = "开启服务";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.Button2Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(473, 127);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "JS_Server";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainFormFormClosing);
this.ResumeLayout(false);
this.PerformLayout();
}
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
rootpath=textBox2.Text;
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
public void runServer(object port)
{
int portnum=(int)port;
string dir=Directory.GetCurrentDirectory();
InitHostFile(dir