呼叫中心(CallCenter)开发应用系列(2)

Socket的控制和管理类:

 

1.Sokcet监听类

using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Net; using System.Net.Sockets; using System.Threading; using CC.QJ.Tools; namespace CC.QJSocket.Server { public class SocketListener { public Hashtable Connection = new Hashtable(); public Hashtable Threadpools = new Hashtable(); protected writelog _myLog; Socket mainsocket; string _socketConfig; public event pubvar.CallbackClientMessage CbMessage; public void StartListen(string socketConfig, writelog log) { try { _myLog = log; string[] config = socketConfig.Split(':'); if (config == null || config.Length < 2) { _myLog(LogFile.Error, "sokcet配制错误,请检查配置项!配置模式如下:IP:Port" ); return; } _socketConfig = socketConfig; //端口号、IP地址 string host = config[0]; IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, int.Parse(config[1])); //创建一个Socket类 mainsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); mainsocket.Bind(ipe);//绑定2000端口 mainsocket.Listen(0);//开始监听 _myLog(LogFile.Trace, "启动Socket监听,配置项:" + socketConfig); while (true) { Socket connectionSocket = mainsocket.Accept();//为新建连接创建新的Socket _myLog(LogFile.Trace, "客户端[" + connectionSocket.RemoteEndPoint.ToString() + "]连接已建立..."); Connection gpsCn = new Connection(connectionSocket,_myLog); gpsCn.CbMessage += new pubvar.CallbackClientMessage(CbMessage); //记住这里层层回调了... Connection.Add(connectionSocket.RemoteEndPoint.ToString(), gpsCn); //在新线程中启动新的socket连接,每个socket等待,并保持连接 Thread thread = new Thread(new ThreadStart(gpsCn.WaitForSendData)); Threadpools.Add(connectionSocket.RemoteEndPoint.ToString(),thread); thread.Name = connectionSocket.RemoteEndPoint.ToString(); thread.Start(); } } catch(Exception ex) { _myLog(LogFile.Error, "Exception:" + ex); } } public void FreeSocketListener() { //Connection = new Hashtable(); //Threadpools = new Hashtable(); if (mainsocket.Connected) { mainsocket.Disconnect(false); } mainsocket.Close(); mainsocket = null; //强制退出系统 System.Environment.Exit(0); } } }

 

2.Sokcet连接类

using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using CC.QJ.Tools; namespace CC.QJSocket.Server { public class Connection { public Socket _connection; protected writelog _myLog; //收到客户端数据后,回调到语音卡线程中处理,但是首先回调到Listener中,一层一层的回调出去. public event pubvar.CallbackClientMessage CbMessage; public Connection(Socket socket, writelog log) { _connection = socket; _myLog = log; } public void WaitForSendData() { while (true) { byte[] bytes = new byte[1024]; string data = ""; try { //等待接收消息 int bytesRec = this._connection.Receive(bytes); if (bytesRec == 0) { _myLog(LogFile.Trace,"客户端[" + _connection.RemoteEndPoint.ToString() + "]连接关闭..."); break; } data += Encoding.UTF8.GetString(bytes, 0, bytesRec); _myLog(LogFile.Trace, "服务端收到消息:" + data); CbMessage(this._connection.RemoteEndPoint.ToString(), data); string sendStr = "ok"; byte[] bs = Encoding.UTF8.GetBytes(sendStr); _connection.Send(bs, bs.Length, 0); } catch (SocketException exp) { //方法二:赋值为NULL在主界面捕获后,删除. _connection = null; return; } } } } }

 

你可能感兴趣的:(thread,exception,socket,String,null,byte)