简体   繁体   English

连接到本地主机的端口 9999 时连接被拒绝

[英]Connection refused when connecting to localhost's port 9999

I am trying to understand some example Kotlin code that connects to http://127.0.0.1 using sockets, and I have IIS enabled and running it on 127.0.0.1.我正在尝试理解一些示例 Kotlin 使用 sockets 连接到http://127.0.0.1的代码,并且我启用了 IIS 并在 127.0.0.1 上运行它。 However, when I run the code, I get:但是,当我运行代码时,我得到:

"C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:52769,suspend=y,server=n -javaagent:C:/Users/ivlat/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.5.0/d8cebccdcddd029022aa8646a5a953ff88b13ac8/kotlinx-coroutines-core-jvm-1.5.0.jar -javaagent:C:\Users\ivlat\AppData\Local\JetBrains\IdeaIC2022.2\captureAgent\debugger-agent.jar=file:/C:/Users/ivlat/AppData/Local/Temp/capture.props -Dfile.encoding=UTF-8 -classpath "C:\Dev\KotlinProjects\7_08_Echo\client\build\classes\kotlin\main;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk8\1.5.21\6b3de2a43405a65502728047db37a98a0c7e72f0\kotlin-stdlib-jdk8-1.5.21.jar;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.2.3\7c4f3c474fb2c041d8028740440937705ebb473a\logback-classic-1.2.3.jar;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.2.3\864344400c3d4d92dfeb0a305dc87d953677c03c\logback-core-1.2.3.jar;C:\Dev\KotlinProjects\7_08_Echo\shared\build\classes\kotlin\main;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlinx\kotlinx-coroutines-core-jvm\1.5.0\d8cebccdcddd029022aa8646a5a953ff88b13ac8\kotlinx-coroutines-core-jvm-1.5.0.jar;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7\1.5.21\f059658740a4b3a3461aba9681457615332bae1c\kotlin-stdlib-jdk7-1.5.21.jar;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.5.21\2f537cad7e9eeb9da73738c8812e1e4cf9b62e4e\kotlin-stdlib-1.5.21.jar;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.25\da76ca59f6a57ee3102f8f9bd9cee742973efa8a\slf4j-api-1.7.25.jar;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-common\1.5.21\cc8bf3586fd2ebcf234058b9440bb406e62dfacb\kotlin-stdlib-common-1.5.21.jar;C:\Users\ivlat\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2.3\lib\idea_rt.jar" com.knowledgespike.client.ApplicationKt
Connected to the target VM, address: '127.0.0.1:52769', transport: 'socket'
Exception in thread "main" java.io.IOException: The remote computer refused the network connection.

    at java.base/sun.nio.ch.Iocp.translateErrorToIOException(Iocp.java:299)
    at java.base/sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:389)
    at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Disconnected from the target VM, address: '127.0.0.1:52769', transport: 'socket'

Process finished with exit code 1

Apparently, my IIS isn't really set up for the port 9999, as I am getting the error below when I type localhost/127.0.0.1:9999显然,我的 IIS 并没有真正设置端口 9999,因为当我键入localhost/127.0.0.1:9999时出现以下错误

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

The connection string that the Kotlin code is also using is localhost/127.0.0.1:9999 Kotlin 代码也使用的连接字符串是localhost/127.0.0.1:9999

How would I go about getting the IIS recognize this request/let it through?我 go 如何让 IIS 识别这个请求/让它通过? It really doesn't even have to be IIS. I just want to understand this sample code better, and any sort of sandbox/test web server that allows for this request would more than suffice at this point.它甚至不必是 IIS。我只是想更好地理解此示例代码,此时允许此请求的任何类型的沙箱/测试 web 服务器都绰绰有余。

This is the Kotlin code that's throwing the exception when trying to connect:这是在尝试连接时抛出异常的 Kotlin 代码:

