Unable to connect to opc-ua server TIMEOUT ERROR

57 Views Asked by At

I want to connect to OPC Server with security policy that is Basic256Sha256.
And I use eclipse/milo code.

this is my createClient code

public OpcUaClient createClient ( final String connectionUrl,
                              final String ip,
                              final int connectionTimeout ) throws Exception
{
    final File securityTempDir = new File ( BaseConsts.CACHE_INFO_FULL_PATH + "driver" + BaseConsts.FILE_SEPER + Props.AGENT_ID,
                    "security" );
    if ( !securityTempDir.exists () && !securityTempDir.mkdirs () )
    {
        throw new Exception ( "unable to create security dir: " + securityTempDir );
    }

    SecurityPolicy tempSecurityPolicy = null;

    if ( Props.SECURITY_POLICY.equals ( Consts.SECURITY_POLICY_0 ) )
    {
        tempSecurityPolicy = SecurityPolicy.None;
    } else if ( Props.SECURITY_POLICY.equals ( Consts.SECURITY_POLICY_1 ) )
    {
        tempSecurityPolicy = SecurityPolicy.Basic256;
    } else if ( Props.SECURITY_POLICY.equals ( Consts.SECURITY_POLICY_2 ) )
    {
        tempSecurityPolicy = SecurityPolicy.Basic256Sha256;
    } else if ( Props.SECURITY_POLICY.equals ( Consts.SECURITY_POLICY_3 ) )
    {
        tempSecurityPolicy = SecurityPolicy.Basic256Sha256;
    }

    final SecurityPolicy securityPolicy = tempSecurityPolicy;

    // final EndpointDescription [] endpoints = UaTcpStackClient.getEndpoints ( connectionUrl ).get ();

    List<EndpointDescription> endpoints;

    try
    {
        endpoints = DiscoveryClient.getEndpoints ( connectionUrl ).get ();

    } catch ( final Throwable ex )
    {
        // try the explicit discovery endpoint as well
        String discoveryUrl = connectionUrl;

        if ( !discoveryUrl.endsWith ( "/" ) )
        {
            discoveryUrl += "/";
        }
        discoveryUrl += "discovery";

        LOGGER.info ( "Trying explicit discovery URL: {}", discoveryUrl );

        endpoints = DiscoveryClient.getEndpoints ( discoveryUrl ).get ();
    }

    EndpointDescription endpoint = endpoints.stream ()
                    .filter ( e -> e.getSecurityPolicyUri ().equals ( securityPolicy.getUri () ) )
                    .filter ( e -> true )
                    .findFirst ()
                    .orElseThrow ( () -> new Exception ( "no desired endpoints returned" ) );

    // EndpointDescription endpoint = Arrays.stream ( endpoints )
    // .filter ( e -> e.getSecurityPolicyUri ().equals ( securityPolicy.getSecurityPolicyUri () ) )
    // .findFirst ().orElseThrow ( () -> new Exception ( "no desired endpoints returned" ) );

    final URI uri = new URI ( endpoint.getEndpointUrl () ).parseServerAuthority ();

    final String endpointUrl = String.format (
                    "%s://%s:%s%s",
                    uri.getScheme (),
                    ip,
                    uri.getPort (),
                    uri.getPath () );

    endpoint = new EndpointDescription (
                    endpointUrl,
                    endpoint.getServer (),
                    endpoint.getServerCertificate (),
                    endpoint.getSecurityMode (),
                    endpoint.getSecurityPolicyUri (),
                    endpoint.getUserIdentityTokens (),
                    endpoint.getTransportProfileUri (),
                    endpoint.getSecurityLevel () );
    

    LOGGER.info ( LogMsgUtil.infoFormat ( "Using endpoint: {} [{}]" ), endpoint.getEndpointUrl (), endpoint.getSecurityMode () );

    final UInteger timeout = UInteger.valueOf ( connectionTimeout );

    this.loader.load ( securityTempDir );

    final MessageLimits messageLimits = new MessageLimits ( DEFAULT_MAX_CHUNK_SIZE, DEFAULT_MAX_CHUNK_COUNT,
                    DEFAULT_MAX_MESSAGE_SIZE );

    // final EncodingLimits encodingLimits = new EncodingLimits ( DEFAULT_MAX_ARRAY_LENGTH,
    // DEFAULT_MAX_STRING_LENGTH,
    // DEFAULT_MAX_RECURSION_DEPTH );

    OpcUaClientConfig config = null;


        config = OpcUaClientConfig.builder ()
                        .setApplicationName ( LocalizedText.english ( BaseProps.SYSTEM_SERVICE_NAME + " OPC-UA Client" ) )
                        .setApplicationUri ( "urn:opcua:" + BaseProps.SYSTEM_SERVICE_NAME + " OPC-UA Client" )
                        .setCertificate ( this.loader.getClientCertificate () )
                        .setKeyPair ( this.loader.getClientKeyPair () )
                        .setEndpoint ( endpoint )
                        .setIdentityProvider ( new AnonymousProvider () )
                        .setRequestTimeout ( timeout )
                        .setMessageLimits ( messageLimits )
                        .build ();



    this.opcUAClient = OpcUaClient.create ( config );
    
    return this.opcUAClient;
}

And this is my code that connect to server.

this.opcUaClient = this.opcClient.createClient ( connectionUrl, this.devicePriIp, connectionTimeout );
                    this.opcUaClient.connect ().get ();

In this.opcUaClient.connect().get() code, Timeout Error is occurred.

this is the log enter image description here

I tried everyting.. help me.

1

There are 1 best solutions below

0
Kevin Herron On

You may not be doing anything "wrong" in your code.

You cut the error message short and provided a screenshot of text (tsk tsk), but from what I can see, it looks like you are successfully opening a TCP connection and sending a request, but not getting a response.

A Wireshark capture may help clear things up.