ClassTCPP.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:14k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Collections;
- using System.IO;
- /*
- ' 迅龙中文分类搜索引擎 v0.6
- '
- ' LGPL 许可发行
- '
- ' 宁夏大学 张冬 康彩 zd4004@163.com
- '
- ' 官网 http://blog.163.com/zd4004/
- */
- namespace XunLong.NewXWord
- {
- /// <summary>
- /// 存储结构
- /// </summary>
- struct DSU
- {
- /// <summary>
- /// 是否忙
- /// </summary>
- public bool isBuse;
- /// <summary>
- /// 需要分的词
- /// </summary>
- public string OldWord;
- /// <summary>
- /// 分词结果
- /// </summary>
- public string Xword;
- /// <summary>
- /// 空闲时间最长的
- /// </summary>
- public int SendZTime;
-
- }
- /// <summary>
- /// 获取和压入文件头数据
- /// </summary>
- class ClassTcpClient
- {
- private Socket clientSocket;
- private const int dataSize = 1024*2;
- private byte[] data = new byte[dataSize];
-
- /// <summary>
- /// 记录TCP状态 key = socket val = T/F 是否空闲
- /// </summary>
- public Hashtable x = new Hashtable();
- /// <summary>
- /// 分词缓存 key =old val = jguo
- /// </summary>
- Hashtable Y = new Hashtable();
- /// <summary>
- /// 存放得到的数据
- /// </summary>
- private string GetOneData = "";
- /// <summary>
- /// 编码
- /// </summary>
- NewNxuEncoding.CNewNxuEncoding mCode = new NewNxuEncoding.CNewNxuEncoding();
- //文件
- StreamWriter w_writer = null;
- /// <summary>
- /// 得到1个值
- /// </summary>
- /// <param name="dat"></param>
- /// <returns></returns>
- public string GetOneEnd(string dat)
- {
- if (Y.Contains(dat) == true)
- {
- return Y[dat].ToString();
- }
- return null;
- }
-
- public bool Conn(string ip, int port)
- {
- IPAddress myIP;
- IPEndPoint iep;
- try
- {
- myIP = IPAddress.Parse(ip);
- iep = new IPEndPoint(myIP, port);
- }
- catch
- {
- Console.WriteLine("你输入的服务器名或端口号格式不正确,请重新输入!");
- return false;
- }
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socket.SendTimeout = 500;
- socket.ReceiveTimeout = 500;
- socket.BeginConnect(iep, new AsyncCallback(ConnectServer), socket);
- return true;
- }
- private void ConnectServer(IAsyncResult ar)
- {
- Socket clientSocket0 = (Socket)ar.AsyncState;
- try
- {
- clientSocket0.EndConnect(ar);
- // Console.WriteLine("与服务器" + clientSocket.RemoteEndPoint.ToString() + "连接成功。");
- clientSocket0.BeginReceive(data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), clientSocket0);
- if (x.Contains(clientSocket0) == false)
- {
- DSU a = new DSU();
- a.isBuse = false;
- a.OldWord = "";
- a.Xword = "";
- a.SendZTime = Environment.TickCount;
- x.Add(clientSocket0, a);
- }
- else
- {
- DSU a = new DSU();
- a.isBuse = false;
- a.OldWord = "";
- a.Xword = "";
- a.SendZTime = Environment.TickCount;
- x[clientSocket0] = a;
- }
- Console.WriteLine("-连接成功-> " + clientSocket0.RemoteEndPoint.ToString() );
- }
- catch
- {
- // Console.WriteLine("与服务器连接失败!");
- if (x.Contains(clientSocket0) == true)
- {
- x.Remove(clientSocket0);
- }
- }
- }
- private void ReceiveData(IAsyncResult ar)
- {
- Socket server = (Socket)ar.AsyncState;
- try
- {
- int receiveDataLength = server.EndReceive(ar);
- // string str = System.Text.Encoding.Unicode.GetString(data, 0, receiveDataLength);
- Encoding gbx = System.Text.Encoding.GetEncoding("gb2312");
- string str =gbx.GetString(data, 0, receiveDataLength-1);
- DSU a = (DSU)x[server];
- try
- {
- //GetOneData = str;
- if (Y.Contains(a.OldWord) == true)
- {
- }
- else
- {
- Y.Add(a.OldWord, str);
- }
- }
- catch
- {
-
-
- }
- a.isBuse = false;
- a.OldWord = "";
- a.Xword = "";
- a.SendZTime = Environment.TickCount;
- //把得到的数据压入分词缓存
- x[server] = a;
- }
- catch
- {
- x.Remove(server);
- }
- }
- public bool SendOneData(string dataIt)
- {
- //找到1个空闲的连接 Environment.TickCount; 空闲时间最长的
- Hashtable x2 = (Hashtable)x.Clone();
- Socket O1 =null;
- int TmpTime = Environment.TickCount+1;
- foreach (System.Collections.DictionaryEntry de in x2)
- {
- DSU a = (DSU)de.Value;
- if (a.isBuse ==true )
- { }
- else
- {
- if (a.SendZTime < TmpTime)
- {
- TmpTime = a.SendZTime;
- try
- {
- O1 = (Socket)de.Key;
- }
- catch
- { }
- }
- }
- }
- if (O1 == null)
- {
- //清理掉超时的任务
- foreach (System.Collections.DictionaryEntry de in x2)
- {
- DSU a = (DSU)de.Value;
- if (a.isBuse == true)
- {
- if (a.SendZTime+1600 < Environment.TickCount)
- {
- try
- {
- Socket pp = (Socket)de.Key;
- pp.Close();
- x.Remove(de.Key);
- }
- catch
- { }
- }
- }
- }
- Console.WriteLine("-缺乏有效可用分词服务-〉");
- return false;
- }
- else
- {
- Encoding gbx = System.Text.Encoding.GetEncoding("gb2312");
- byte[] message = gbx.GetBytes(dataIt);
- O1.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), O1);
- DSU a = new DSU();
- a.OldWord = dataIt;
- a.isBuse = true;
- a.Xword = "";
- a.SendZTime = Environment.TickCount;
- x[O1] = a;
- //清理掉超时的任务
- foreach (System.Collections.DictionaryEntry de in x2)
- {
- DSU avv = (DSU)de.Value;
- if (avv.isBuse == true)
- {
- if (avv.SendZTime + 1600 < Environment.TickCount)
- {
- try
- {
- Socket pp = (Socket)de.Key;
- pp.Close();
- x.Remove(de.Key);
- }
- catch
- { }
- }
- }
- }
- return true;
- }
-
- }
- private void SendData(IAsyncResult ar)
- {
- Socket socketE = (Socket)ar.AsyncState;
- try
- {
- int send = socketE.EndSend(ar);
- socketE.BeginReceive(data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), socketE);
- }
- catch
- {
- x.Remove(socketE);
- }
- }
- /// <summary>
- /// 关闭连接
- /// </summary>
- public void ConnStop()
- {
-
- }
- /// <summary>
- /// 初始化已经分词的结果 可以用来加速
- /// </summary>
- /// <param name="okPath">使用缓存服务器的缓存数据</param>
- public void initOKxWord(string okPath)
- {
- //初始化分词缓存
- Y.Clear();
- StreamReader reader = null;
- try
- {
- reader = new StreamReader(okPath, System.Text.Encoding.GetEncoding("gb2312"));
- for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
- {
- if (line != null)
- {
- if (line.IndexOf('t') > 0)
- {
- string[] mxd = line.Split('t');
- //解码
- mxd[0] = mCode.CODE2CN(mxd[0]);
- mxd[1] = mCode.CODE2CN(mxd[1]);
- if (Y.Contains(mxd[0]) == false)
- {
- Y.Add(mxd[0], mxd[1]);
- }
- }
- }
- }
- reader.Close();
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- finally
- {
- if (reader != null)
- reader.Close();
- }
- Console.WriteLine("-共加载分词缓存数据-> "+Y.Count.ToString()+" 条");
- }
- /// <summary>
- /// 写入一条数据
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <param name="data">数据</param>
- /// <param name="isApp">是否追加模式</param>
- public void putXwordCacheFileData_A(string okPath, string data)
- {
- StreamWriter writer = null;
- try
- {
- writer = new StreamWriter(okPath, true, System.Text.Encoding.GetEncoding("gb2312"));
- // writer.Write(data);
- writer.WriteLine(data);
- writer.Close();
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- finally
- {
- if (writer != null)
- writer.Close();
- }
- Console.WriteLine("=-当前共有分词缓存数据-> " + Y.Count.ToString() + " 条");
- }
- /// <summary>
- /// 写入一条数据
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <param name="data">数据</param>
- /// <param name="isApp">是否追加模式</param>
- public void putXwordCacheFileData_Write(string okPath, string data)
- {
-
- try
- {
- w_writer.WriteLine(data);
-
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- Console.WriteLine("严重错误!!!");
- System.Threading.Thread.Sleep(1000);
- }
-
- Console.WriteLine("写入数据=-当前共有分词缓存数据-> " + Y.Count.ToString() + " 条");
- }
- /// <summary>
- /// 打开文件
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <param name="data">数据</param>
- /// <param name="isApp">是否追加模式</param>
- public void putXwordCacheFileData_Open(string okPath, string data)
- {
-
- try
- {
- w_writer = new StreamWriter(okPath, true, System.Text.Encoding.GetEncoding("gb2312"));
-
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- Console.WriteLine("严重错误!!!");
- System.Threading.Thread.Sleep(1000000);
- }
- Console.WriteLine("打开数据=-当前共有分词缓存数据-> " + Y.Count.ToString() + " 条");
- }
- /// <summary>
- /// 关闭
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <param name="data">数据</param>
- /// <param name="isApp">是否追加模式</param>
- public void putXwordCacheFileData_Close(string okPath, string data)
- {
-
- try
- {
- w_writer.Close();
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- finally
- {
- if (w_writer != null)
- w_writer.Close();
- }
- Console.WriteLine("关闭数据=-当前共有分词缓存数据-> " + Y.Count.ToString() + " 条");
- }
- }
- }