【问题标题】:Invalid Pointer Address error attempting to connect to TCP Socket尝试连接到 TCP 套接字时出现无效的指针地址错误
【发布时间】:2012-10-16 20:18:35
【问题描述】:

我有以下 .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 隧道的远程端,并且我们确实通过我们使用的其他软件建立了连接。

帮助?

【问题讨论】:

    标签: c# sockets networking


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2013-05-21
    • 2021-07-01
    • 1970-01-01
    • 2018-09-22
    相关资源
    最近更新 更多