问题描述
求大神帮我把这个Socket改得更稳定点,能链接的客户端数量更多点,当然如果有更好的方案更好,谢谢了。代码如下usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net.Sockets;usingSystem.IO;usingSystem.Threading;usingSystem.Net;usingExceptionEx;namespaceSocketServer{publicclassReceive{///<summary>///Socket通信集合///</summary>publicstaticDictionary<string,User>dictionary=newDictionary<string,User>();///<summary>///需要重新获取IMEI号集合///</summary>publicstaticDictionary<string,int>dcount=newDictionary<string,int>();///<summary>///保存TcpListener对象///</summary>publicstaticTcpListenermyListener=null;#region接受消息///<summary>///接受消息///</summary>publicstaticvoidTcpMain(){try{//端口intport=9023;if(port==0)return;//IP地址stringhost="127.0.01";#region创建终结点(EndPoint)//把ip地址字符串转换为IPAddress类型的实例IPAddressip=IPAddress.Parse(host);//用指定的端口和ip初始化IPEndPoint类的新实例IPEndPointipe=newIPEndPoint(ip,port);#endregion#region创建socket并开始监听myListener=newTcpListener(ipe);myListener.Start();#endregion#region接受到client连接,为此连接建立新的socket,并接受信息TcpClientnewClient=null;while(myListener!=null){try{newClient=myListener.AcceptTcpClient();//为新建连接创建新的socket}catch(Exceptionce){ExceptionEx.MyExceptionLog.AddLogError("创建链接失败:"+ce.Message);//CloseSocket();break;}Useruser=newUser(newClient);ThreadOneThread=newThread(newParameterizedThreadStart(OneThreadDo));OneThread.Start(user);//ExceptionEx.MyExceptionLog.AddLogError("建立连接"+DateTime.Now);}#endregion}catch(Exceptionce){ExceptionEx.MyExceptionLog.AddLogError(ce.Message+"Socket");}}#endregion#region处理消息///<summary>///处理消息///</summary>///<paramname="value">Socket对象</param>publicstaticvoidOneThreadDo(Objectvalue){Useruser=(User)value;TcpClienttcp=user.client;//遗漏消息stringylmsg="";while(tcp!=null){stringmgs=null;try{byte[]byteR=newbyte[1024*1024];intbytesRead=user.br.Read(byteR,0,byteR.Length);if(bytesRead==0)return;mgs=Encoding.GetEncoding("GBK").GetString(byteR,0,bytesRead);Console.WriteLine(mgs);mgs=mgs.Trim().Replace("","");if(!mgs.Contains("|#")){ylmsg=ylmsg+mgs;continue;}if(!string.IsNullOrEmpty(ylmsg)){mgs=ylmsg+mgs;ylmsg="";}}catch(Exceptionce){ExceptionEx.MyExceptionLog.AddLogError("链接断开:"+ce.Message);break;}try{ExceptionEx.MyExceptionLog.AddLogError("接收到的信息:"+mgs);#region接收信息string[]_msgs=mgs.Split(newstring[]{"|#"},StringSplitOptions.RemoveEmptyEntries);#endregion#region给client端返回信息stringsendStr="";byte[]bs=Encoding.UTF8.GetBytes(sendStr+"|#");user.bw.Write(bs,0,bs.Length);//返回信息给客户端user.bw.Flush();#endregion#region把通信渠道保存到字典//if(msgs.Length>=2&&!dictionary.ContainsKey(msgs[1]))//{//dictionary.Add(msgs[1],user);//}//elseif(msgs.Length>=2&&dictionary.ContainsKey(msgs[1]))//{//dictionary[msgs[1]]=user;//}#endregion}catch(Exceptionce){ExceptionEx.MyExceptionLog.AddLogError(ce.Message+"Socket2");break;}}}#endregion#region发送信息///<summary>///发送信息///</summary>///<paramname="SendType">类型(报警信息,同屏开始信息,同屏结束信息)</param>///<paramname="TerminalNumber">终端编号</param>///<paramname="Port">端口号</param>///<paramname="linkMode">同屏方式</param>publicstaticvoidSend(stringSendType,stringTerminalNumber,stringPort,intLinkMode){try{Userm_User=dictionary[TerminalNumber];//发送内容stringsendStr="";byte[]bs=Encoding.UTF8.GetBytes(sendStr);//把字符串编码为字节m_User.bw.Write(bs,0,bs.Length);m_User.bw.Flush();}catch(Exceptionce){ExceptionEx.MyExceptionLog.AddLogError(ce.Message+";终端编号:"+TerminalNumber+"端口号:"+Port+"同屏方式:"+LinkMode);throw;}}#endregion#region用户信息publicclassUser{publicTcpClientclient{get;privateset;}publicBinaryReaderbr{get;privateset;}publicBinaryWriterbw{get;privateset;}publicUser(TcpClientclient){this.client=client;NetworkStreamnetworkStream=client.GetStream();br=newBinaryReader(networkStream,Encoding.GetEncoding("GBK"));bw=newBinaryWriter(networkStream);}publicvoidClose(){br.Close();bw.Close();client.Close();}}#endregion}}
解决方案
解决方案二:
@本拉灯在不在呢求回复
解决方案三:
我就这点分数了,没有更多了后面又更多了再扑发给你们