socket第二次连接服务器时,服务器接收函数,总是超时

.Net技术 码拜 9年前 (2016-03-07) 995次浏览
客户端代码:

            var client = new ConnectionClient.AsyncBinarySocketClient(8192, 8192, 3000, 3000);
            client.RegisterServerNode("1122", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8888));
            byte[] bytes = null;
            string str = "njyang110"+"\t"+"123456";
            bytes = Encoding.Unicode.GetBytes(ConnectionBase.SerializeHelper.StringToBinaryString(str));
            for (int i = 0; i < 12; i++)
            {
                client.Send("Login", bytes, res => BitConverter.ToInt32(res.Buffer, 0)).ContinueWith(c =>
                {
                    if (c.IsFaulted)
                    {
                        Console.WriteLine(c.Exception.ToString());
                        return;
                    }
                });
                System.Threading.Thread.Sleep(50);
            }
            client.UnRegisterServerNode("1122");
            client.Stop();
            Console.ReadLine();

上面的代码 可以把消息循环发送完成,假如把for循环往上面移动一层 ,只能发送成功一次消息 然后就就开始超时了。客户端有做短线重新链接的功能,但是也是现实超时。请指点这是问什么呢
修改后的代码如下【修改后超时】

            for (int i = 0; i < 1200; i++)
            {
                var client = new ConnectionClient.AsyncBinarySocketClient(8192, 8192, 3000, 3000);
                client.RegisterServerNode("主服务器", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8401));
                byte[] bytes = null;
                string str = "njyang110"+"\t"+"123456";
                bytes = Encoding.Unicode.GetBytes(ConnectionBase.SerializeHelper.StringToBinaryString(str));
       
                client.Send("Login", bytes, res => BitConverter.ToInt32(res.Buffer, 0)).ContinueWith(c =>
                {
                    if (c.IsFaulted)
                    {
                        Console.WriteLine(c.Exception.ToString());
                        return;
                    }
                });
                System.Threading.Thread.Sleep(50);
                client.UnRegisterServerNode("主服务器");
                client.Stop();
            }
            Console.ReadLine();
解决方案

35

90%是服务端accept一次然后就没下文了

5

引用 LZ luluyy 的回复:

客户端代码:

            var client = new ConnectionClient.AsyncBinarySocketClient(8192, 8192, 3000, 3000);
            client.RegisterServerNode("1122", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8888));
            byte[] bytes = null;
            string str = "njyang110"+"\t"+"123456";
            bytes = Encoding.Unicode.GetBytes(ConnectionBase.SerializeHelper.StringToBinaryString(str));
            for (int i = 0; i < 12; i++)
            {
                client.Send("Login", bytes, res => BitConverter.ToInt32(res.Buffer, 0)).ContinueWith(c =>
                {
                    if (c.IsFaulted)
                    {
                        Console.WriteLine(c.Exception.ToString());
                        return;
                    }
                });
                System.Threading.Thread.Sleep(50);
            }
            client.UnRegisterServerNode("1122");
            client.Stop();
            Console.ReadLine();

上面的代码 可以把消息循环发送完成,假如把for循环往上面移动一层 ,只能发送成功一次消息 然后就就开始超时了。客户端有做短线重新链接的功能,但是也是现实超时。请指点这是问什么呢
修改后的代码如下【修改后超时】

            for (int i = 0; i < 1200; i++)
            {
                var client = new ConnectionClient.AsyncBinarySocketClient(8192, 8192, 3000, 3000);
                client.RegisterServerNode("主服务器", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8401));
                byte[] bytes = null;
                string str = "njyang110"+"\t"+"123456";
                bytes = Encoding.Unicode.GetBytes(ConnectionBase.SerializeHelper.StringToBinaryString(str));
       
                client.Send("Login", bytes, res => BitConverter.ToInt32(res.Buffer, 0)).ContinueWith(c =>
                {
                    if (c.IsFaulted)
                    {
                        Console.WriteLine(c.Exception.ToString());
                        return;
                    }
                });
                System.Threading.Thread.Sleep(50);
                client.UnRegisterServerNode("主服务器");
                client.Stop();
            }
            Console.ReadLine();

client的定义不能放在循环里面,
否则每一轮就会生成一个新的client,
前一段代码正常是连接对象生成一次,多次使用


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明socket第二次连接服务器时,服务器接收函数,总是超时
喜欢 (0)
[1034331897@qq.com]
分享 (0)