GO编程,在阅读器事件上阻止读取功能

我是 Go 编程的初学者,我对 bufio 读者的问题感到困惑。我正在编写一种聊天客户端,它必须实时显示和发送我们的消息。但是直到我在我的终端中按下回车键,我收到的消息才会显示。经过几次测试,问题似乎出在我的“inputListener()”函数上,因为如果我在读取来自服务器的消息后放置它,来自服务器的消息将首先显示。我认为 Read 函数可能会阻塞我的循环,直到它得到一个 '\n' 或类似的东西。


这是我的代码:


package main

import "os"

import "strconv"

import "net"

import "bufio"


/*Recovery our input message into a buffer*/

func inputListener()([] byte){

    buf := make([]byte, 512)

    readerInput := bufio.NewReader(os.Stdin)

    _, err := readerInput.Read(buf)

    if err != nil{

        panic("Error reading input.")

        os.Exit(0)

    }

    return buf

}



func main(){

if len(os.Args) != 3{

    println("Usage: ",os.Args[0], " <host> <port>\n")

    os.Exit(0)

}


//Recovery the port.

port, err := strconv.Atoi(os.Args[2])

if err != nil{

  panic("Error during the port recovery\n")

  os.Exit(0)

}

println(port)


/*Join the adresse*/

addr := os.Args[1] + ":" + strconv.Itoa(port)

println(addr)


/*  sources -- https://golang.org/pkg/net/  */


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

if err != nil{

    panic("Error connecting " + addr)

    os.Exit(0)

}



buf := make([]byte, 512)

t := make([]byte, 512)


for {


    /*Receive data from server*/

    size, err := conn.Read(buf)

    if err != nil {

        panic("Error reading output.")

        os.Exit(0)

    }

    if size >= 0{

        print(string(buf[0:size]))

    }


    /*Data we need to send*/

    t = inputListener()

    if len(t) >= 0{

        conn.Write(t)

    }


conn.Close()

}

我需要按收到的每条消息回车:/


预先感谢您的回答!


开满天机
浏览 128回答 1
1回答

慕尼黑的夜晚无繁华

你可以尝试这样的事情:package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "io"&nbsp; &nbsp; "net"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strconv")/*Recovery our input message into a buffer*/func inputListener() []byte {&nbsp; &nbsp; buf := make([]byte, 512)&nbsp; &nbsp; readerInput := bufio.NewReader(os.Stdin)&nbsp; &nbsp; _, err := readerInput.Read(buf)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic("Error reading input.")&nbsp; &nbsp; }&nbsp; &nbsp; return buf}func main() {&nbsp; &nbsp; if len(os.Args) != 3 {&nbsp; &nbsp; &nbsp; &nbsp; println("Usage: ", os.Args[0], " <host> <port>\n")&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(0)&nbsp; &nbsp; }&nbsp; &nbsp; //Recovery the port.&nbsp; &nbsp; port, err := strconv.Atoi(os.Args[2])&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic("Error during the port recovery\n")&nbsp; &nbsp; }&nbsp; &nbsp; println(port)&nbsp; &nbsp; /*Join the adresse*/&nbsp; &nbsp; addr := os.Args[1] + ":" + strconv.Itoa(port)&nbsp; &nbsp; println(addr)&nbsp; &nbsp; /*&nbsp; sources -- https://golang.org/pkg/net/&nbsp; */&nbsp; &nbsp; conn, err := net.Dial("tcp", addr)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic("Error connecting " + addr)&nbsp; &nbsp; }&nbsp; &nbsp; defer conn.Close()&nbsp; &nbsp; go io.Copy(os.Stdout, conn)&nbsp; &nbsp; r := bufio.NewReader(os.Stdin)&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; p, err := r.ReadSlice('\n')&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic("Error reading output.")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; conn.Write(p)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go