2

I have a function to connect to my ftp

   clientSocket = new Socket(AddressFamily.Unspecified, SocketType.Stream, ProtocolType.Tcp);
   clientSocket.Connect(new IPEndPoint(Dns.Resolve(remoteAddress).AddressList[0], remotePort));

But, with some computers, this doesn't work. The exception is

The system detected an invalid pointer address when it tried to use a pointer argument of a call.

If I change AddressFamily.Unspecified to AddressFamily.InterNetwork, problem is solved. I have to understand why there is this problem, why the original code works with certains computers but not with everyone... If someone could help me :)

Loïc Haye
  • 61
  • 2
  • 8

1 Answers1

2

When using AddressFamily.Unspecified, you are probably using the first address which can be IPv6 and not IPv4. From this MSDN website, you can see that using AddressFamily.InterNetwork uses the IPv4 address.

It is probably safer to get all the addresses and then use the first IPv4 address. This answer will show you how: Get IPv4 addresses from Dns.GetHostEntry()

Community
  • 1
  • 1
Olivier
  • 921
  • 10
  • 19
  • Thank you for the anwer. I tried IPHostEntry hostInfo = Dns.GetHostByName(remoteAddress); for (int index = 0; index < hostInfo.AddressList.Length; index++) { System.Windows.Forms.MessageBox.Show(hostInfo.AddressList[index].ToString()); } It returns just one IP address and it is an IPv4 – Loïc Haye Jan 14 '16 at 14:12
  • Did you try it on the computer where the exception is triggered? – Olivier Jan 14 '16 at 14:17
  • yes, i have it on my computer (precedent dev hadn't it, with another computer) – Loïc Haye Jan 14 '16 at 15:15