当前位置:编程学习 > C#/ASP.NET >>

socket连接gprs成功后,断线无法重连。

自己做的socket连接gprs程序,可以连接gprs设备。但gprs设置断线(如:断电、网络原因等),却无法重连。
自己觉得好象是程序卡在什么地方。希望各位不吝赐教。
以下是部分程序代码:
public void ClientConnect()
            {
                while (true)
                {
                    mySocket = tcpListener.AcceptSocket();//将新连接挂起,直到连接成功

                      IPEndPoint clientep = (IPEndPoint)mySocket.RemoteEndPoint;//获取远程IP和端口号
                    
                    strCliIP = clientep.Address.ToString();//获取远程IP
                    strCliPort = clientep.Port.ToString(); //获取远程端口号

                    this.BeginInvoke(new ShowDelegate(Cli_IP));//委托并显示远程IP和端口号               
                                       
                    bytes = mySocket.Receive(ReveiceData, ReveiceData.Length, 0);
                    recvStr = Encoding.ASCII.GetString(ReveiceData, 0, bytes);
                   
                    this.BeginInvoke(new ShowDelegate(dbString));//委托并显示远程IP和端口号
                    
                    
                    if (recvStr.StartsWith("\r"))
                    {
                        strZbm = recvStr.Substring(1, 3);
                    }

                    if (htable.Count <= 0)
                    {
                        htable.Add(strZbm, mySocket);//调用Hashtable  
                        this.BeginInvoke(new ShowDelegate(this.Cli_IP_Port));//委托并显示远程IP和端口号
                        this.BeginInvoke(new ShowDelegate(this.Hashtable_H));//委托并显示Hashtable
                    }
                    else
                    {
                        if (htable.Contains(strZbm))
                        {
                            htable.Remove(strZbm);
                            htable.Add(strZbm, mySocket);
                            this.BeginInvoke(new ShowDelegate(this.Cli_IP_Port));//委托并显示远程IP和端口号
                            this.BeginInvoke(new ShowDelegate(this.Hashtable_H));//委托并显示Hashtable
                        }
                        else
                        {
                            htable.Add(strZbm, mySocket);
                            this.BeginInvoke(new ShowDelegate(this.Cli_IP_Port));//委托并显示远程IP和端口号
                            this.BeginInvoke(new ShowDelegate(this.Hashtable_H));//委托并显示Hashtable
                        }
                    }                                        
                    ReadT();
                }
            }

            
            public void ReadT()
            {
                thread = new Thread(new ThreadStart(ClientConnect));
                
                while (true)
                {                    
                    if (tcpListener.Pending())//判断是否有新连接                  
                    {
                        ClientConnect();
                  }
                    else
                    {
                        foreach (DictionaryEntry de in htable)//遍历读Hashtable
                        {
                            string _clientName = de.Key as string;//获得Hashtable中套接字的句柄
                            Socket _clientSkt = de.Value as Socket;//获得Hashtable中的套接字

                            bytes = _clientSkt.Receive(ReveiceData, ReveiceData.Length, 0);//获得数据长度
                            recvStr = Encoding.ASCII.GetString(ReveiceData, 0, bytes);//将ASCII类型转换为STRING类型
                            //数据插入!!!!!!!!                            
           this.BeginInvoke(new ShowDelegate(dbString));//委托并显示远程IP和端口号   
                                                        
                        }
                    }
                }
            } --------------------编程问答-------------------- --------------------编程问答-------------------- GPRS非正常中断后,Socket无法检测到断开。建议才用异步socket,事件方式,我也做GPRS网络Socket,没有问题。 --------------------编程问答-------------------- 通讯程序,尤其是无线通讯,都用做心跳检测的。一般根据心跳包的返回结果判断连接状态 --------------------编程问答-------------------- GPRS设备的地址会有变化吧
--------------------编程问答-------------------- 断线以后,如果下位机重新联网,其分配的地址是变的,上位机不会知道;下位机可以加入心跳包,因为上位机端口和IP不变,这样上位机就可知道下位机的“地址”了。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,