【发布时间】:2015-03-16 04:09:06
【问题描述】:
我在将 Socket.DuplicateAndClose 与 C# 的 async/await 功能结合使用时遇到了问题。我已将问题隔离为一个最小的测试用例,如下所示。请注意,使用writer.WriteLine 而不是WriteLineAsync(和Flush 而不是FlushAsync)可以使程序成功而不会出错。但是,引入 async/await 会在 WriteLineAsync("Second socket") 行引发异常。
对于相当长的最小测试用例表示歉意,它尽可能小。
这是 .net 中的错误吗?还是我做错了什么?
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace SocketTest
{
class Program
{
// Simple hosting socket, not actually part of the problem, just something to connect to in Main()
static void HostSocket(object dummy)
{
var listen = new TcpListener(IPAddress.Any, 12345);
listen.Start();
var client = listen.AcceptTcpClient();
listen.Stop();
var reader = new StreamReader(client.GetStream());
while (true)
{
string line;
try
{
line = reader.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Server exception:");
Console.WriteLine(e);
break;
}
if (line == null)
break;
Console.WriteLine("Server: " + line);
}
reader.Dispose();
Console.WriteLine("Server closed");
}
// The main problem
static async Task RealMain()
{
// Connect to localhost and write a line to it. This works.
var client = new TcpClient("127.0.0.1", 12345);
var writer = new StreamWriter(client.GetStream());
await writer.WriteLineAsync("First socket");
await writer.FlushAsync();
// Duplicate the socket with Socket.DuplicateAndClose, passing the current PID
var info = client.Client.DuplicateAndClose(Process.GetCurrentProcess().Id);
// Close() must be after Duplicate, otherwise ObjectDisposedException
writer.Close();
// Create a TcpClient using the SocketInformation struct obtained from DuplicateAndClose
var second = new TcpClient();
second.Client = new Socket(info);
writer = new StreamWriter(second.GetStream());
// Write the second line from the new socket
await writer.WriteLineAsync("Second socket"); // EXCEPTION HERE
await writer.FlushAsync();
writer.Close();
}
// Boot the server and then start the client
static void Main(string[] args)
{
new Thread(HostSocket) { IsBackground = true }.Start();
Thread.Sleep(100);
RealMain().Wait();
Console.ReadKey(true);
}
}
}
例外是:
System.AggregateException was unhandled
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at SocketTest.Program.Main(String[] args) in c:\users\khyperia\documents\visual studio 2015\Projects\SocketTest\Program.cs:line 69
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
HResult=-2146232800
Message=Unable to write data to the transport connection: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)).
Source=System
StackTrace:
at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.IO.Stream.<BeginEndWriteAsync>b__16(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamWriter.<FlushAsyncInternal>d__1e.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at SocketTest.Program.<RealMain>d__1.MoveNext() in c:\users\khyperia\documents\visual studio 2015\Projects\SocketTest\Program.cs:line 60
InnerException:
HResult=-2147024809
Message=The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
Source=mscorlib
StackTrace:
at System.Threading.ThreadPool.BindIOCompletionCallbackNative(IntPtr fileHandle)
at System.Threading.ThreadPool.BindHandle(SafeHandle osHandle)
at System.Net.Sockets.Socket.BindToCompletionPort()
at System.Net.Sockets.BaseOverlappedAsyncResult.SetUnmanagedStructures(Object objectsToPin)
at System.Net.Sockets.OverlappedAsyncResult.SetUnmanagedStructures(Byte[] buffer, Int32 offset, Int32 size, SocketAddress socketAddress, Boolean pinSocketAddress)
at System.Net.Sockets.Socket.DoBeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError& errorCode, AsyncCallback callback, Object state)
at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
InnerException:
【问题讨论】:
-
这个问题本身是完全有效的,但由于误解,DuplicateAndClose 大部分时间都被误用。您能否解释一下为什么 DuplicateAndClose 是满足您要求的最佳解决方案?
-
@usr,我正在写一个 IRC 机器人来玩,我正在使用 AppDomains 来实现代码的重新加载以进行更新。基本上我扔掉了整个旧代码库(它在一个隔离的 AppDomain 中运行),启动另一个 AppDomain,并将套接字从旧域传递到新域(永远不会与服务器断开连接)。据我所知,套接字不能像这样在 AppDomain 之间传递,并且需要 DuplicateAndClose。当然,我可以使用保持连接的“代理”,并且客户端连接到代理而不是服务器,但 DuplicateAndClose 似乎更简单。
-
有道理。我会尝试让父/控制器 AppDomain 处理连接,并且只将机器人逻辑放入一次性 AppDomain。换句话说,跨 AppDomain 流式传输数据,而不是通过套接字。
标签: c# sockets async-await