CWebServer.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:10k
源码类别:

搜索引擎

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.IO;
  8. /*
  9.       '       迅龙中文分类搜索引擎  v0.6
  10.       '
  11.       '        LGPL  许可发行
  12.       '
  13.       '       宁夏大学  张冬 康彩  zd4004@163.com
  14.       ' 
  15.       '        官网 http://blog.163.com/zd4004/
  16.  */
  17. namespace XunLong.NewWebServer
  18. {
  19.     class CWebServer
  20.     {
  21.         /// <summary>
  22.         /// 编码解码
  23.         /// </summary>
  24.         NewNxuEncoding.CNewNxuEncoding newz_code = new NewNxuEncoding.CNewNxuEncoding(); 
  25.         /// <summary>
  26.         /// 帮定的地址 不含 http://
  27.         /// </summary>
  28.         public string HTTPURL = "";
  29.         Encoding gbx = System.Text.Encoding.GetEncoding("gb2312");
  30.         Socket clientSocket;
  31.         
  32.        string   Set_Path ;
  33.         public CWebServer(Socket socket , string path_1)
  34.         {
  35.             //
  36.             // TODO: 在此处添加构造函数逻辑
  37.             //
  38.             this.clientSocket = socket;
  39.             this.Set_Path = path_1;
  40.         }
  41.         public void Run()
  42.         {
  43.             int X = 0;
  44.             string time = DateTime.Now.Millisecond.ToString();
  45.             string clientmessage = " ";
  46.             //存放来自客户端的HTTP请求字符串
  47.             string URL = " ";
  48.             Byte[] read = new byte[2048];
  49.             try
  50.             {
  51.                 while (true)
  52.                 {
  53.                     X = X + 1;
  54.                     //存放解析出地址请求信息
  55.                     int bytes = clientSocket.Receive(read, 2048, 0);
  56.                     if (bytes == 0)
  57.                     {
  58.                         clientSocket.Close();
  59.                         return;
  60.                     }
  61.                     string str = gbx.GetString(read, 0, bytes);
  62.                     byte[] message = CmdRunIt(str);
  63.                     clientSocket.Send(message);
  64.                     
  65.                     Console.WriteLine(" Send HTML  Data "+ DateTime.Now.ToLongTimeString() );
  66.                 //    string end = "rnrn";
  67.                 //    byte[] endbyte = gbx.GetBytes(end);
  68.                 //    clientSocket.Send(endbyte);
  69.                  //   System.Threading.Thread.Sleep(200);
  70.                  //   clientSocket.Close();
  71.                     Console.WriteLine(" 本线程 " + time + " 请求次数:" + X);
  72.                   //  return;
  73.                 }
  74.             }
  75.             catch
  76.             {
  77.                 Console.WriteLine(" 线程 " + time + "退出 请求次数:" + X);
  78.                 try
  79.                 { clientSocket.Close(); }
  80.                 catch
  81.                 { }
  82.             }
  83.             
  84.         }
  85.         /// <summary>
  86.         /// 合成
  87.         /// </summary>
  88.         /// <param name="b1"></param>
  89.         /// <param name="b2"></param>
  90.         /// <param name="b3"></param>
  91.         /// <returns></returns>
  92.         private byte[] GetAll2One(byte[] b1, byte[] b2)
  93.         {
  94.             byte[] w = new byte[b1.Length + b2.Length];
  95.             int h = 0;
  96.             for (int i = 0; i < b1.Length; i++)
  97.             {
  98.                 w[h] = b1[i];
  99.                 h = h + 1;
  100.             }
  101.             for (int i = 0; i < b2.Length; i++)
  102.             {
  103.                 w[h] = b2[i];
  104.                 h = h + 1;
  105.             }
  106.             return w;
  107.         }
  108.         /// <summary>
  109.         /// 任务命令执行
  110.         /// </summary>
  111.         /// <param name="data"></param>
  112.         /// <returns></returns>
  113.         private byte[] CmdRunIt(string data)
  114.         {
  115.             //1 分析应答头  只识别 jpg  gif  png 
  116.             // GET /test.html HTTP/1.1
  117.             if (data.IndexOf('n') > -1)
  118.             {
  119.                 char[] x ={ 'n', 'r' };
  120.                 string[] n_GETHEAD = data.Split(x);
  121.                 // GET方法  GET /aaa HTTP/1.1  GET 路径/文件名 HTTP/1.0
  122.                 if (n_GETHEAD[0].ToUpper().IndexOf("GET") == 0)
  123.                 {
  124.                     string[] tmp_1 = n_GETHEAD[0].Split(' ');
  125.                     string tmp_2 = tmp_1[1];
  126.                     tmp_2 = "/"+ newz_code.CODE2CN(tmp_2.Substring(1,tmp_2.Length-1));
  127.                     // 搜索的 GET方法 -->  得到一个列表数据 one1?wd=word&n=1  搜索词和页码
  128.                     if (tmp_2.IndexOf("/one1?wd=") > -1)
  129.                     {
  130.                         //Environment.TickCount;
  131.                         string New_CANSHU = System.Web.HttpUtility.UrlDecode(tmp_2, System.Text.Encoding.GetEncoding("GB2312"));
  132.                         int L = New_CANSHU.IndexOf("czd=");
  133.                         string mainType = New_CANSHU.Substring(L + 4, New_CANSHU.Length - L - 4);
  134.                         New_CANSHU = New_CANSHU.Replace("&czd=" + mainType, "");
  135.                         string B_data = GetOne1HTM(New_CANSHU, "&czd=" + mainType);
  136.                         byte[] tmp_p2 = gbx.GetBytes(B_data);
  137.                         string sBuffer = "";
  138.                         sBuffer = "HTTP/1.1 200 OK" + "rn";
  139.                         sBuffer = sBuffer + "Server: KCrn";
  140.                         sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
  141.                         sBuffer = sBuffer + "Accept-Ranges: bytesrn";
  142.                         sBuffer = sBuffer + "Content-Length: " + tmp_p2.Length.ToString() + "rnrn";
  143.                         byte[] tmp_p1 = gbx.GetBytes(sBuffer);
  144.                         byte[] b_k = GetAll2One(tmp_p1, tmp_p2);
  145.                         return b_k;
  146.                     }
  147.                     // 搜索的 GET方法 --> 得到1个属性选项列表  one2?wd=word  word = 搜索词+主类别
  148.                     if (tmp_2.IndexOf("/one2?wd=") > -1)
  149.                     {
  150.                         string New_CANSHU = System.Web.HttpUtility.UrlDecode(tmp_2, System.Text.Encoding.GetEncoding("GB2312"));
  151.                         int L = New_CANSHU.IndexOf("czd=");
  152.                         string mainType = New_CANSHU.Substring(L + 4, New_CANSHU.Length - L - 4);
  153.                         New_CANSHU = New_CANSHU.Replace("&czd=" + mainType, "");
  154.                         string B_data = GetOne2HTM(New_CANSHU, "&czd=" + mainType);
  155.                         byte[] tmp_p2 = gbx.GetBytes(B_data);
  156.                         string sBuffer = "";
  157.                         sBuffer = "HTTP/1.1 200 OK" + "rn";
  158.                         sBuffer = sBuffer + "Server: KCrn";
  159.                         sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
  160.                         sBuffer = sBuffer + "Accept-Ranges: bytesrn";
  161.                         sBuffer = sBuffer + "Content-Length: " + tmp_p2.Length.ToString() + "rnrn";
  162.                         byte[] tmp_p1 = gbx.GetBytes(sBuffer);
  163.                         byte[] b_k = GetAll2One(tmp_p1, tmp_p2);
  164.                         return b_k;
  165.                     }
  166.                     // 搜索的 GET方法 得到总体 
  167.                     if (tmp_2.IndexOf("/s?wd=") > -1)
  168.                     {
  169.                         ///s?wd=<关键词>北京</关键词>&czd=类别1
  170.                         string New_CANSHU = System.Web.HttpUtility.UrlDecode(tmp_2, System.Text.Encoding.GetEncoding("GB2312"));
  171.                         int L = New_CANSHU.IndexOf("czd=");
  172.                         string mainType = New_CANSHU.Substring(L + 4, New_CANSHU.Length - L - 4);
  173.                         New_CANSHU = New_CANSHU.Replace("&czd=" + mainType, "");
  174.                         // s?wd= 得到搜索词
  175.                         string[] xF = New_CANSHU.Split('=');
  176.                         string sWord = xF[1];
  177.                         string B_data = SearchGet(sWord, "&czd=" + mainType);
  178.                         byte[] tmp_p2 = gbx.GetBytes(B_data);
  179.                         string sBuffer = "";
  180.                         sBuffer = "HTTP/1.1 200 OK" + "rn";
  181.                         sBuffer = sBuffer + "Server: KCrn";
  182.                         sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
  183.                         sBuffer = sBuffer + "Accept-Ranges: bytesrn";
  184.                         sBuffer = sBuffer + "Content-Length: " + tmp_p2.Length.ToString() + "rnrn";
  185.                         byte[] tmp_p1 = gbx.GetBytes(sBuffer);
  186.                         byte[] b_k = GetAll2One(tmp_p1, tmp_p2);
  187.                         return b_k;
  188.                     }
  189.                 }
  190.             }
  191.         ERRIT:
  192.             //下面为出错后的返回
  193.             string B_HEAD_NO = "";
  194.             B_HEAD_NO = "HTTP/1.1 200 OK" + "rn";
  195.             B_HEAD_NO = B_HEAD_NO + "Server: Apache/1.3.33 (Unix)" + "rn";
  196.             B_HEAD_NO = B_HEAD_NO + "Connection: close" + "rn";
  197.             B_HEAD_NO = B_HEAD_NO + "Content-Type: text/html" + "rnrn";
  198.             string B_RE = "<html>没有发现数据!</html>" + "rnrn";
  199.             byte[] tm1 = gbx.GetBytes(B_HEAD_NO);
  200.             byte[] tm2 = gbx.GetBytes(B_RE);
  201.             byte[] b_k_n = GetAll2One(tm1, tm2);
  202.             return b_k_n;
  203.         }
  204.         /// <summary>
  205.         /// 由GET 得到的搜索接口 主搜索
  206.         /// </summary>
  207.         /// <param name="dat"></param>
  208.         /// <returns></returns>
  209.         private string SearchGet(string url, string mainType)
  210.         {
  211.             string a = ClassRUN_XZ.SearchGet(url, mainType);
  212.             return a;
  213.         }
  214.         /// <summary>
  215.         /// 页面数据
  216.         /// </summary>
  217.         /// <param name="url"></param>
  218.         /// <returns></returns>
  219.         private string GetOne1HTM(string url, string mainType)
  220.         {
  221.             string a = ClassRUN_XZ.GetOne1HTM(url, mainType);
  222.             return a;
  223.         }
  224.         /// <summary>
  225.         /// 广告+ 头 + 属性
  226.         /// </summary>
  227.         /// <param name="url"></param>
  228.         /// <returns></returns>
  229.         private string GetOne2HTM(string url, string mainType)
  230.         {
  231.             string a = ClassRUN_XZ.GetOne2HTM(url, mainType);
  232.             return a;
  233.         }
  234.     }
  235. }