首页 新闻 会员 周边

C# Vs2010 调试时代码正常执行,运行时 do-while 只执行了一次

0
悬赏园豆:30 [已关闭问题] 关闭于 2014-07-01 13:45
    /// <summary>
    /// 连接服务
    /// </summary>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static string GetContentFromUrl(string url, string data)
    {
        Socket sokClient = null;
        string Message = "";
        try
        {


            sokClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress address = IPAddress.Parse(url);
            IPEndPoint point = new IPEndPoint(address, 8000);
            sokClient.Blocking = true;
            sokClient.Connect(point);

            char[] cmgs = data.ToCharArray(); //字符串转字符数组
            byte[] message = new byte[1024];
            message = Encoding.Default.GetBytes(data);
            sokClient.Send(message);

            byte[] totalBytes = null;
            int receiveLength = 0;
            do
            {
                byte[] buffer = new byte[1024];
                receiveLength = sokClient.Receive(buffer, buffer.Length, 0);
                if (totalBytes == null)
                {
                    totalBytes = new byte[receiveLength];
                    Buffer.BlockCopy(buffer, 0, totalBytes, 0, receiveLength);
                }
                else
                {
                    byte[] appendBytes = new byte[receiveLength];
                    Buffer.BlockCopy(buffer, 0, appendBytes, 0, receiveLength);
                    byte[] newTotalBytes = new byte[receiveLength + totalBytes.Length];
                    Buffer.BlockCopy(totalBytes, 0, newTotalBytes, 0, totalBytes.Length);
                    Buffer.BlockCopy(appendBytes, 0, newTotalBytes, totalBytes.Length, appendBytes.Length);
                    totalBytes = newTotalBytes;
                }


                //Message = Message + Encoding.Default.GetString(buffer, 0, bytes);

                if (sokClient.Available <= 0)
                {
                    break;
                }

            } while (receiveLength > 0 && receiveLength == 1024);

            Message = Encoding.Default.GetString(totalBytes);

            return Message;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        finally
        {
            sokClient.Close();
        }
SSS.M.AAA的主页 SSS.M.AAA | 初学一级 | 园豆:5
提问于:2014-07-01 11:43
< >
分享
所有回答(3)
0

while条件不满足,那就只执行一次了。

幻天芒 | 园豆:37175 (高人七级) | 2014-07-01 11:55

   调试时是正常的

支持(0) 反对(0) SSS.M.AAA | 园豆:5 (初学一级) | 2014-07-01 11:56

@锈戒: 你用Consle或其他方式,打印while里面的结果看看。

支持(0) 反对(0) 幻天芒 | 园豆:37175 (高人七级) | 2014-07-01 11:58
0

调试时你看得到正常执行,不调试你能看得到不正常执行?你怎么知道只执行一次???

拾梦小侠ด้้้ | 园豆:713 (小虾三级) | 2014-07-01 12:41

   因为数据少了呗

支持(0) 反对(0) SSS.M.AAA | 园豆:5 (初学一级) | 2014-07-01 12:42

@锈戒: Message = Encoding.Default.GetString(totalBytes);

    return Message;

你的返回都不是一个叠加的过程,怎么可能给你返回多数据,执行多少次都只会给你返回最后一条吧

支持(0) 反对(0) 拾梦小侠ด้้้ | 园豆:713 (小虾三级) | 2014-07-01 12:44

@阿 牛:   totalbytes 已经是叠加完成的数据了  ~~好了     

            判断问题  if (sokClient.Available <= 0) { break; }

支持(0) 反对(0) SSS.M.AAA | 园豆:5 (初学一级) | 2014-07-01 13:43
0

 删除  if (sokClient.Available <= 0) { break; }

SSS.M.AAA | 园豆:5 (初学一级) | 2014-07-01 13:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册