CWebServer.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:10k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.IO;
- /*
- ' 迅龙中文分类搜索引擎 v0.6
- '
- ' LGPL 许可发行
- '
- ' 宁夏大学 张冬 康彩 zd4004@163.com
- '
- ' 官网 http://blog.163.com/zd4004/
- */
- namespace XunLong.NewWebServer
- {
- class CWebServer
- {
- /// <summary>
- /// 编码解码
- /// </summary>
- NewNxuEncoding.CNewNxuEncoding newz_code = new NewNxuEncoding.CNewNxuEncoding();
- /// <summary>
- /// 帮定的地址 不含 http://
- /// </summary>
- public string HTTPURL = "";
- Encoding gbx = System.Text.Encoding.GetEncoding("gb2312");
- Socket clientSocket;
-
- string Set_Path ;
- public CWebServer(Socket socket , string path_1)
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- this.clientSocket = socket;
- this.Set_Path = path_1;
- }
- public void Run()
- {
- int X = 0;
- string time = DateTime.Now.Millisecond.ToString();
- string clientmessage = " ";
- //存放来自客户端的HTTP请求字符串
- string URL = " ";
- Byte[] read = new byte[2048];
- try
- {
- while (true)
- {
- X = X + 1;
- //存放解析出地址请求信息
- int bytes = clientSocket.Receive(read, 2048, 0);
- if (bytes == 0)
- {
- clientSocket.Close();
- return;
- }
- string str = gbx.GetString(read, 0, bytes);
- byte[] message = CmdRunIt(str);
- clientSocket.Send(message);
-
- Console.WriteLine(" Send HTML Data "+ DateTime.Now.ToLongTimeString() );
- // string end = "rnrn";
- // byte[] endbyte = gbx.GetBytes(end);
- // clientSocket.Send(endbyte);
- // System.Threading.Thread.Sleep(200);
- // clientSocket.Close();
- Console.WriteLine(" 本线程 " + time + " 请求次数:" + X);
- // return;
- }
- }
- catch
- {
- Console.WriteLine(" 线程 " + time + "退出 请求次数:" + X);
- try
- { clientSocket.Close(); }
- catch
- { }
- }
-
- }
- /// <summary>
- /// 合成
- /// </summary>
- /// <param name="b1"></param>
- /// <param name="b2"></param>
- /// <param name="b3"></param>
- /// <returns></returns>
- private byte[] GetAll2One(byte[] b1, byte[] b2)
- {
- byte[] w = new byte[b1.Length + b2.Length];
- int h = 0;
- for (int i = 0; i < b1.Length; i++)
- {
- w[h] = b1[i];
- h = h + 1;
- }
- for (int i = 0; i < b2.Length; i++)
- {
- w[h] = b2[i];
- h = h + 1;
- }
- return w;
- }
- /// <summary>
- /// 任务命令执行
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- private byte[] CmdRunIt(string data)
- {
- //1 分析应答头 只识别 jpg gif png
- // GET /test.html HTTP/1.1
- if (data.IndexOf('n') > -1)
- {
- char[] x ={ 'n', 'r' };
- string[] n_GETHEAD = data.Split(x);
- // GET方法 GET /aaa HTTP/1.1 GET 路径/文件名 HTTP/1.0
- if (n_GETHEAD[0].ToUpper().IndexOf("GET") == 0)
- {
- string[] tmp_1 = n_GETHEAD[0].Split(' ');
- string tmp_2 = tmp_1[1];
- tmp_2 = "/"+ newz_code.CODE2CN(tmp_2.Substring(1,tmp_2.Length-1));
- // 搜索的 GET方法 --> 得到一个列表数据 one1?wd=word&n=1 搜索词和页码
- if (tmp_2.IndexOf("/one1?wd=") > -1)
- {
- //Environment.TickCount;
- string New_CANSHU = System.Web.HttpUtility.UrlDecode(tmp_2, System.Text.Encoding.GetEncoding("GB2312"));
- int L = New_CANSHU.IndexOf("czd=");
- string mainType = New_CANSHU.Substring(L + 4, New_CANSHU.Length - L - 4);
- New_CANSHU = New_CANSHU.Replace("&czd=" + mainType, "");
- string B_data = GetOne1HTM(New_CANSHU, "&czd=" + mainType);
- byte[] tmp_p2 = gbx.GetBytes(B_data);
- string sBuffer = "";
- sBuffer = "HTTP/1.1 200 OK" + "rn";
- sBuffer = sBuffer + "Server: KCrn";
- sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
- sBuffer = sBuffer + "Accept-Ranges: bytesrn";
- sBuffer = sBuffer + "Content-Length: " + tmp_p2.Length.ToString() + "rnrn";
- byte[] tmp_p1 = gbx.GetBytes(sBuffer);
- byte[] b_k = GetAll2One(tmp_p1, tmp_p2);
- return b_k;
- }
- // 搜索的 GET方法 --> 得到1个属性选项列表 one2?wd=word word = 搜索词+主类别
- if (tmp_2.IndexOf("/one2?wd=") > -1)
- {
- string New_CANSHU = System.Web.HttpUtility.UrlDecode(tmp_2, System.Text.Encoding.GetEncoding("GB2312"));
- int L = New_CANSHU.IndexOf("czd=");
- string mainType = New_CANSHU.Substring(L + 4, New_CANSHU.Length - L - 4);
- New_CANSHU = New_CANSHU.Replace("&czd=" + mainType, "");
- string B_data = GetOne2HTM(New_CANSHU, "&czd=" + mainType);
- byte[] tmp_p2 = gbx.GetBytes(B_data);
- string sBuffer = "";
- sBuffer = "HTTP/1.1 200 OK" + "rn";
- sBuffer = sBuffer + "Server: KCrn";
- sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
- sBuffer = sBuffer + "Accept-Ranges: bytesrn";
- sBuffer = sBuffer + "Content-Length: " + tmp_p2.Length.ToString() + "rnrn";
- byte[] tmp_p1 = gbx.GetBytes(sBuffer);
- byte[] b_k = GetAll2One(tmp_p1, tmp_p2);
- return b_k;
- }
- // 搜索的 GET方法 得到总体
- if (tmp_2.IndexOf("/s?wd=") > -1)
- {
- ///s?wd=<关键词>北京</关键词>&czd=类别1
- string New_CANSHU = System.Web.HttpUtility.UrlDecode(tmp_2, System.Text.Encoding.GetEncoding("GB2312"));
- int L = New_CANSHU.IndexOf("czd=");
- string mainType = New_CANSHU.Substring(L + 4, New_CANSHU.Length - L - 4);
- New_CANSHU = New_CANSHU.Replace("&czd=" + mainType, "");
- // s?wd= 得到搜索词
- string[] xF = New_CANSHU.Split('=');
- string sWord = xF[1];
- string B_data = SearchGet(sWord, "&czd=" + mainType);
- byte[] tmp_p2 = gbx.GetBytes(B_data);
- string sBuffer = "";
- sBuffer = "HTTP/1.1 200 OK" + "rn";
- sBuffer = sBuffer + "Server: KCrn";
- sBuffer = sBuffer + "Content-Type: " + "text/html" + "rn";
- sBuffer = sBuffer + "Accept-Ranges: bytesrn";
- sBuffer = sBuffer + "Content-Length: " + tmp_p2.Length.ToString() + "rnrn";
- byte[] tmp_p1 = gbx.GetBytes(sBuffer);
- byte[] b_k = GetAll2One(tmp_p1, tmp_p2);
- return b_k;
- }
- }
- }
- ERRIT:
- //下面为出错后的返回
- string B_HEAD_NO = "";
- B_HEAD_NO = "HTTP/1.1 200 OK" + "rn";
- B_HEAD_NO = B_HEAD_NO + "Server: Apache/1.3.33 (Unix)" + "rn";
- B_HEAD_NO = B_HEAD_NO + "Connection: close" + "rn";
- B_HEAD_NO = B_HEAD_NO + "Content-Type: text/html" + "rnrn";
- string B_RE = "<html>没有发现数据!</html>" + "rnrn";
- byte[] tm1 = gbx.GetBytes(B_HEAD_NO);
- byte[] tm2 = gbx.GetBytes(B_RE);
- byte[] b_k_n = GetAll2One(tm1, tm2);
- return b_k_n;
- }
- /// <summary>
- /// 由GET 得到的搜索接口 主搜索
- /// </summary>
- /// <param name="dat"></param>
- /// <returns></returns>
- private string SearchGet(string url, string mainType)
- {
- string a = ClassRUN_XZ.SearchGet(url, mainType);
- return a;
- }
- /// <summary>
- /// 页面数据
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- private string GetOne1HTM(string url, string mainType)
- {
- string a = ClassRUN_XZ.GetOne1HTM(url, mainType);
- return a;
- }
- /// <summary>
- /// 广告+ 头 + 属性
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- private string GetOne2HTM(string url, string mainType)
- {
- string a = ClassRUN_XZ.GetOne2HTM(url, mainType);
- return a;
- }
- }
- }