简体   繁体   English

从Socket Golang读取数据

[英]Reading Data from Socket Golang

I'm trying to read data from a telnet session in golang. 我正在尝试从golang中的telnet会话读取数据。 I wrote the following functions in an attempt to accomplish this. 为了达到这个目的,我编写了以下函数。

Initially I was having an issue where I was reading from a socket with no data so it would lock and never return. 最初,我遇到一个问题,即我从没有数据的套接字读取数据,因此它将锁定并且永远不会返回。 BufferSocketData is my attempt to work around this issue as I can't know if there is data to read. BufferSocketData是我尝试解决此问题的方法,因为我不知道是否有要读取的数据。 The idea is it will wait 1 second before determining there is not data in the socket and return an empty string. 这个想法是它将等待1秒钟,然后再确定套接字中没有数据并返回一个空字符串。

GetData seems to work the first time there is new data in the buffer, but beyond that it gets no new data. GetData似乎第一次在缓冲区中有新数据时起作用,但是除此之外,它没有任何新数据。 I'm sure this has something to do with my use of goroutines and channels, I'm new to go and I'm sure I'm not using them correctly. 我确定这与我使用goroutine和通道有关,我是新手,而且我确定我没有正确使用它们。

Any ideas as to why my subsequent reads return no data? 关于为什么我的后续读取不返回任何数据的任何想法?

/*
ReadDataFromSocket - Attempts to read any data in the socket.
*/
func ReadDataFromSocket(sock io.Reader, c chan string) {
    var recvData = make([]byte, 1024)
    var numBytes, _ = sock.Read(recvData)
    c <- string(recvData[:numBytes])
}

/*
BufferSocketData - Read information from the socket and store it in the buffer.
*/
func (tn *TelnetLib) BufferSocketData(inp chan string, out chan string) {
    var data string
    var timeout int64 = 1000 // 1 second timeout.
    var start = utils.GetTimestamp()

    for utils.GetTimestamp()-start < timeout {
        select {
        case data = <-inp:
        default:
        }
        if data != "" {
            break
        }
    }
    out <- data
}

/*
GetData - Start goroutines to get and buffer data.
*/
func (tn *TelnetLib) GetData() {
    var sockCh = make(chan string)
    var buffCh = make(chan string)

    go ReadDataFromSocket(tn.Conn, sockCh)
    go tn.BufferSocketData(sockCh, buffCh)

    var data = <-buffCh

    if data != "" {
        tn.Buffer += data
    }
}

Please let me know if you need any additional information. 如果您需要任何其他信息,请告诉我。

Use SetReadDeadline to read data with a time limit: 使用SetReadDeadline读取具有时间限制的数据:

func (tn *TelnetLib) GetData() {
    tn.Conn.SetReadDeadline(time.Second)
    recvData := make([]byte, 1024)
    n, err := tn.Conn.Read(recvData)
    if n > 0 {
       // do something with recvData[:n]
    }
    if e, ok := err.(interface{ Timeout() bool }); ok && e.Timeout() {
        // handle timeout
    } else if err != nil {
       // handle error
    }
}

Note that a single call Read may not read all data sent by the peer. 请注意,单次呼叫Read可能无法读取对等方发送的所有数据。 You may want to accumulate data by calling Read in a loop or call io.ReadFull . 您可能需要通过循环调用Read或io.ReadFull来积累数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM