一个使用Go通道的Go回声服务器,但服务器没有回复。

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

A go echo server using go channel, but no reply from the server

问题

我正在尝试使用Go的通道(channel)和goroutine编写一个回显服务器,但服务器没有回复。以下服务器监听9090端口,并创建一个名为ch的通道来接收连接请求,然后将其传递给handleClient函数来处理连接的详细信息。以下代码有问题吗?在使用go build时没有出现错误。

package main

import (
    "fmt"
    "net"
)

const (
    CONN_HOST = "localhost"
    CONN_PORT = "9090"
    CONN_TYPE = "tcp"
)

func main() {
    listen, err := net.Listen(CONN_TYPE, CONN_HOST + ":" + CONN_PORT)
    if err != nil {
        fmt.Println("Error listening: ", err.Error())
        return
    }

    ch := make(chan net.Conn)
    go func() {
        for {
            conn, err := listen.Accept()
            if err != nil {
                fmt.Println("Error Accept: ", err.Error())
                return
            }
            ch <- conn
        }
    }()
    go handleClient(ch)
}

func handleClient(connChan <-chan net.Conn) {
    var tcpConn net.Conn
    // fmt.Println("Accepted new client: ", connChan.RemoteAddr().String())
    for {
        tcpConn = <-connChan
        go Serve(tcpConn)
    }
}

func Serve(conn net.Conn) {
  // 处理连接
}

以上代码看起来没有明显的错误。它使用goroutine来接受连接并将其发送到handleClient函数进行处理。你可以检查一下其他部分的代码,例如Serve函数,确保连接的处理逻辑正确。另外,你可以尝试在代码中添加一些调试输出,以便更好地理解程序的执行流程和可能的问题。

英文:

I am trying to use go channel and goroutine to write an echo server, but there is no reply from the server. The following server listens on port 9090, and create a channel ch to receive connection acceptances, then it pass to handleClient to handle the connection details. Is the following code wrong? It has no errors when under go build.

package main

import (
    &quot;fmt&quot;
    &quot;net&quot;
)

const (
    CONN_HOST = &quot;localhost&quot;
    CONN_PORT = &quot;9090&quot;
    CONN_TYPE = &quot;tcp&quot;
)

func main() {
    listen, err := net.Listen(CONN_TYPE, CONN_HOST + &quot;:&quot; + CONN_PORT)
    if err != nil {
	    fmt.Println(&quot;Error listening: &quot;, err.Error())
	    return
    }

    ch := make(chan net.Conn)
    go func() {
	    for {
	        conn, err := listen.Accept()
	        if err != nil {
		        fmt.Println(&quot;Error Accept: &quot;, err.Error())
    		    return
	        }
	        ch &lt;- conn
	    }
    }()
    go handleClient(ch)
}

func handleClient(connChan &lt;-chan net.Conn) {
    var tcpConn net.Conn
    // fmt.Println(&quot;Accepted new client: &quot;, connChan.RemoteAddr().String())
    for {
	    tcpConn = &lt;-connChan
	    go Serve(tcpConn)
    }
}

func Serve(conn net.Conn) {
  // handle the connection
}

答案1

得分: 2

只需稍微修改你的主函数:

ch := make(chan net.Conn)
go handleClient(ch)

for {
    conn, err := listen.Accept()
    if err != nil {
        fmt.Println("Error Accept:", err.Error())
        return
    }
    ch <- conn
}

这个for循环是服务器的主循环,如果你没有在其他地方退出服务器,它将一直运行下去。

英文:

Just change your main a bit:

ch := make(chan net.Conn)
go handleClient(ch)

for {
    conn, err := listen.Accept()
    if err != nil {
        fmt.Println(&quot;Error Accept: &quot;, err.Error())
        return
    }
    ch &lt;- conn
}

The for loop is the server's main loop and will run forever if you do not exit the server somewhere else.

huangapple
  • 本文由 发表于 2015年6月28日 15:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/31097200.html
匿名

发表评论

匿名网友

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

确定