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

搜索引擎

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. /*
  7.       '       迅龙中文分类搜索引擎  v0.6
  8.       '
  9.       '        LGPL  许可发行
  10.       '
  11.       '       宁夏大学  张冬 康彩  zd4004@163.com
  12.       ' 
  13.       '        官网 http://blog.163.com/zd4004/
  14.  */
  15. namespace XunLong.binWebGate
  16. {
  17.     class ServerXLClass
  18.     {
  19.         /// <summary>
  20.         /// 编码解码
  21.         /// </summary>
  22.         NewNxuEncoding.CNewNxuEncoding newz_code = new NewNxuEncoding.CNewNxuEncoding();
  23.         Encoding gbx = System.Text.Encoding.GetEncoding("gb2312");
  24.         Socket clientSocket;   
  25. System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312");
  26.         public ServerXLClass(Socket socket)
  27.         {
  28.             this.clientSocket = socket;
  29.         }
  30.         public string one(string url)
  31.         {
  32.             if (url == "")
  33.             {
  34.                 string dat = "URL出错!";
  35.                 byte[] x = gb.GetBytes(dat);
  36.                 string sBuffer = "";
  37.                 sBuffer = "HTTP/1.1 200 OK" + "rn";
  38.                 sBuffer = sBuffer + "Server: KCrn";
  39.                 sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
  40.                 sBuffer = sBuffer + "Accept-Ranges: bytesrn";
  41.                 sBuffer = sBuffer + "Content-Length: " + x.Length.ToString() + "rnrn";
  42.                 return sBuffer + dat + "rnrn";
  43.             }
  44.             else
  45.             {
  46.                 try
  47.                 {
  48.                     WebClient wc = new WebClient();
  49.                    
  50.                     byte[] x = wc.DownloadData(url);
  51.                     
  52.                     wc.Dispose();
  53.                     string str = gb.GetString(x);
  54.                     string sBuffer = "";
  55.                     sBuffer = "HTTP/1.1 200 OK" + "rn";
  56.                     sBuffer = sBuffer + "Server: KCrn";
  57.                     sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
  58.                     sBuffer = sBuffer + "Accept-Ranges: bytesrn";
  59.                     sBuffer = sBuffer + "Content-Length: " + x.Length.ToString() + "rnrn";
  60.                     return sBuffer + str + "rnrn";
  61.                 }
  62.                 catch
  63.                 {
  64.                     string dat = "服务器忙,请稍候再试!";
  65.                     byte[] x = gb.GetBytes(dat);
  66.                     string sBuffer = "";
  67.                     sBuffer = "HTTP/1.1 200 OK" + "rn";
  68.                     sBuffer = sBuffer + "Server: KCrn";
  69.                     sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
  70.                     sBuffer = sBuffer + "Accept-Ranges: bytesrn";
  71.                     sBuffer = sBuffer + "Content-Length: " + x.Length.ToString() + "rnrn";
  72.                     return sBuffer + dat + "rnrn";
  73.                 }
  74.             }
  75.         }
  76.         public void Run()
  77.         {
  78.             int X = 0;
  79.             string time = DateTime.Now.Millisecond.ToString();
  80.             string clientmessage = " ";
  81.             //存放来自客户端的HTTP请求字符串
  82.             string URL = " ";
  83.             Byte[] read = new byte[2048];
  84.             try
  85.             {
  86.                 while (true)
  87.                 {
  88.                     X = X + 1;
  89.                     //存放解析出地址请求信息
  90.                     int bytes = clientSocket.Receive(read, 2048, 0);
  91.                     if (bytes == 0)
  92.                     {
  93.                         clientSocket.Close();
  94.                         return;
  95.                     }
  96.                     string str = gbx.GetString(read, 0, bytes);
  97.                     string newurl = GetOneUrl(str);
  98.                     string newdat = one(newurl);
  99.                     byte[] message = gbx.GetBytes(newdat)  ;
  100.                     clientSocket.Send(message);
  101.                   // System.Threading.Thread.Sleep(200);
  102.                    // clientSocket.Close();
  103.                     Console.WriteLine(" 本线程 "+time+" 请求次数:"+X);
  104.                 }
  105.             }
  106.             catch
  107.             {
  108.                 Console.WriteLine(" 线程 " + time + "退出 请求次数:" + X);
  109.                 try
  110.                 { clientSocket.Close(); }
  111.                 catch
  112.                 { }
  113.             }
  114.             
  115.         }
  116.         /// <summary>
  117.         /// 根据得到的数据判断出 需要访问那个服务器
  118.         /// </summary>
  119.         /// <param name="dat"></param>
  120.         /// <returns>给出明确的url</returns>
  121.         private string GetOneUrl(string dat)
  122.         {
  123.             try
  124.             {
  125.                 int x = dat.IndexOf("r");
  126.                 string a_1 = dat.Substring(0, x);
  127.                 string[] a_2 = a_1.Split(' ');
  128.                 string a_3 = a_2[1].Substring(1, a_2[1].Length - 1);
  129.                 a_3 = System.Web.HttpUtility.UrlDecode(a_3, System.Text.Encoding.GetEncoding("GB2312"));
  130.                 //取出主类别 判断 s?wd=<关键词>北京</关键词>&czd=类别1
  131.                 int L = a_3.IndexOf("czd=");
  132.                
  133.                 string mainType = a_3.Substring(L+4,a_3.Length-L-4);
  134.                 //请求串
  135.                 string urlAdd = a_3;
  136.                 if (XunLong.CongifData.Config_web.mainType.Contains(mainType) == false)
  137.                 {
  138.                     return "";
  139.                 }
  140.                 else
  141.                 {
  142.                     Console.WriteLine(" >"+mainType + " " + XunLong.CongifData.Config_web.mainType[mainType].ToString() + " " + urlAdd);
  143.                     
  144.                     return "http://" + XunLong.CongifData.Config_web.mainType[mainType].ToString() + "/" + newz_code.CN2CODE(urlAdd);
  145.                 }
  146.                 return a_3;
  147.             }
  148.             catch
  149.             {
  150.                 return "";
  151.             }
  152.         }
  153.     }
  154. }