fun main() = runBlocking {

    val client: AsynchronousSocketChannel = AsynchronousSocketChannel.open()
    val hostAddress = InetSocketAddress("localhost", PORT)
    val tcpSocket = TcpSocket(client)
    tcpSocket.connect(hostAddress)

    val br = BufferedReader(InputStreamReader(System.`in`))
    var line: String
    println("Main name is: \t\t\t\t\t${Thread.currentThread().name}")
    println("Message to server:")
    while (br.readLine().also { line = it } != null) {
        val result = async {
            println("while:async name is: \t\t\t${Thread.currentThread().name}")
            sendMessage(tcpSocket, line)
        }
        println("while: name is: \t\t\t\t${Thread.currentThread().name}")
        if (line == "bye") {
            println("End")
            break
        }
        val response: String = result.await()
        withContext(Dispatchers.Default) {
            println("withContext[Default] name is: \t${Thread.currentThread().name}")
            println("response from server: $response")
            println("Message to server:")
        }
    }
}
  1. A webserver listens on a particular port, the default is port 80 for HTTP. Unless you go out of your way to configure it, I expect that's where IIS will be listening网络服务器侦听特定端口,HTTP 的默认端口为 80。除非您 go 不按您的方式配置它,否则我希望 IIS 将在此处侦听
  2. I would start by taking Kotlin out of the question and establish you have a webserver listening on a particular port.我首先将 Kotlin 排除在外,并确定您有一个侦听特定端口的网络服务器。 Just use a regular web browser to check.使用普通的 web 浏览器查看即可。 Beware the browsers handle comms fails in nice friendly ways, hiding the real issues.当心浏览器以非常友好的方式处理通信失败,隐藏真正的问题。 Better to use a more technical tool like最好使用更技术性的工具,例如
    1. curl curl
    2. Postman Postman
  3. If IIS is suspect, then what IDE are you using?如果怀疑 IIS,那么您使用的是什么 IDE? If you are using IntelliJ如果您使用的是 IntelliJ
    1. find yourself an HTML file,找到一个 HTML 文件,
    2. open it up, then notice in the Top Right several however icons will show up打开它,然后在右上角注意几个但是图标会出现
    3. pick the IntelliJ one, this will start an internal web server and then a web browser will launch to connect to it选择 IntelliJ,这将启动一个内部 web 服务器,然后将启动一个 web 浏览器以连接到它
    4. try this web server for your tests试试这个 web 服务器进行测试

I was able to circumvent the error by going to bindings for the default web site in the IIS Manager UI and then adding the port 9999. The Kotlin code connects fine now.通过转到 IIS 管理器 UI 中的默认 web 站点的绑定,然后添加端口 9999,我能够规避该错误。Kotlin 代码现在连接正常。 I'm pretty sure there are better ways of solving this IRL, but I think it's good enough, as I am just learning Kotlin at this point.我很确定有更好的方法来解决这个 IRL,但我认为这已经足够了,因为我现在正在学习 Kotlin。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Okhttp 无法连接到 localhost 端点抛出连接失败:ECONNREFUSED(连接被拒绝) - Okhttp Unable to connect to a localhost endpoint throws Connected failed: ECONNREFUSED (Connection refused) RabbitMQ 连接被拒绝 - RabbitMQ Connection refused MockWebServer 连接被拒绝:没有更多信息 - MockWebServer Connection refused: no further information 连接到Axon Server时更改节点的实例名称 - Change a node's Instance Name when connecting to Axon Server Android应用程序中的Kotlin编译器:Connection拒绝主机:127.0.0.1 - Kotlin Compiler in Android Application: Connection refused to host: 127.0.0.1 连接到 Azurite 时身份验证失败 - Authentication Failure when connecting to Azurite 获取 isConnected 失败:ECONNREFUSED(连接被拒绝)错误 Kotlin Retrofit 发布请求 - Getting isConnected failed: ECONNREFUSED (Connection refused) error for Kotlin Retrofit post request 重新启动Spark服务器后,为什么出现“拒绝连接”错误? - Why am I getting “connection refused” error after restart Spark server? 将 RecycleRview 适配器连接到片段时缺少参数 - Parameter missing when connecting RecycleRview adapter to fragment 连接到 Python websockets 时出现 Ktor MalformedInputException - Ktor MalformedInputException when connecting to Python websockets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM