问题描述
用Windows服务写的一个Socket异步服务器,用来解析和处理客户端上传的数据,客户端有100多个吧,刚开始运行正常,几个小时后,服务还在运行,但不能解析客户端的数据了,而且客户端也连不上了,重新了一下服务,又好了,求高手解决,主要代码如下:privateManualResetEventallDone=newManualResetEvent(false);//启动服务方法publicvoidInitRfidServer(){try{//实例化套接字(ip4寻址协议,流式传输,TCP协议)rifdServerSocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建网络节点对象包含ip和portIPEndPointendpoint=newIPEndPoint(IPAddress.Parse(rfidIP),rfidPort);//将监听套接字绑定到对应的IP和端口rifdServerSocket.Bind(endpoint);//设置监听队列长度为300(同时能够处理300个连接请求)rifdServerSocket.Listen(300);rifdServerSocket.IOControl(IOControlCode.KeepAliveValues,keepAlive(1,1000,1000),null);}catch(Exceptionex){Common.WriteLogFile("SOCKET",3,"InitRfidServer():"+ex.Message);return;}rfidServerThread=newThread(StartWatch);rfidServerThread.IsBackground=true;rfidServerThread.Start();}//监听privatevoidStartWatch(){try{while(isWatch){allDone.Reset();rifdServerSocket.BeginAccept(newAsyncCallback(AcceptCallBack),rifdServerSocket);allDone.WaitOne();}rifdServerSocket.Close();}catch(Exceptionex){Common.WriteLogFile("SOCKET",3,"StartWatch():"+ex.Message);}}publicconstint_bufferSize=1024;classStateObject{publicSocketworkSocket=null;publicbyte[]buffer=newbyte[_bufferSize];}//接收到客户端连接privatevoidAcceptCallBack(IAsyncResultar){allDone.Set();Socketlistener=(Socket)ar.AsyncState;if(listener!=null){Sockethandler=listener.EndAccept(ar);StateObjectstate=newStateObject();state.workSocket=handler;ConnectionClientconnection=newConnectionClient(handler);//将负责与当前连接请求客户端通信的套接字所在的连接通信类对象装入集合lock(obj){if(!dictConn.Keys.Contains(handler.RemoteEndPoint.ToString())){RequestInfoMessage("SOCKET",0,"建立连接"+handler.RemoteEndPoint.ToString());dictConn.Add(handler.RemoteEndPoint.ToString(),connection);stringremoteIP=(handler.RemoteEndPointasIPEndPoint).Address.ToString();DbHelperSQL.connectionString=PubConstant.LocalConString;DbHelperSQL.RunProcedure("proc_UpdateConStatus",newSqlParameter[3]{newSqlParameter("ip",remoteIP),newSqlParameter("status",1),newSqlParameter("mytime",DateTime.Now)});if(dicRfidIPSnapIP.ContainsKey(remoteIP))//每次检漏3重新连接时让摄像机重新登录,因为断电上电后,摄像机需要重新登录{SnapReLogin(dicRfidIPSnapIP[remoteIP]);}}else{RequestInfoMessage("SOCKET",0,string.Format("dictConn已经包含{0}的连接了",handler.RemoteEndPoint.ToString()));Common.WriteLogFile("SOCKET",3,string.Format("dictConn已经包含{0}的连接了",handler.RemoteEndPoint.ToString()));}}handler.BeginReceive(state.buffer,0,_bufferSize,0,newAsyncCallback(readCallBack),state);}}///<summary>///异步接收客户端发送来的信息///</summary>///<paramname="ar"></param>privatevoidreadCallBack(IAsyncResultar){stringremotePoint=string.Empty;StateObjectstate=null;byte[]receiveBuff;Sockethandler=null;try{state=(StateObject)ar.AsyncState;handler=state.workSocket;remotePoint=handler.RemoteEndPoint.ToString();if(handler.Connected){intread=handler.EndReceive(ar);if(read>0)lock(obj){receiveBuff=state.buffer;RecMsg(receiveBuff,handler.RemoteEndPoint,read);//}}else{RemoveClientConnection(remotePoint);return;}ar.AsyncWaitHandle.Close();//清空数据,重新开始异步接收receiveBuff=newbyte[_bufferSize];handler.BeginReceive(state.buffer,0,_bufferSize,0,newAsyncCallback(readCallBack),state);}else{RemoveClientConnection(remotePoint);}}catch(Exceptionex){Common.WriteLogFile("SOCKET",3,"readCallBack():"+ex.Message);RemoveClientConnection(remotePoint);}}privatebyte[]keepAlive(intonOff,intkeepAliveTime,intkeepAliveInterval){byte[]buffer=newbyte[12];BitConverter.GetBytes(onOff).CopyTo(buffer,0);BitConverter.GetBytes(keepAliveTime).CopyTo(buffer,4);BitConverter.GetBytes(keepAliveInterval).CopyTo(buffer,8);returnbuffer;}
解决方案
解决方案二:
所以while的代码我觉得应该不完全属于"异步",其次所谓的异步里面应该也看不到thread之类的代码.而且allDone这是什么东西,反正我写代码从来没用过照样能连接很多"客户端"..
解决方案三:
那个While是监听客户端连接的,没有的话,只能连接一个客户端吧?allDone是线程的控制信号