gpt4 book ai didi

c# - 使用异步/等待的 Socket.DuplicateAndClose 因 E_INVALIDARG 而失败

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:07 35 4
gpt4 key购买 nike

我在将 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);
}
}
}

异常(exception)情况是:

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:

最佳答案

根据 MSDN :

If the process creating the socket uses asynchronous methods (BeginReceive or BeginSend), the process must first set the UseOnlyOverlappedIO property to true; otherwise, the socket is bound to the completion port of the creating process, which may cause an ArgumentNullException to be thrown on the target process.

虽然它不是抛出的 ArgumentNullException,但试试这个:

var info = client.Client.DuplicateAndClose(Process.GetCurrentProcess().Id);
info.Options |= SocketInformationOptions.UseOnlyOverlappedIO;

关于c# - 使用异步/等待的 Socket.DuplicateAndClose 因 E_INVALIDARG 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064176/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com