- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- /*
- ' 迅龙中文分类搜索引擎 v0.6
- '
- ' LGPL 许可发行
- '
- ' 宁夏大学 张冬 康彩 zd4004@163.com
- '
- ' 官网 http://blog.163.com/zd4004/
- */
- namespace XunLong.binWebGate
- {
- class ServerXLClass
- {
- /// <summary>
- /// 编码解码
- /// </summary>
- NewNxuEncoding.CNewNxuEncoding newz_code = new NewNxuEncoding.CNewNxuEncoding();
- Encoding gbx = System.Text.Encoding.GetEncoding("gb2312");
- Socket clientSocket;
- System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312");
- public ServerXLClass(Socket socket)
- {
- this.clientSocket = socket;
- }
- public string one(string url)
- {
- if (url == "")
- {
- string dat = "URL出错!";
- byte[] x = gb.GetBytes(dat);
- 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: " + x.Length.ToString() + "rnrn";
- return sBuffer + dat + "rnrn";
- }
- else
- {
- try
- {
- WebClient wc = new WebClient();
- byte[] x = wc.DownloadData(url);
- wc.Dispose();
- string str = gb.GetString(x);
- 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: " + x.Length.ToString() + "rnrn";
- return sBuffer + str + "rnrn";
- }
- catch
- {
- string dat = "服务器忙,请稍候再试!";
- byte[] x = gb.GetBytes(dat);
- 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: " + x.Length.ToString() + "rnrn";
- return sBuffer + dat + "rnrn";
- }
- }
- }
- 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);
- string newurl = GetOneUrl(str);
- string newdat = one(newurl);
- byte[] message = gbx.GetBytes(newdat) ;
- clientSocket.Send(message);
- // System.Threading.Thread.Sleep(200);
- // clientSocket.Close();
- Console.WriteLine(" 本线程 "+time+" 请求次数:"+X);
- }
- }
- catch
- {
- Console.WriteLine(" 线程 " + time + "退出 请求次数:" + X);
- try
- { clientSocket.Close(); }
- catch
- { }
- }
- }
- /// <summary>
- /// 根据得到的数据判断出 需要访问那个服务器
- /// </summary>
- /// <param name="dat"></param>
- /// <returns>给出明确的url</returns>
- private string GetOneUrl(string dat)
- {
- try
- {
- int x = dat.IndexOf("r");
- string a_1 = dat.Substring(0, x);
- string[] a_2 = a_1.Split(' ');
- string a_3 = a_2[1].Substring(1, a_2[1].Length - 1);
- a_3 = System.Web.HttpUtility.UrlDecode(a_3, System.Text.Encoding.GetEncoding("GB2312"));
- //取出主类别 判断 s?wd=<关键词>北京</关键词>&czd=类别1
- int L = a_3.IndexOf("czd=");
- string mainType = a_3.Substring(L+4,a_3.Length-L-4);
- //请求串
- string urlAdd = a_3;
- if (XunLong.CongifData.Config_web.mainType.Contains(mainType) == false)
- {
- return "";
- }
- else
- {
- Console.WriteLine(" >"+mainType + " " + XunLong.CongifData.Config_web.mainType[mainType].ToString() + " " + urlAdd);
- return "http://" + XunLong.CongifData.Config_web.mainType[mainType].ToString() + "/" + newz_code.CN2CODE(urlAdd);
- }
- return a_3;
- }
- catch
- {
- return "";
- }
- }
- }
- }