在运行 Windows 7 x64 和 VS2012 U1 并使用 .Net 4 的两个系统上,如果我调用 UdpClient 或 TcpClient,则会收到错误“提供了无效参数”。
我已经尝试了许多端口,并且我已经验证了这些端口尚未绑定,但我每次都会遇到相同的错误。
我也尝试以提升的权限运行代码无济于事。
测试代码:
try
{
using (UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 123)))
{
Console.WriteLine("Port is open");
}
}
catch (SocketException error)
{
Console.WriteLine("Error:\n{0}", error.Message);
}
错误输出:
System.Net.Sockets.SocketException was unhandled
HResult=-2147467259
Message=An invalid argument was supplied
Source=System
ErrorCode=10022
NativeErrorCode=10022
StackTrace:
at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
at System.Net.Sockets.UdpClient..ctor(IPEndPoint localEP)
at GetNTPTime.Program.Main(String[] args) in z:\My Documents\Dropbox\Dev\C#\_Example\GetNTPTime\GetNTPTime\Program.cs:line 15
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
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:
作为一个注释,如果我简单地调用UdpClient client = new UdpClient();我会得到同样的错误。