Andy
Andy

Reputation: 11492

.Net - Difference between ProtocolType.IP, ProtocolType.IPv4 and ProtocolType.IPv6

I can understand the difference between ProtocolType.IPv4 and ProtocolType.IPv6, but I'm not sure where ProtocolType.IP comes into the equation and the documentation doesn't really describe what the different values mean.

Am I OK to just use IP every time and assume the resultant socket will work for both IPv4 and IPv6 addresses?

Upvotes: 2

Views: 1006

Answers (3)

Hans Passant
Hans Passant

Reputation: 942119

ProtocolType.IP has the same underlying value as ProtocolType.Unspecified, 0. The enum values match the argument value of the protocol argument passed to the WSASocket() winapi function. Which documents what 0 means:

If a value of 0 is specified, the caller does not wish to specify a protocol and the service provider will choose the protocol to use.

Which on Windows will indeed be IP, but could be IPv4 or IPv6, depending on machine config. I'd say you'd typically want to be explicit about it so you won't have to guess when there's trouble.

Upvotes: 1

CodeCaster
CodeCaster

Reputation: 151690

I think you're looking for this:

Socket socket 
  = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);

This creates a socket that can handle both IPv4 and IPv6, using TCP.

Upvotes: 0

Austin T French
Austin T French

Reputation: 5140

As I recall its functionality a from a project, you are on the right track. Using the generic protocol for say an IP address returns any IPv4 or IPv6 address, which can be OK. But on our domain we have IPv6 devices enabled, but we are only actually using IPv4, so a PC might return an IP address in hex for IPv6, but nothing can actually use or communicate by that alone (At least reliably to my knowledge) so I fell back to IPv4 forced. Now I get just the 10.xx.xxx.xxx instead of the 10-space and the IPv6 hex address.

Upvotes: 0

Related Questions