Socket problem migrating to Vista

V

Valerie Hough

My app currently runs C#, .NET v2.0 on Windows Xp/Windows Server 2003, using
asynchronous socket communication.

I set up my server socket:

Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
IPEndPoint ep = new IPEndPoint( IPAddress.Any, 0 );
s.Bind( ep );
int port = ep.Port;

The server socket's computer name and port number get stored in a SQL
database and new clients retrieve them to connect as follows:

Socket sClient = new Socket( AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
IPAddress ipAddress = Dns.GetHostEntry( computerName, port ).AddressList[0];
IPEndPoint ep = new IPEndPoint( ipAddress, port );
sClient.Bind( ep );

When I run the server socket application on the Vista machine and the client
socket application from the XP machine everything works fine.
When I run the server socket application on the XP machine and the client
socket application from the Vista machine everything works fine.

However, when I run both applications from Vista machines, the server socket
gets set up, but when my client tries to connect, I get the following
exception:

Error message: An address incompatible with the requested protocol was used

at System.Net.Sockets.Socket.DoBeginConnect( EndPoint endPointShapshot,
SocketAddress socketAddress, LazyAsyncResult asyncResult )
at System.Net.Sockets.Socket.BeginConnect( EndPoint remoteEP,
AsyncCallback callback, Object state )
at MySocketClass.Sockets.Connect()

My googling suggests that the problem stems from Vista by default using
IPv6 socket and the way I have my client socket created resulting in IPv4.

Is there a solution which will work correctly in all three of these
scenarios without switching versions of .NET ?

Thanks in advance,
Chris Hough
 
A

Andrew McLaren

Valerie Hough said:
However, when I run both applications from Vista machines, the server
socket
gets set up, but when my client tries to connect, I get the following
exception:
Error message: An address incompatible with the requested protocol was
used

Hi Chris,

You need to create the socket as a IPv6 Socket:

Socket s = new Socket( AddressFamily.InterNetwork, ... etc ... );

and set the IPV6_V6ONLY Socket option to FALSE:

sock.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0);

In Vista and Server 2008, the TCP/IP stack will create IPv6 Sockets, by
default. To make them accept IPv4 comms, you need to configure them as "dual
mode" sockets, IPv6 *and&* IPv4. Your socket will always be IPv6 in its
heart-of-hearts; but with IPV6_V6ONLY turned off, it will handle IPv4 stuff
as well.

There's a great article describing this in more detail (helped me to
understand it, at last):
http://blogs.msdn.com/wndp/archive/...ic-applications-part-2-dual-mode-sockets.aspx

Hope it helps,
 
V

Valerie Hough

Thanks for the reply. I was hoping to have code that would run both under
Vista as well as on older machines that did not have support for IPv6
Sockets. When I tried to use this article's approach for an older machine,
the socket did not get created.

One approach I considered was to use Try/Catch while trying to create a v6
socket, and then when this failed use my old approach, but this does not
seem to be a very robust solution.

Does anyone have any other thoughts?

Thanks in advance,
Chris Hough
 
A

Andrew McLaren

Valerie Hough said:
I was hoping to have code that would run both under Vista as well as on
older machines that did not have support for IPv6 Sockets. When I tried to
use this article's approach for an older machine, the socket did not get
created.

Maybe use System.Environment.OSVersion?

See:
How to determine the Windows version by using Visual C#
http://support.microsoft.com/kb/304283

Then put your Socket creation code in a conditional wrapper:

if (osInfo.Version.Major => 6)
// make an IPv6 sockets
else
// make an IPv4 Socket

For developer questions, you may get better results on groups like
microsoft.public.dotnet.languages.csharp or
microsoft.public.dotnet.framework. Even if it is a Vista-specific question,
very few developers are active in this
microsoft.public.windows.vista.general newsgroup.

Good luck with it!

(btw, Chris ... from here, you sure do look like Valerie, dude! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top