/// <summary> /// 开始监听Tcp连接请求 /// </summary> public void StartTcpListening() { TcpClient newClient = null; tcpEndPoint.myListener = new TcpListener(tcpEndPoint.ipAddress, tcpEndPoint.port); tcpEndPoint.myListener.Start(10);//最多监听10个连接请求 LogHelper.WriteLogInfo("开始Tcp监听,时间", DateTime.Now.ToString()); tcpRunningTimeInfo.Ht.Add("StartListening", string.Format(">>在端口{0}开始监听,等待公积金中心发起连接请求...", tcpEndPoint.port)); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["StartListening"].ToString()); while (true) { try { newClient = tcpEndPoint.myListener.AcceptTcpClient();//接收客户端的连接请求,创建一个连接通道; LogHelper.WriteLogInfo("成功与客户端建立连接", newClient.Client.RemoteEndPoint); tcpRunningTimeInfo.Ht.Add("ConnectSuccess", string.Format(">>与客户端{0}建立连接", newClient.Client.RemoteEndPoint)); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["ConnectSuccess"].ToString()); } catch (Exception ex) { tcpRunningTimeInfo.Ht.Add("ConnectFail", "建立与客户端的连接错误"); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["ConnectFail"].ToString()); LogHelper.WriteLogException("建立与客户端的连接错误", ex); break; } //在新线程中打开与客户端的TCP连接通道,进行监听; tcpUserManager = new TcpUserManager(newClient); Thread newThread = new Thread(new ParameterizedThreadStart(StartListeningClient)); newThread.Start(tcpUserManager); } }
static void Main(string[] args) { Console.WriteLine("\t\t------------银行模拟程序------------"); //银行作为服务端,监听公积金中心的请求; TcpServer bankServer = new TcpServer(); try { Thread t = new Thread(bankServer.StartTcpListening); t.Start(); } catch (Exception ex) { BasicOperation.ShowInfo(ex.Message); } }
/// <summary> /// 向客户端发送消息,byte[]; /// </summary> /// <param name="user"></param> /// <param name="msg"></param> private void SendMsgToClient(TcpUserManager user, byte[] msg) { try { Console.WriteLine("发送响应报文:\n{0},字节数:{1}", Encoding.Default.GetString(msg), msg.Length); tcpRunningTimeInfo.Ht.Add("SendMsg", string.Format("发送响应报文:\n{0},字节数:{1}", Encoding.Default.GetString(msg), msg.Length)); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["SendMsg"].ToString()); user.bw.Write(msg); LogHelper.WriteLogInfo("发送响应报文", Encoding.Default.GetString(msg)); user.bw.Flush(); } catch (System.Exception ex) { Console.WriteLine("发送响应报文时出错,错误信息:{0}", ex.Message); tcpRunningTimeInfo.Ht.Add("SendError", string.Format("发送响应报文时出错,错误信息:{0}", ex.Message)); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["SendError"].ToString()); LogHelper.WriteLogException("发送响应报文时发生异常", ex); } }
/// <summary> /// 监听客户端 /// </summary> /// <param name="tcpUserManager"></param> public void StartListeningClient(object tcpUserManager) { TcpUserManager u = tcpUserManager as TcpUserManager; TcpClient myClient = u.client; while (true) { try { string recvString = null; bllEntryPoint.Hb = Encoding.Default.GetString(u.br.ReadBytes(2)); //读取4个字节,用来确定要读取的字节数; int countRead; countRead = Convert.ToInt32(Encoding.Default.GetString(u.br.ReadBytes(4))); byte[] recvBytes = new byte[countRead]; recvBytes = u.br.ReadBytes(countRead); recvString = Encoding.Default.GetString(recvBytes); tcpRunningTimeInfo.Ht.Add("ReceiveMsg", string.Format("接收到的请求报文:{0}", recvString)); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["ReceiveMsg"].ToString()); LogHelper.WriteLogInfo("接收到请求报文", recvString); //业务处理过程; HandleRequest(u, recvBytes, bllEntryPoint); tcpRunningTimeInfo.Ht.Add("Fengexian", "----------------------------------------------------------------"); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["Fengexian"].ToString()); break; } catch (Exception ex) { Console.WriteLine("与客户端断开连接,原因:{0}", ex.Message); tcpRunningTimeInfo.Ht.Add("Unconnect", string.Format("与客户端断开连接,原因:{0}", ex.Message)); BasicOperation.ShowInfo(tcpRunningTimeInfo.Ht["Unconnect"].ToString()); LogHelper.WriteLogException("与客户端断开连接", ex); break; } } }