I have the following .NET code. Most of it was written long before I was hired, and none of the original devs still work for us.
private void SendTCPMessage(string IpAddress, string Message)
{
...
//original code that fails because the Host entry produced
//has no elements in AddressList.
//IPHostEntry remoteMachineInfo = Dns.GetHostEntry(IpAddress);
//New code that fails when connecting
IPHostEntry remoteMachineInfo;
try
{
remoteMachineInfo = Dns.GetHostEntry(IpAddress);
if (remoteMachineInfo.AddressList.Length == 0)
remoteMachineInfo.AddressList =
new[]
{
new IPAddress(
//Parse the string into the byte array needed by the constructor;
//I double-checked that the correct address is produced
IpAddress.Split('.')
.Select(s => byte.Parse(s))
.ToArray())
};
}
catch (Exception)
{
//caught and displayed in a status textbox
throw new Exception(String.Format("Could not resolve or parse remote host {0} into valid IP address", IpAddress));
}
socketClient.Connect(remoteMachineInfo, 12345, ProtocolType.Tcp);
...
}
The SocketClient code of note is as follows:
public void Connect(IPHostEntry serverHostEntry, int serverPort, ProtocolType socketProtocol)
{
//this line was causing the original error;
//now AddressList always has at least one element.
m_serverAddress = serverHostEntry.AddressList[0];
m_serverPort = serverPort;
m_socketProtocol = socketProtocol;
Connect();
}
...
public void Connect()
{
try
{
Disconnect();
SocketConnect();
}
catch (Exception exception) ...
}
...
private void SocketConnect()
{
try
{
if (SetupLocalSocket())
{
IPEndPoint serverEndpoint = new IPEndPoint(m_serverAddress, m_serverPort);
//This line is the new point of failure
socket.Connect(serverEndpoint);
...
}
else
{
throw new Exception("Could not connect!");
}
}
...
catch (SocketException se)
{
throw new Exception(se.Message);
}
...
}
...
private bool SetupLocalSocket()
{
bool return_value = false;
try
{
IPEndPoint myEndpoint = new IPEndPoint(m_localAddress, 0);
socket = new Socket(myEndpoint.Address.AddressFamily, SocketType.Stream, m_socketProtocol);
return_value = true;
}
catch (SocketException)
{
return_value = false;
}
catch (Exception)
{
return_value = false;
}
return return_value;
}
When connecting to the endpoint within SocketConnect, I get a SocketException stating:
The system detected an invalid pointer address in attempting to use a pointer argument in a call.
Information online is a bit light on how to fix this. AFAICT, the address is parsing properly, and it's retrieved properly once passed in to the SocketClient class. Honestly, I don't know if this code has ever worked; I have never personally seen it do what it's supposed to, and the functionality that uses all this was created for the benefit of a single client of ours, and has apparently not been functional since before I was hired.
I need to know what to look for to resolve the error. If it helps, the remote computer to which I am trying to establish a connection is on the remote side of a VPN tunnel, and we do have connectivity via other pieces of software we use.
Help?
Found it. The address used as the local endpoint for the socket, in SetupLocalSocket(), used a similarly naive method of getting the address; by resolving the local host and getting the first address. That first address, more often than not, is an IPv6 address, not the IPv4 address that was obviously expected. So, I had it look for the first IPv4 address in the list and use that as the endpoint, and it worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With