V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
raw0xff
V2EX  ›  Go 编程语言

golang 的 net 库为什么接不到其他 tcp 消息?

  •  
  •   raw0xff · 2022-03-20 02:22:12 +08:00 · 1359 次点击
    这是一个创建于 1006 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是一个 go 教程的聊天服务器例子,例子的 S 端 C 端都没问题。 为什么我用网络调试助手 tcp client 可以连上,但是发信息收不到呢?我在 S 端的很多位置都 fmt.Println 了都找不到。

    //以下是服务端 // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. // License: creativecommons.org/licenses/by-nc-sa/4.0/ // See page 254. //!+ // Chat is a server that lets clients chat with each other. package main

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

    //!+broadcaster type client chan<- string // an outgoing message channel

    var ( entering = make(chan client) leaving = make(chan client) messages = make(chan string) // all incoming client messages )

    func broadcaster() { clients := make(map[client]bool) // all connected clients for { select { case msg := <-messages: // Broadcast incoming message to all // clients' outgoing message channels.

    		for cli := range clients {
    			cli <- msg
    			fmt.Println(msg)
    		}
    
    	case cli := <-entering:
    		clients[cli] = true
    
    	case cli := <-leaving:
    		delete(clients, cli)
    		close(cli)
    	}
    }
    

    }

    //!-broadcaster

    //!+handleConn func handleConn(conn net.Conn) { ch := make(chan string) // outgoing client messages go clientWriter(conn, ch)

    who := conn.RemoteAddr().String()
    ch <- "You are " + who
    messages <- who + " has arrived"
    entering <- ch
    
    input := bufio.NewScanner(conn)
    for input.Scan() {
    	messages <- who + ": " + input.Text()
    	fmt.Println(222)
    }
    // NOTE: ignoring potential errors from input.Err()
    
    leaving <- ch
    messages <- who + " has left"
    conn.Close()
    

    }

    func clientWriter(conn net.Conn, ch <-chan string) { for msg := range ch { fmt.Fprintln(conn, msg) // NOTE: ignoring network errors } }

    //!-handleConn

    //!+main func main() { listener, err := net.Listen("tcp", "localhost:8000") if err != nil { log.Fatal(err) }

    go broadcaster()
    for {
    	conn, err := listener.Accept()
    	fmt.Println(111)
    	if err != nil {
    		log.Print(err)
    		continue
    	}
    	go handleConn(conn)
    }
    

    } //!-main

    //以下是客户端 // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. // License: creativecommons.org/licenses/by-nc-sa/4.0/ // See page 227. // Netcat is a simple read/write client for TCP servers. package main

    import ( "io" "log" "net" "os" )

    //!+ func main() { conn, err := net.Dial("tcp", "localhost:8000") if err != nil { log.Fatal(err) } done := make(chan struct{}) go func() { io.Copy(os.Stdout, conn) // NOTE: ignoring errors log.Println("done") done <- struct{}{} // signal the main goroutine }() mustCopy(conn, os.Stdin) conn.Close() <-done // wait for background goroutine to finish }

    //!-

    func mustCopy(dst io.Writer, src io.Reader) { if _, err := io.Copy(dst, src); err != nil { log.Fatal(err) } }

    2 条回复    2022-03-21 13:46:53 +08:00
    tairan2006
        1
    tairan2006  
       2022-03-20 09:14:02 +08:00
    用 gist ,你这代码没眼看
    qfdk
        2
    qfdk  
       2022-03-21 13:46:53 +08:00 via iPhone
    @tairan2006 严重同意
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5507 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 01:48 · PVG 09:48 · LAX 17:48 · JFK 20:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.