简单的服务器客户端通信不起作用

这个看似简单的例子并没有按预期工作,我觉得问这个问题很糟糕,但这里是:


有一个客户端重试连接到服务器,发送消息,然后等待响应:


func client() {

    var conn net.Conn

    var err error


    // retry server until it is up

    for {

        conn, err = net.Dial("tcp", ":8081")

        if err == nil {

            break

        }

        log.Println(err)

        time.Sleep(time.Second)

    }


    // write to server

    _, err = conn.Write([]byte("request"))

    if err != nil {

        log.Println(err)

        return

    }


    // block & read from server

    var buf []byte

    n, err := conn.Read(buf)

    if err != nil {

        log.Println(err)

        return

    }

    log.Printf("From server: %s\n", buf[:n])

}

它连接到一个服务器,该服务器对于每个连接,读取并解释发送的数据,并在需要时发送响应:


func server() {

    ln, _ := net.Listen("tcp", ":8081")

    for {

        conn, _ := ln.Accept()

        go handleConn(conn)

    }

}


func handleConn(conn net.Conn) {

    var buf []byte

    n, err := conn.Read(buf)

    if err != nil {

        return

    }

    log.Printf("Server got: %s\n", buf)


    if string(buf[:n]) == "request" {

        _, _ = conn.Write([]byte("response"))

    }

}

全部由main函数驱动:


func main() {

    go client()

    server()

}

为了简洁起见,省略了错误处理。预期的行为是客户端将连接到服务器并发送消息“请求”,然后阻止读取。服务器接收“请求”并将消息“响应”发送回同一连接。客户端解锁,打印收到的消息并退出。相反,当程序运行时,会打印以下内容:


2019/09/01 22:24:02 From server: 

2019/09/01 22:24:02 Server got: 

表明没有数据交换,并且客户端没有阻塞。


ibeautiful
浏览 87回答 1
1回答

www说

客户端的循环很奇怪!如果读/写已结束,则循环没有意义。但错误只是这样:&nbsp; //var buf []byte <--- this read 0 bytes&nbsp; buf := make([]byte, 1024)&nbsp;&nbsp; n, err := conn.Read(buf)给您的建议:package mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "net"&nbsp; &nbsp; "time")func client() {&nbsp; &nbsp; var conn net.Conn&nbsp; &nbsp; var err error&nbsp; &nbsp; // retry server until it is up&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("Connecting...")&nbsp; &nbsp; &nbsp; &nbsp; conn, err = net.Dial("tcp", ":8082")&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(time.Second)&nbsp; &nbsp; &nbsp; &nbsp; // write to server&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("Writing...")&nbsp; &nbsp; &nbsp; &nbsp; _, err = conn.Write([]byte("request"))&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // block & read from server&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("Reading...")&nbsp; &nbsp; &nbsp; &nbsp; var buf []byte&nbsp; &nbsp; &nbsp; &nbsp; n, err := conn.Read(buf)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("From server: %s\n", buf[:n])&nbsp; &nbsp; }}func server() {&nbsp; &nbsp; ln, _ := net.Listen("tcp", ":8082")&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; conn, _ := ln.Accept()&nbsp; &nbsp; &nbsp; &nbsp; go handleConn(conn)&nbsp; &nbsp; }}func handleConn(conn net.Conn) {&nbsp; &nbsp; buf := make([]byte, 1024)&nbsp; &nbsp; n, err := conn.Read(buf)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; log.Printf("Server got: [%d bytes] %s\n", n, buf)&nbsp; &nbsp; if string(buf[:n]) == "request" {&nbsp; &nbsp; &nbsp; &nbsp; _, _ = conn.Write([]byte("response"))&nbsp; &nbsp; }&nbsp; &nbsp; conn.Close()}func main() {&nbsp; &nbsp; go client()&nbsp; &nbsp; server()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go