TcpClient and UdpClient throwing errors

2019-07-26 09:19发布

On two systems running Windows 7 x64 and VS2012 U1 and using .Net 4 I get the error "An invalid argument was supplied" if I call UdpClient or TcpClient.

I've tried a number of ports and I've verified that the ports are not already bound but I get the same error every time.

I've also tried running the code with elevated privileges to no avail.

Test code:

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);
        }

Error Output:

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:

As a note I get the same error if I simply call UdpClient client = new UdpClient();

2条回答
贼婆χ
2楼-- · 2019-07-26 10:07

I see your using port 123 in your example, that may be a bad choice as it falls within the reserved range, in particular 123 is used by the Network Time Protocol. Try to use unreserved ports above 1024.

If you still get an error with

UdpClient client = new UdpClient();

my guess is there is a problem with your system. Try running the same executable on a different machine if you can.

查看更多
姐就是有狂的资本
3楼-- · 2019-07-26 10:14

The issue was that my code was being executed from a Dropbox folder and for some reason that was causing the issues even if Dropbox wasn't running on the system at the time. If I moved the code base to the desktop and run it everything works.

Update

I did some more research and I've found that it is not Dropbox causing the issue but Cloudfogger, if I close Cloudfogger everything works.

查看更多
登录 后发表回答