我在使用Socket.DuplicateAndClose和C#的异步/等待功能时遇到了麻烦。我已将问题分离到最小的测试用例,如下所示。请注意,使用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:
答案 0 :(得分:2)
根据MSDN:
如果创建套接字的进程使用异步方法(BeginReceive或BeginSend),则进程必须首先将UseOnlyOverlappedIO属性设置为true;否则,套接字绑定到创建进程的完成端口,这可能导致在目标进程上抛出ArgumentNullException。
虽然不是ArgumentNullException
抛出,但试试这个:
var info = client.Client.DuplicateAndClose(Process.GetCurrentProcess().Id);
info.Options |= SocketInformationOptions.UseOnlyOverlappedIO;