Zotako
Zotako

Reputation: 41

Ktor project localhost:8080 refuses to connect

I was working on a ktor project and everything was working fine. I started the server and it was working fine on port 8080 but now for some reason suddenly it stopped working. I killed the task and tried everything, I'm not sure what's wrong. I tried to reinstall IntelliJ Idea and I'm still facing the same issue. I tried using 127.0.0.1 , 0.0.0.0 , localhost but none of them work idk what to do. I've wasted like 2 hours on this thing. I've tried changing port, blocking firewall and antivirus.

Upvotes: 4

Views: 5180

Answers (5)

jagadheshvar
jagadheshvar

Reputation: 67

What worked for me is this:

ApiClient.httpClient.get {
            url("http:////192.168.0.104:8090/api/v1/auth/login")
        }

I am hosting it in my pc and my service's ip:port is 192.168.0.104:8090

Upvotes: 0

Shanki Bansal
Shanki Bansal

Reputation: 1740

for Android Developers: if running the app on the android-studio-emulator, then replace http://localhost:8080/ with http://10.0.2.2:8080/

Upvotes: 2

nibarius
nibarius

Reputation: 4117

I had the same problem and it turned out that it was because I had the HttpsRedirect and HSTS plugins installed. Due to this it refused to handle the http request and since I don't have any certificate for localhost or my local IP it didn't work.

Disabling these two plugins when running locally makes things work for me.

Upvotes: 1

Lim혁
Lim혁

Reputation: 41

change 127.0.0.1(localhost) to your private ip like 172.30.1.59 if you use wifi. if you use fixed ip then use it. i have same problem, but solve it with this. Nice! cmd -> ipconfig -> use ip address

Upvotes: 3

Sergi
Sergi

Reputation: 21

I usually had the same issue using Ktor and it is frustrating. Then I am gonna post the following possible fixes that you should try in order and reading the steps. I am going to consider that you are running a WebSocket server and a WebSocket client for your Android app in a unique computer.

  1. You are running the server side and the client using the same ip and it should not work, because the client can not connect to the server. When I am testing a project that requires server-side and client-side I use my computer to run the Android app and the laptop to run the server side. If this is not your case, then, do not pay attention.

  2. In the client-side, when you create the instance of the HttpClient, do you pass any value to the client as engine or you simply go directly with lambda? From my experience, when I create the HttpClient instance, only works these two following first ones:

    val client = HttpClient {
       install(WebSockets)
    }
    

    or

    val client = HttpClient(CIO){
       install(WebSockets)
    }
    

    The engine that doesn't work for me is:

    val client = HttpClient(OkHttp) {
       install(WebSockets)
    }
    
  3. Finally, when you create the WebSocket using the past client instance, you should use

    client.ws(
            HttpMethod.Get,
            "localhost",
            8080,
            "/"
    )
    {
    //Client code
    }
    

    and not client.wss. That is because in local connection, your client do not connect using TLS security and it will throw an exception. If you're deploying your server-side in a hosting that has TLS security as Heroku, then you can use the wss one, because the client-side will connect using TLS certificate.

Hope that my response can help. Good luck!

Upvotes: 0

Related Questions