dousi5358 2019-02-21 23:58
浏览 13
已采纳

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

I am a beginner in Go programming and I am confused about a problem about bufio readers. I'm programming a kind of chat client who must display and send our messages in live time. But messages that I receive are not displayed until I press enter in my terminal. After few tests, it seems to be my "inputListener()" function being the problem because, if I put it after reading messages from server, messages from server are displaying first. I supposed that the Read function may blocked my loop until it get a ' ' or something like that.

Here is my code:

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>
")
    os.Exit(0)
}

//Recovery the port.
port, err := strconv.Atoi(os.Args[2])
if err != nil{
  panic("Error during the port recovery
")
  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()
}

I need to press enter per messages received :/

Thank you in advance for your answers !

  • 写回答

1条回答 默认 最新

  • 普通网友 2019-02-22 00:31
    关注

    You be try something like this:

    package main
    
    import (
        "bufio"
        "io"
        "net"
        "os"
        "strconv"
    )
    
    /*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.")
        }
        return buf
    }
    
    func main() {
        if len(os.Args) != 3 {
            println("Usage: ", os.Args[0], " <host> <port>
    ")
            os.Exit(0)
        }
    
        //Recovery the port.
        port, err := strconv.Atoi(os.Args[2])
        if err != nil {
            panic("Error during the port recovery
    ")
        }
        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)
        }
        defer conn.Close()
    
        go io.Copy(os.Stdout, conn)
    
        r := bufio.NewReader(os.Stdin)
        for {
            p, err := r.ReadSlice('
    ')
            if err != nil {
                panic("Error reading output.")
            }
            conn.Write(p)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