none
求教:GPRS连接时,报“应答方调制解调器断开连接”什么原因?怎么解决 RRS feed

  • 问题

  • 我开发了一个应用,用于上传GPS Location info到服务器,开发环境是c#, wm6sdk
    第一步:我使用 ConnMgrEstablishConnectionSync连接GPRS,用的是cmwap
    第二步:当GPRS连接成功后,使用socket将数据上传
    正常情况下,GPRS都能正常连接
    但是,如果开始GPRS连接的时候,手机正在通话或者处于GPRS覆盖弱的区域,比如:地铁,GPRS连接会失败,从这开始,以后每次进行GPRS连接时,会告诉我消息:应答方调制解调器断开连接
    这是什么原因,在程序中怎么解决?
    由于是按照一定时间间隔上传,所以在连接GPRS时不能避免用户没有在进行通话和处于信号不好的地方
    代码片段如下:每5分钟调用一次send
     private  bool ConnectMobileNetwork(string csDesc)
            {
                //ConnectManager connectManager = new ConnectManager();

                List<ConnectManager.CONNMGR_DESTINATION_INFO> lst = connectManager.EnumNetIdentifier();

                int nIndex = 0;
                //选择连接
                for (; nIndex < lst.Count; nIndex++)
                {
                    if (string.Compare(lst[nIndex].szDescription, csDesc, true) == 0)
                    {
                        break;
                    }
                }
                //建立连接
                if (nIndex >= 0 && nIndex < lst.Count)
                {
                    return connectManager.EstablishConnection((uint)nIndex);
                }
                else
                {
                    _connectStatus = false;
                    GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), Utils.GetCurrentTime() + "\n");
                    GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), "no valid gprs connection\n");
                }
                return false;
            }
           
    public void Send(string imsi,string url,string  path,string param)
            {
                try
                {
                    if (ConnectMobileNetwork("单位设置"))
                    {
                        _connectStatus = true;
                        byte[] data = new byte[1024];
                        IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("10.0.0.172"), 80);
                        //使用tcp协议 stream类型 (IPV4)
                        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        try
                        {
                            socket.Connect(ipEnd);
                        }
                        catch (SocketException e)
                        {
                            connectManager.ReleaseConnection();
                            _serverStatus = false;
                            GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), Utils.GetCurrentTime() + "\n");
                            GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), "connect server exception!\n");
                            GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), e.Message);
                            return;
                        }
                       

                        StringBuilder buf = new StringBuilder();
                        buf.Append("POST ").Append(path).Append(" HTTP/1.1\r\n");
                        buf.Append("Accept:    */*\r\n");
                        buf.Append("Host:").Append("10.0.0.172:80").Append("\r\n");
                        buf.Append("X-Online-Host:   ").Append(url).Append(":80\r\n");
                        buf.Append("Connection:Keep-Alive\r\n");
                        buf.Append("Content-Type:    application/x-www-form-urlencoded\r\n");
                        buf.Append("X-Up-Sim-Card-ID: ").Append(imsi).Append("\r\n");
                        buf.Append("X-Up-Version-ID: 1.0\r\n");
                      
                        StringBuilder bufdata = new StringBuilder();
                        bufdata.Append("Content-Length: ").Append(param.Length).Append("\r\n");
                        bufdata.Append("\r\n");
                        bufdata.Append(param).Append("\r\n");
                        bufdata.Append("\r\n");
                        byte[] ms = System.Text.UTF8Encoding.UTF8.GetBytes(buf.ToString()+bufdata.ToString());
                        //GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), Encoding.Default.GetString(ms,0,ms.Length)+ "\n");
                        //发送
                        socket.Send(ms);
                        //接收返回
                        int recv = 0;
                        recv = socket.Receive(data);
                        string rec = Encoding.Default.GetString(data, 0, recv);
                        if (rec.IndexOf("200 OK") >= 0)
                        {
                            if (String.Compare(rec.Substring(rec.LastIndexOf("\r\n") + 2), "0", true) == 0)
                            {
                                GpsFileUtil.Append(GpsFileUtil.GetOkFileName(), param);
                                _serverStatus = true;
                            }
                            else
                            {
                                _serverStatus = false;
                                GpsFileUtil.Append(GpsFileUtil.GetNetFileName(), param);
                                GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), Utils.GetCurrentTime() + "\n");
                                GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), "server response:" + rec.Substring(rec.LastIndexOf("\r\n") + 1)+"\n");
                            }
                        }
                        else
                        {
                            _serverStatus = false;
                            GpsFileUtil.Append(GpsFileUtil.GetNetFileName(), param);
                            GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), Utils.GetCurrentTime() + "\n");
                            GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), "server response Exception!\n" + rec);
                        }
                         //禁用上次的发送和接受
                        socket.Shutdown(SocketShutdown.Both);
                        socket.Close();                 

                    }
                    else
                    {
                        _connectStatus = false;
                        GpsFileUtil.Append(GpsFileUtil.GetNetFileName(), param);
                        GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), Utils.GetCurrentTime() + "\n");
                        GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), "CMWAP GPRS connect fail\n");
                    }
                   

                }
                catch (Exception ex)
                {
                    _serverStatus = false;
                    GpsFileUtil.Append(GpsFileUtil.GetNetFileName(), param);
                    GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), Utils.GetCurrentTime() + "\n");
                    GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), "send data fail\n");
                    GpsFileUtil.Append(GpsFileUtil.GetLogFileName(), ex.Message + "\n");
                }
                finally
                {
                    connectManager.ReleaseConnection();
                }

    2009年4月17日 7:06

答案

  • 试试把 
                       socket.Shutdown(SocketShutdown.Both);
                        socket.Close();    
    放到finally中

      


    I'm a device.
    2009年4月17日 15:44
    版主

全部回复

  • 试试把 
                       socket.Shutdown(SocketShutdown.Both);
                        socket.Close();    
    放到finally中

      


    I'm a device.
    2009年4月17日 15:44
    版主
  • 好的,我试试你的建议,不过从日志的情况来看, if (ConnectMobileNetwork("单位设置"))这里就返回的是false,所以socket相关的代码应该是没有被指行到,而且这个时候,即使我使用别的应用(IE 接入点cmnet)试图建立GPRS连接,系统也会弹出提示框,报同样的错误
    2009年4月18日 11:44