7

我有以下 .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 类就会正确检索。老实说,我不知道这段代码是否有效;我从来没有亲眼见过它做它应该做的事情,使用这一切的功能是为了我们的一个客户的利益而创建的,而且在我被录用之前显然没有功能。

我需要知道要寻找什么来解决错误。如果有帮助,我尝试与之建立连接的远程计算机位于 VPN 隧道的远程端,并且我们确实通过我们使用的其他软件建立了连接。

帮助?

4

1 回答 1

6

找到了。用作套接字本地端点的地址,在 SetupLocalSocket() 中,使用了类似的简单方法来获取地址;通过解析本地主机并获取第一个地址。第一个地址通常是 IPv6 地址,而不是显然预期的 IPv4 地址。因此,我让它在列表中查找第一个 IPv4 地址并将其用作端点,并且它起作用了。

于 2012-10-16T22:15:14.823 回答