Kotlin Socket客户端连接localhost:9999被拒,求解决方案
问题排查与解决方案
问题概述
我正在学习一段Kotlin Socket代码,目标连接本地地址http://127.0.0.1,已在本地启用并运行IIS,但代码执行时抛出java.io.IOException: The remote computer refused the network connection错误;同时访问错误格式的地址localhost/127.0.0.1:9999时返回404,代码中实际使用InetSocketAddress("localhost", PORT)(PORT值为9999)建立连接。
代码执行错误日志
"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
网页访问错误
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
相关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: ${Thread.currentThread().name}") println("Message to server:") while (br.readLine().also { line = it } != null) { val result = async { println("while:async name is: ${Thread.currentThread().name}") sendMessage(tcpSocket, line) } println("while: name is: ${Thread.currentThread().name}") if (line == "bye") { println("End") break } val response: String = result.await() withContext(Dispatchers.Default) { println("withContext[Default] name is: ${Thread.currentThread().name}") println("response from server: $response") println("Message to server:") } } }
解决方案
一、IIS配置步骤
修正端口绑定
- 打开IIS管理器,找到目标网站,右键选择「编辑绑定」
- 添加或修改绑定:类型选「HTTP」,IP地址选择「127.0.0.1」或「所有未分配」,端口设置为9999,主机名留空
- 保存设置后,执行命令
iisreset重启IIS服务
检查防火墙规则
- 打开Windows Defender防火墙,进入「高级设置」
- 添加入站规则,允许TCP协议在9999端口接收连接
修正访问地址格式
- 访问时使用正确格式:
http://localhost:9999或http://127.0.0.1:9999,而非错误的localhost/127.0.0.1:9999
- 访问时使用正确格式:
二、推荐调试用测试服务器
- Ktor(Kotlin专属):用Ktor框架可以快速搭建Echo服务,几行代码就能监听9999端口并响应Socket请求,完全适配Kotlin开发环境
- Python内置服务器:命令行执行
python -m http.server 9999,快速启动静态文件服务器,适合基础连接测试 - Node.js http-server:安装后执行
http-server -p 9999,轻量级HTTP服务器,启动和关闭都很便捷
内容的提问来源于stack exchange,提问作者Ivan
相关产品推荐
相关产品推荐

