Exemplo n.º 1
0
        close()
        {
            if (socket != null)
            {
                if (trace.enabled(2))
                {
                    trace.write(title + ": closing network connection");
                }

                try { socket.Close(); }
                catch (Exception /* ignore */) { }
                finally
                {
                    socket    = null;
                    ipAddress = null;
                }
            }

            return;
        }         // close
Exemplo n.º 2
0
        /*
        ** Name: close
        **
        ** Description:
        **	Close socket used for network connection.
        **
        ** Input:
        **	None.
        **
        ** Output:
        **	None.
        **
        ** Returns:
        **	void
        **
        ** History:
        **	 3-May-05 (gordy)
        **	    Created.
        */
        private void close()
        {
            if (socket != null)
            {
                if (trace.enabled(2))
                    trace.write(title + ": closing network connection");

                try { socket.Close(); }
                catch (Exception /* ignore */) { }
                finally
                {
                    socket = null;
                    ipAddress = null;
                }
            }

            return;
        }
Exemplo n.º 3
0
        connect(String host, String portID)
        {
            int i;

            System.Net.IPAddress[] ipAddresses;

            System.Net.IPEndPoint ipEndPoint = null;
            socket        = null;
            ipAddresses   = null;
            ipAddress     = null;
            socketIsLocal = false;

            try
            {
                if (trace.enabled(2))
                {
                    trace.write(title + ": opening network connection '" +
                                host + "', '" + portID + "'");
                }

                if (host == null ||
                    host.Length == 0 ||                       // if local machine
                    host.ToLowerInvariant() == "(local)")
                {
                    ipAddresses   = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
                    socketIsLocal = true;
                }
                else
                {
                    ipAddresses = System.Net.Dns.GetHostAddresses(host);
                }

                int port = translatePortID(portID);

                for (i = 0; i < ipAddresses.Length; i++)                  // loop addresses
                {
                    ipAddress = ipAddresses[i];

                    if (trace.enabled(3))
                    {
                        string address = ipAddress.ToString() + "  ";
                        address += ipAddress.AddressFamily.ToString();
                        if (ipAddress.AddressFamily ==
                            System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            address += "(IPv4)";
                        }
                        else if (ipAddress.AddressFamily ==
                                 System.Net.Sockets.AddressFamily.InterNetworkV6)
                        {
                            address += "(IPv6),";
                            address += "ScopeId=" + ipAddress.ScopeId.ToString();
                        }
                        trace.write(title + ": host address " + address +
                                    "; port=" + port);
                    }

                    ipEndPoint = new System.Net.IPEndPoint(ipAddress, port);
                    socket     = new MsgSocket(ipAddress.AddressFamily);
                    try
                    {
                        socket.Connect(ipEndPoint);
                        if (trace.enabled(3))
                        {
                            trace.write(title + ": connected to host address and port");
                        }
                        return;                          // success!
                    }
                    catch (Exception ex)
                    {
                        if (trace.enabled(1))
                        {
                            trace.write(title + ": connection failed to address[" + i + "]=" + ipAddress + " - " + ex.Message);
                        }
                        if (i == (ipAddresses.Length - 1))
                        {
                            throw;                               // continue throw of last exception up chain
                        }
                        // else try next address on list.
                    }
                }                  // end for loop through ipHostInfo.AddressList

                if (i == ipAddresses.Length)
                {                  // should not have reached this point but we will bullet-proof
                    trace.write(title + ": connection error - all addresses failed");
                    throw SqlEx.get(ERR_GC4001_CONNECT_ERR);
                }
            }              // end try block
            catch (System.Net.Sockets.SocketException ex)
            {
                if (trace.enabled(1))
                {
                    trace.write(title +
                                ": error connecting to host - (SocketException) " +
                                ex.Message);
                }
                SqlEx pex = (SqlEx)
                            SqlEx.get(ERR_GC4001_CONNECT_ERR, ex);
                // "Unable to establish connection due to communications error."
                if (ipAddresses == null)                  // is it the GetHostAddresses( )
                {
                    pex = SqlEx.get(pex, SqlEx.get(
                                        "SocketException thrown " +
                                        "by System.Net.Dns.GetHostAddresses(" +
                                        (host == null?"<null>":("\"" + host + "\"")) + ") " +
                                        "in resolving the hostname",
                                        "08001", ex.ErrorCode));
                }
                else                  // must be TcpClient( ) call
                {
                    pex = SqlEx.get(pex, SqlEx.get(
                                        "SocketException thrown " +
                                        "by System.Net.Sockets.TcpClient( ) " +
                                        "while connecting to host " +
                                        (host == null ? "<null>" : ("\"" + host + "\".")),
                                        "08001", ex.ErrorCode));
                }

                socket    = null;
                ipAddress = null;

                throw pex;
            }
            catch (System.Exception ex)
            {
                if (trace.enabled(1))
                {
                    trace.write(title + ": error connecting to host - " + ex.Message);
                }

                socket    = null;
                ipAddress = null;

                throw SqlEx.get(ERR_GC4001_CONNECT_ERR, ex);
            }
        }          // connect
