私は次の.NETコードを持っています。そのほとんどは私が雇われるずっと前に書かれたものであり、元の開発者はまだ私たちのために働いていません。
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);
...
}
注意すべきSocketClientコードは次のとおりです。
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;
}
SocketConnect内のエンドポイントに接続すると、次のようなSocketExceptionが発生します。
システムは、呼び出しでポインター引数を使用しようとしたときに、無効なポインターアドレスを検出しました。
オンラインの情報は、これを修正する方法について少し軽いです。AFAICT、アドレスは適切に解析されており、SocketClientクラスに渡されると適切に取得されます。正直なところ、このコードが機能したことがあるかどうかはわかりません。私はそれが想定どおりに機能するのを見たことがありません。これらすべてを使用する機能は、私たちの1人のクライアントの利益のために作成されたものであり、採用される前から機能していないようです。
エラーを解決するために何を探すべきかを知る必要があります。それが役に立ったら、私が接続を確立しようとしているリモートコンピューターはVPNトンネルのリモート側にあり、私たちが使用している他のソフトウェアを介して接続できます。
ヘルプ?