简体   繁体   English

Golang TCP 服务器给出“dial tcp 127.0.0.1:9999: connect: connection denied”错误

[英]Golang TCP server gives “dial tcp 127.0.0.1:9999: connect: connection refused” error

I am learning from the book An Introduction to Programming in Go by Caleb Doxsey我正在从Caleb Doxsey 的《Go 编程简介》一书中学习

In chapter 13 about servers we are given the code:在关于服务器的第 13 章中,我们给出了代码:

package main

import (
    "encoding/gob"
    "fmt"
    "net"
)

func server() {
    // listen on a port

    ln, err := net.Listen("tcp", ":9999")

    if err != nil {
        fmt.Println("server, Listen", err)
        return
    }

    for {
        // accept a connection
        c, err := ln.Accept()
        if err != nil {
            fmt.Println("server, Accept", err)
            continue
        }
        // handle the connection
        go handleServerConnection(c)
    }
}

func handleServerConnection(c net.Conn) {
    // receive the message
    var msg string
    err := gob.NewDecoder(c).Decode(&msg)
    if err != nil {
        fmt.Println("handleServerConnection", err)
    } else {
        fmt.Println("Received", msg)
    }

    c.Close()
}

func client() {
    // connect to the server
    c, err := net.Dial("tcp", "127.0.0.1:9999")
    if err != nil {
        fmt.Println("client, Dial", err)
        return
    }

    // send the message
    msg := "Hello World"
    fmt.Println("Sending", msg)
    err = gob.NewEncoder(c).Encode(msg)
    if err != nil {
        fmt.Println("client, NewEncoder", err)
    }

    c.Close()
}

func main() {
    go server()
    go client()
    
    var input string
    fmt.Scanln(&input)
}

Running this code i almost always receive:运行此代码我几乎总是收到:

client, Dial dial tcp 127.0.0.1:9999: connect: connection refused客户端,拨号 tcp 127.0.0.1:9999: 连接:连接被拒绝

But sometimes I receive:但有时我会收到:

Sending Hello World发送 Hello World

Received Hello World收到你好世界

I have also discovered if i run just run server separately from client, and then run client on a separate file, it works as intended.我还发现,如果我只运行与客户端分开运行的服务器,然后在单独的文件上运行客户端,它会按预期工作。 Why is that?这是为什么?

Listen and Dial are called concurrently, and you can't predict which one executes first. Listen 和 Dial 是并发调用的,您无法预测哪个先执行。 If Dial executes before Listen then there is obviously nothing listening yet and that produces the error.如果 Dial 在 Listen 之前执行,那么显然还没有任何东西在监听,这会产生错误。

Call Listen in main, before starting the goroutines:在启动 goroutine 之前,在 main 中调用 Listen:

func main() {
    ln, err := net.Listen("tcp", ":9999")
    if err != nil {
        fmt.Fatal("server, Listen", err)
    }

    go server(ln)
    go client()
    
    var input string
    fmt.Scanln(&input)
}

func server(ln net.Listener) {
    for {
        // accept a connection
        c, err := ln.Accept()
        if err != nil {
            fmt.Println("server, Accept", err)
            continue
        }
        // handle the connection
        go handleServerConnection(c)
    }
}

暂无
暂无

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

相关问题 拨号 tcp 127.0.0.1:8000: 连接:连接被拒绝 golang docker 容器 - dial tcp 127.0.0.1:8000: connect: connection refused golang docker containers docker + golang lib/pq“拨号 tcp 127.0.0.1:5432:连接:连接被拒绝” - docker + golang lib/pq "dial tcp 127.0.0.1:5432: connect: connection refused" 无法使用 go 和 docker 连接到 mysql 服务器 - 拨打 tcp 127.0.0.1:3306:连接:连接被拒绝 - Unable to connect to mysql server with go and docker - dial tcp 127.0.0.1:3306: connect: connection refused 初始化数据库失败,出现错误拨号 tcp 127.0.0.1:3306:连接:连接被拒绝 问题 - failed to initialize database, got error dial tcp 127.0.0.1:3306: connect: connection refused Problem “传输:拨打拨号 tcp 127.0.0.1:9001 时出错:连接:连接被拒绝” - "transport: Error while dialing dial tcp 127.0.0.1:9001: connect: connection refused" Golang + postgres + docker: 拨打 tcp 172.18.0.2:5432: connect: connection refused - Golang + postgres + docker: Dial tcp 172.18.0.2:5432: connect: connection refused Golang:`dial tcp 172.20.0.7:8081: connect: connection refused`,在 docker 容器/服务之间随机出错 - Golang: `dial tcp 172.20.0.7:8081: connect: connection refused`, error at random times between docker containers / services redis HMSEET 发生错误,拨打 tcp:6379:连接:连接被拒绝 - Error occured with redis HMSEET, dial tcp :6379: connect: connection refused 拨打 tcp 127.0.0.1:8080:连接:连接被拒绝。 go docker 应用程序 - dial tcp 127.0.0.1:8080: connect: connection refused. go docker app Docker 获取 http://localhost:8091/api/order: 拨打 tcp 127.0.0.1:8091: 连接连接被拒绝 - Docker Get http://localhost:8091/api/order: dial tcp 127.0.0.1:8091: connect: connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM