Ktor项目本地主机(localhost):8080拒绝连接。

huangapple go评论60阅读模式
英文:

Ktor project localhost:8080 refuses to connect

问题

我正在进行一个 Ktor 项目的工作,一切都正常。我启动了服务器,它在 8080 端口上正常运行,但是现在出现了某种原因突然停止工作。我终止了任务并尝试了一切,我不确定出了什么问题。我尝试重新安装了 IntelliJ Idea,但仍然面临着相同的问题。我尝试使用 127.0.0.1、0.0.0.0、localhost,但它们都不起作用,我不知道该怎么办。我已经在这上面浪费了大约 2 小时。我尝试了更改端口,阻止防火墙和防病毒软件。

英文:

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.

答案1

得分: 1

将 127.0.0.1(本地主机)更改为您的私有 IP,例如 172.30.1.59,如果您使用的是无线网络。如果您使用固定 IP,请使用该 IP。我遇到了相同的问题,但通过这种方法解决了。很好!
命令提示符 -> ipconfig -> 使用 IP 地址

英文:

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

答案2

得分: 0

我通常在使用Ktor时遇到相同的问题,这真让人沮丧。然后,我将发布以下可能的解决方法,您应该按顺序尝试并阅读这些步骤。我会假设您在同一台计算机上运行WebSocket服务器和用于Android应用的WebSocket客户端。

  1. 您正在使用相同的IP地址运行服务器端和客户端,这是行不通的,因为客户端无法连接到服务器。当我测试一个需要服务器端和客户端的项目时,我会使用我的计算机运行Android应用,使用笔记本电脑运行服务器端。如果不是您的情况,请忽略这一点。

  2. 在客户端,当您创建HttpClient实例时,您是否将任何值作为engine传递给客户端,还是直接使用lambda?根据我的经验,当我创建HttpClient实例时,只有以下两种方式有效:

    val client = HttpClient {
       install(WebSockets)
    }
    

    或者

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

    对我无效的引擎是:

    val client = HttpClient(OkHttp) {
       install(WebSockets)
    }
    
  3. 最后,当您使用上述客户端实例创建WebSocket时,应该使用:

    client.ws(
            HttpMethod.Get,
            "localhost",
            8080,
            "/"
    )
    {
    //客户端代码
    }
    

    而不是client.wss。这是因为在本地连接中,您的客户端不使用TLS安全连接,它会抛出异常。如果您将服务器端部署在支持TLS安全的托管中,比如Heroku,那么您可以使用wss,因为客户端将使用TLS证书连接。

希望我的回答能对您有所帮助。祝您好运!

英文:

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!

答案3

得分: 0

我遇到了同样的问题,结果发现是因为我安装了HttpsRedirect和HSTS插件。由于这个原因,它拒绝处理HTTP请求,并且因为我既没有针对本地主机或本地IP的证书,所以它无法工作。

在本地运行时禁用这两个插件可以让事情对我起作用。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月28日 12:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63627470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定