尝试连接到TCP套接字时出现无效的指针地址错误

时间:2012-10-16 20:18:36

标签: c# sockets networking

我有以下.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隧道的远程端,我们通过我们使用的其他软件进行连接。

帮助?

1 个答案:

答案 0 :(得分:4)

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