go - 如何在Golang中使用gob连续监听客户端

标签 go tcp connection gob

在我的用例中,我想连续监听 TCP 连接并接收值。期望值是一个对象。所以我使用 gob 解码器来接收来自连接的值。我想连续监听连接并使用 go 例程接收对象。我这里有代码片段[它是应用程序的一部分。代码片段无法编译]。它第一次获取值,但没有接收后续对象。

func main() {

    //...
    // SOME CODE
    //...


    // All hosts who are connected; a map wherein
    // the keys are ip addreses and the values are
    //net.Conn objects
    allClients := make(map[string]net.Conn)

    tMaps := make(chan map[string]int64)

    for {
            select {
            // Accept new clients
            //
            case conn := <-newConnections:
            log.Printf("Accepted new client, #%s", hostIp)

            // Constantly read incoming messages from this
            // client in a goroutine and push those onto
            // the tMaps channel for broadcast to others.
            //
            go func(conn net.Conn) {
                    dec := gob.NewDecoder(conn)
                    for {
                            var tMap map[string]int64
                            err := dec.Decode(&tMap)
                            if err != nil {
                                    fmt.Println("Error in decoding ", err)
                                    break
                            }
                            log.Printf("Received values: %+v", tMap)
                            //update throttle map based on the received value
                            tMaps <- throttleMap
                    }

            }(conn)
    }
}

有人可以帮我解决这个问题吗?

最佳答案

让我们了解一下 Go 中 TCP 服务器的基础知识。

首先是“听”部分。我们可以这样设置:

package main

import (
    "fmt"
    "io"
    "net"
    "time"
)

func main() {
    ln, err := net.Listen("tcp", ":9000")
    if err != nil {
        panic(err)
    }
    defer ln.Close()

    for {
        conn, err := ln.Accept()
        if err != nil {
            panic(err)
        }

        io.WriteString(conn, fmt.Sprint("Hello World\n", time.Now(), "\n"))

        conn.Close()
    }
}

注意无限 for 循环。它总是运行并循环该代码。正在循环的代码有什么作用?如果连接进入正在监听的端口,则该连接将被接受。然后我们利用这种联系做一些事情。在本例中,我们使用 io.WriteString 写回它。对于这一连接,我们正在发送响应。然后我们关闭连接。如果有更多连接,我们已准备好接受它们。

现在让我们创建一个客户端来连接到 TCP 服务器。这称为“拨入”TCP 服务器。

要在您的计算机上运行所有这些代码,请运行上面的 TCP 服务器代码。要运行代码,请转到终端并输入:go run main.go

现在将下面的代码直接放入另一个文件中。在终端中启动另一个选项卡。也可以通过输入以下内容来运行该代码: go run main.go

下面的代码“拨入”您的 TCP 服务器将连接到该服务器,TCP 服务器将响应,然后关闭连接。

以下是作为客户端拨入 TCP 服务器的代码:

package main

import (
    "fmt"
    "io/ioutil"
    "net"
)

func main() {
    conn, err := net.Dial("tcp", "localhost:9000")
    if err != nil {
        panic(err)
    }
    defer conn.Close()

    bs, _ := ioutil.ReadAll(conn)
    fmt.Println(string(bs))

}

我们可以掌握这些基础知识并开始享受乐趣。

让我们创建一个“echo”服务器。

这将说明接受许多连接。

package main

import (
    "io"
    "net"
)

func main() {
    ln, err := net.Listen("tcp", ":9000")
    if err != nil {
        panic(err)
    }
    defer ln.Close()

    for {
        conn, err := ln.Accept()
        if err != nil {
            panic(err)
        }

        // handles unlimited connections
        go func() {
            io.Copy(conn, conn)
            conn.Close()
        }()
    }
}

按照之前的方式运行上面的文件:go run main.go

如果出现错误,请确保已关闭我们在上一个示例中运行的 TCP 服务器。您可以在终端中使用 ctrl+c 关闭 TCP 服务器。

现在您的新 TCP 服务器正在运行,让我们使用 Telnet 连接到它。

在 Windows 上您可以安装 telnet;在 Mac 上,它应该已经存在。使用此命令运行 telnet 并连接到您的 TCP 服务器:telnet localhost 9000

现在再举一个例子 - 像 Redis 这样的内存数据库:

package main

import (
    "bufio"
    "fmt"
    "io"
    "log"
    "net"
    "strings"
)

var data = make(map[string]string)

func handle(conn net.Conn) {
    defer conn.Close()

    scanner := bufio.NewScanner(conn)
    for scanner.Scan() {
        ln := scanner.Text()
        fs := strings.Fields(ln)

        if len(fs) < 2 {
            io.WriteString(conn, "This is an in-memory database \n" +
            "Use SET, GET, DEL like this: \n" +
            "SET key value \n" +
            "GET key \n" +
            "DEL key \n\n" +
            "For example - try these commands: \n" +
            "SET fav chocolate \n" +
            "GET fav \n\n\n")
            continue
        }

        switch fs[0] {
        case "GET":
            key := fs[1]
            value := data[key]
            fmt.Fprintf(conn, "%s\n", value)
        case "SET":
            if len(fs) != 3 {
                io.WriteString(conn, "EXPECTED VALUE\n")
                continue
            }
            key := fs[1]
            value := fs[2]
            data[key] = value
        case "DEL":
            key := fs[1]
            delete(data, key)
        default:
            io.WriteString(conn, "INVALID COMMAND "+fs[0]+"\n")
        }
    }
}

func main() {
    li, err := net.Listen("tcp", ":9000")
    if err != nil {
        log.Fatalln(err)
    }
    defer li.Close()

    for {
        conn, err := li.Accept()
        if err != nil {
            log.Fatalln(err)
        }
        handle(conn)
    }
}

并添加并发性:

package main

import (
    "bufio"
    "fmt"
    "io"
    "log"
    "net"
    "strings"
)

type Command struct {
    Fields []string
    Result chan string
}

func redisServer(commands chan Command) {
    var data = make(map[string]string)
    for cmd := range commands {
        if len(cmd.Fields) < 2 {
            cmd.Result <- "Expected at least 2 arguments"
            continue
        }

        fmt.Println("GOT COMMAND", cmd)

        switch cmd.Fields[0] {
        // GET <KEY>
        case "GET":
            key := cmd.Fields[1]
            value := data[key]

            cmd.Result <- value

        // SET <KEY> <VALUE>
        case "SET":
            if len(cmd.Fields) != 3 {
                cmd.Result <- "EXPECTED VALUE"
                continue
            }
            key := cmd.Fields[1]
            value := cmd.Fields[2]
            data[key] = value
            cmd.Result <- ""
        // DEL <KEY>
        case "DEL":
            key := cmd.Fields[1]
            delete(data, key)
            cmd.Result <- ""
        default:
            cmd.Result <- "INVALID COMMAND " + cmd.Fields[0] + "\n"
        }
    }
}

func handle(commands chan Command, conn net.Conn) {
    defer conn.Close()

    scanner := bufio.NewScanner(conn)
    for scanner.Scan() {
        ln := scanner.Text()
        fs := strings.Fields(ln)

        result := make(chan string)
        commands <- Command{
            Fields: fs,
            Result: result,
        }

        io.WriteString(conn, <-result+"\n")
    }

}

func main() {
    li, err := net.Listen("tcp", ":9000")
    if err != nil {
        log.Fatalln(err)
    }
    defer li.Close()

    commands := make(chan Command)
    go redisServer(commands)

    for {
        conn, err := li.Accept()
        if err != nil {
            log.Fatalln(err)
        }

        go handle(commands, conn)
    }
}

查看我的lectures from my CSUF class describing all of this here 。和one more great resource .

关于go - 如何在Golang中使用gob连续监听客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34195658/

相关文章:

c# - 你如何在 C# 中获取原始 TCP 数据包?

go - TCP监听器没有完全关闭

Go channel 未接收/打印发送到 channel 的最后一个值

go - 在缓冲的阅读器中查找

c++ - 什么是 C++ 中的标准延迟/终结器实现?

http - 长时间运行的 http 连接永远不会得到响应

java - 有没有办法保持与静态 Web 服务的连接?

linux - 在 Golang 中统计 linux 上的进程

C:使用 select() 写入新客户端

mysql - CloverETL mySQL 数据库 SSL 安全连接 & 参数