# Unable to connect to Elasticsearch client running locally: receiving 'connection refused' on KOTLIN

**URL:** <https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-client-running-locally-receiving-connection-refused-on-kotlin/327215>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 7, 2023, 5:17pm UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-client-running-locally-receiving-connection-refused-on-kotlin/327215 "2023-03-07T17:17:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GAETANO\_SIMONELLI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gaetano_simonelli/32/118044_2.png) [@GAETANO\_SIMONELLI](https://discuss.elastic.co/u/GAETANO_SIMONELLI)\
**Post date:** [March 7, 2023, 5:17pm UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-client-running-locally-receiving-connection-refused-on-kotlin/327215/1 "2023-03-07T17:17:55Z")

</div>

I am experiencing connection issues with my Elasticsearch client running locally. Specifically, when I try to connect using localhost in the RestClient.builder, I receive a java.net.connectException: connection refused error, and when I try to use my IP address, I get a timeout error. I have developed two different versions of my test app, one using a low-level REST client and the other using the Java API, both of which were developed using Kotlin for and android application. Despite this, I still receive the same errors. I have included the code for both versions below. Can anyone suggest a solution or help me diagnose the issue?

Here are a code snipet to better understand my situation. Code used in the low level rest client:

```auto
private fun sendFileStandard() {
        val restClient = RestClient.builder(
            HttpHost("192.167.208.131", 9200, "http"),
            HttpHost("localhost", 9200, "http")
        ).build()

        val request = Request(
            "GET",
            "/"
        )

        var risultato = "defualt value"

        doAsync {
            val cancellable: Cancellable = restClient.performRequestAsync(request,
                object : ResponseListener {
                    override fun onSuccess(response: Response) {
                        dii("onsuccess", response)
                        risultato = "success" +response.toString()
                    }

                    override fun onFailure(exception: Exception) {
                        dii("onfailure", exception)
                        risultato = "exception" + exception.toString()
                    }
                })
        }
        dii("risultato", risultato)

        Thread.sleep(9000)
        restClient.close();

    }

```

the java api ones:

```auto
val restClient = RestClient.builder(
            HttpHost("localhost", 9200)
        ).build()

        val transport: ElasticsearchTransport = RestClientTransport(
            restClient, JacksonJsonpMapper()
        )
        val client = ElasticsearchClient(transport)

        val input: Reader = StringReader(
            "{'@timestamp': '2022-04-08T13:55:32Z', 'level': 'warn', 'message': 'Some log message'}"
                .replace('\'', '"')
        )

        val request = IndexRequest.of { i: IndexRequest.Builder<JsonData?> ->
            i
                .index("logs")
                .withJson(input)
        }
        doAsync {
            val response: IndexResponse = client.index(request)
        }

```

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [March 8, 2023, 3:12pm UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-client-running-locally-receiving-connection-refused-on-kotlin/327215/2 "2023-03-08T15:12:33Z")

</div>

You should probably use `"https"` instead of `"http"` - see [Connecting | Elasticsearch Java API Client [8.6] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html#using-a-secure-connection)

Note also that the Java API client uses the RestClient to manage connections, which is why you have started your Java API example by creating a RestClient 😉

---

<div class="post-metadata">

**Author:** ![GAETANO\_SIMONELLI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gaetano_simonelli/32/118044_2.png) [@GAETANO\_SIMONELLI](https://discuss.elastic.co/u/GAETANO_SIMONELLI)\
**Post date:** [March 8, 2023, 4:05pm UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-client-running-locally-receiving-connection-refused-on-kotlin/327215/3 "2023-03-08T16:05:49Z")

</div>

if what are you trying to say is that i should replace "http" here with "https" I tried it yet and obtained the same result

```auto
        val restClient = RestClient.builder(
            HttpHost("192.167.208.131", 9200, "http"),
            HttpHost("localhost", 9200, "http")
        ).build()

```

Otherwise if you are trying to say is that i should configure ssl, autentication and https I don't need those level of securety and I disabled those feature. That's part of the elasticsearch.yml file

```auto

# Enable security features
xpack.security.enabled: false

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: false
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["DESKTOP-29F0DN7"]

# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication
http.host: 0.0.0.0

# Allow other nodes to join the cluster from anywhere
# Connections are encrypted and mutually authenticated
#transport.host: 0.0.0.0

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 5, 2023, 4:06pm UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-client-running-locally-receiving-connection-refused-on-kotlin/327215/4 "2023-04-05T16:06:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