Exemplo n.º 4
0
        /*
        ** Name: connect
        **
        ** Description:
        **	Opens a socket connection to target host.
        **
        ** Input:
        **	host		Host name or address.
        **	portID		Symbolic or numeric port ID.
        **
        ** Output:
        **	None.
        **
        ** Returns:
        **	void.
        **
        ** History:
        **	 7-Jun-99 (gordy)
        **	    Created.
        **	17-nov-04 (thoda04)
        **	    Be forgiving and treat "(local)" as localhost.
        **	04-may-06 (thoda04)
        **	    Replace the obsolete .NET 1.1 GetHostByName() method with
        **	    new .NET 2.0 System.Net.Dns.GetHostEntry() method.
        **	 3-Nov-08 (gordy, ported by thoda04)
        **	    Extracted from constructor.
        **	04-feb-10 (thoda04) B123229
        **	    Data provider connects slowly if using IPv6 addresses.
        **	    Use TcpClient(AddressFamily) constructor to avoid
        **	    SocketException if an IPv6 address is passed.
        **	    Replace System.Net.Dns.GetHostEntry() method with
        **	    GetHostAddresses() to avoid with DNS reverse lookup.
        */
        protected internal virtual void connect( String host, String portID )
        {
            int    i;
            System.Net.IPAddress[] ipAddresses;

            System.Net.IPEndPoint  ipEndPoint = null;
            socket                            = null;
            ipAddresses                       = null;
            ipAddress                         = null;
            socketIsLocal                     = false;

            try
            {
                if (trace.enabled(2))
                    trace.write(title + ": opening network connection '" +
                        host + "', '" + portID + "'");

                if (host == null    ||
                    host.Length==0  ||    // if local machine
                    host.ToLowerInvariant() == "(local)")
                {
                    ipAddresses = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
                    socketIsLocal = true;
                }
                else
                    ipAddresses = System.Net.Dns.GetHostAddresses(host);

                int port = translatePortID(portID);

                for (i = 0; i < ipAddresses.Length; i++)  // loop addresses
                {
                    ipAddress = ipAddresses[i];

                    if (trace.enabled(3))
                    {
                        string address = ipAddress.ToString() + "  ";
                        address += ipAddress.AddressFamily.ToString();
                        if (ipAddress.AddressFamily ==
                            System.Net.Sockets.AddressFamily.InterNetwork)
                            address += "(IPv4)";
                        else if (ipAddress.AddressFamily ==
                            System.Net.Sockets.AddressFamily.InterNetworkV6)
                        {
                            address += "(IPv6),";
                            address += "ScopeId=" + ipAddress.ScopeId.ToString();
                        }
                        trace.write(title + ": host address " + address +
                            "; port=" + port);
                    }

                    ipEndPoint = new System.Net.IPEndPoint(ipAddress, port);
                    socket = new MsgSocket(ipAddress.AddressFamily);
                    try
                    {
                        socket.Connect(ipEndPoint);
                        if (trace.enabled(3))
                            trace.write(title + ": connected to host address and port");
                        return;  // success!
                    }
                    catch (Exception ex)
                    {
                        if (trace.enabled(1))
                            trace.write(title + ": connection failed to address[" + i + "]=" + ipAddress + " - " + ex.Message);
                        if (i == (ipAddresses.Length - 1))
                            throw;   // continue throw of last exception up chain
                                     // else try next address on list.
                    }
                }  // end for loop through ipHostInfo.AddressList

                if (i == ipAddresses.Length)
                {  // should not have reached this point but we will bullet-proof
                    trace.write(title + ": connection error - all addresses failed");
                    throw SqlEx.get(ERR_GC4001_CONNECT_ERR);
                }
            }  // end try block
            catch (System.Net.Sockets.SocketException ex)
            {
                if (trace.enabled(1))
                    trace.write(title +
                        ": error connecting to host - (SocketException) " +
                        ex.Message);
                SqlEx pex = (SqlEx)
                    SqlEx.get( ERR_GC4001_CONNECT_ERR, ex );
                    // "Unable to establish connection due to communications error."
                if (ipAddresses == null)  // is it the GetHostAddresses( )
                    pex = SqlEx.get(pex, SqlEx.get(
                        "SocketException thrown " +
                        "by System.Net.Dns.GetHostAddresses(" +
                        (host==null?"<null>":("\""+host+"\"")) + ") " +
                        "in resolving the hostname",
                            "08001",ex.ErrorCode));
                else  // must be TcpClient( ) call
                    pex = SqlEx.get(pex, SqlEx.get(
                        "SocketException thrown " +
                        "by System.Net.Sockets.TcpClient( ) " +
                        "while connecting to host " +
                        (host == null ? "<null>" : ("\"" + host + "\".")),
                            "08001",ex.ErrorCode));

                socket                            = null;
                ipAddress                         = null;

                throw pex;
            }
            catch (System.Exception ex)
            {
                if (trace.enabled(1))
                    trace.write(title + ": error connecting to host - " + ex.Message);

                socket                            = null;
                ipAddress                         = null;

                throw SqlEx.get( ERR_GC4001_CONNECT_ERR, ex );
            }
        }