一、问题
DotNetty使用ConnectAsync连接服务器,在VS中调试运行,一切正常;而在打包目录中直接运行就会报错。
二、相关代码
private async Task ConnectServerAsync()
{
try
{
var con = new ConnectHandler(m_sLoginRet);
con.OnChatServerConnected += (sender, e) =>
{
ConnectCallBack(true);
};
con.OnChatServerDisConnected += (sender, e) =>
{
ConnectCallBack(false);
};
con.OnRecieveMessage += (sender, e) =>
{
Msg msg = e.data as Msg;
this.m_mainForm.OnRecieveMessage(msg);
};
this.m_mainForm.m_connection = con;
var group = new MultithreadEventLoopGroup();
var bootstrap = new Bootstrap();
bootstrap
.Group(group)
.Channel<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
IChannelPipeline pipeline = channel.Pipeline;
pipeline.AddLast("frameDecoder", new ProtobufVarint32FrameDecoder());
pipeline.AddLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
pipeline.AddLast("decoder", new ProtobufDecoder(Msg.Parser));
pipeline.AddLast("encoder", new ProtobufEncoder());
pipeline.AddLast("ping", new IdleStateHandler(0, 10, 0));
pipeline.AddLast("connect", con);
}));
**_IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse("192.168.20.84"), 9877));_**
}
catch (Exception ex)
{
ConnectCallBack(false, ex.StackTrace);
}
finally
{
//group.ShutdownGracefullyAsync().Wait(10000);
}
}
private void ConnectChatServer()
{
try
{
this.Text = this.m_strAppName + " 正在连接聊天服务器,请稍后...";
Task.Run(() => ConnectServerAsync());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
1.ConnectChatServer 函数在主线程中调用。
2.IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse("192.168.20.84"), 9877)); 在VS中调试运行一切正常。而手动运行则会报错。
三、报错信息
-
[DotNetty.Transport.Channels.ClosedChannelException] {DotNetty.Transport.Channels.ClosedChannelException: 发生 I/O 错误。
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 DotNetty.Transport.Bootstrapping.Bootstrap.d__15.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
在 TotRobot.LoginForm.d__21.MoveNext()} DotNetty.Transport.Channels.ClosedChannelException请问有大神碰到过类似的情况吗?不胜感激!!!
C币不多,如果确实能解决问题或对解决问题有帮助请私信我。