Search code examples
c#socketsexceptionnetwork-programmingallocation

Can not allocate new socket, throwing invalid argument exception


I created a new project and the single line of code is the allocation of a new socket:

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

And it throws an exception:

An unhandled exception of type system.net.sockets.socket exception occurred in System32.DLL

Additional information: An invalid argument was supplied

It also writes those codes: 0x80004005, ErrorCode: 10022


Solution

  • I hope my late answer is still useful for others, who have the same issue.

    I have had the same issues with several networking/socket examples and every searching (parameter combination mismatch?/Disconnect instead of Close/optional settings to reuse address or socket) did NOT fix it. This 'invalid argument' error message was misleading. Now, I figured this out:

    Most likely, you are also running the code in Visual Studio or the Executable (exe) directly on a network volume or share. Even executed as an Administrator will not work.

    When I copied the executable/code project to a local volume on my PC, all socket programs worked well (even without Administrator rights).

    Probably, a change of your User Account Control (UAC) rights or your companies domain group policies could circumvent this issue.

    Regards F.J.