Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

go - Get instant output after dial TCP send

I'm creating something like a "bridge" to IRC. Via this bridge I can send IRC commands to the server but I need now to check the output from IRC after command send.

Actually code looks like this:

func main() {
   conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", "localhost", "6667"))
    if err != nil {
        panic(err)
    }

    out := make(chan string)
    go IrcListen(out, ircConn)

   // Here is a very large switch to exec some command, e.g. BanCmd(conn, out, channel, nickname)
}

func IrcListen(out chan<- string, ircConn net.Conn) {
    defer close(out)

    tp := textproto.NewReader(bufio.NewReader(ircConn))
    for {
        l, err := tp.ReadLine()
        if err != nil {
            panic(err)
        }

        if strings.HasPrefix(l, "PING") {
            execCmd(ircConn, "PONG timestamp")
        } else {
            out <- l
        }
    }
}

func BanCmd(c net.Conn, out <-chan string, channel string, nickname string) {
    execCmd(c, fmt.Sprintf("MODE #%s +b %s", channel, nickname))

    irc_out := <- out
    fmt.Println(irc_out) // Here I need to get the output of command, so the next line from output
}

func execCmd(conn net.Conn, command string) {
    fmt.Fprintf(conn, "%s
", command)
}

Commands are sent correctly to IRC server.
Now, via irc_out := <- out I want to out the response of executed command but currently I'm getting the full output line by line from beginning, e.g.
Full output starts like:

:my-server.irc 001 Damian :Welcome to the Internet Relay Network Damian
:my-server.irc 002 Damian :Your host is my-server.irc, running version oragono-2.4.0
:my-server.irc 003 Damian :This server was created Wed, 03 Feb 2021 15:45:35 UTC
:my-server.irc 004 Damian my-server.irc oragono-2.4.0 BERTZios CEIMRUabehiklmnoqstuv Iabehkloqv
:my-server.irc 005 Damian AWAYLEN=390 BOT=B CASEMAPPING=ascii CHANLIMIT=#:100 CHANMODES=Ibe,k,l,CEMRUimnstu CHANNELLEN=64 CHANTYPES=# ELIST=U EXCEPTS EXTBAN=,m INVEX KICKLEN=390 MAXLIST=beI:60 :are supported by this server
... etc

Now, with this on every command execute I'm getting line by line from the beginning:

IN: BanCmd(ircConn, out, pbCmd.Channel, pbCmd.Arguments.Nickname)
OUT: :my-server.irc 001 Damian :Welcome to the Internet Relay Network Damian

IN: BanCmd(ircConn, out, pbCmd.Channel, pbCmd.Arguments.Nickname)
OUT: :my-server.irc 002 Damian :Your host is my-server.irc, running version oragono-2.4.0

IN: BanCmd(ircConn, out, pbCmd.Channel, pbCmd.Arguments.Nickname)
OUT: :my-server.irc 003 Damian :This server was created Wed, 03 Feb 2021 15:45:35 UTC

IN: BanCmd(ircConn, out, pbCmd.Channel, pbCmd.Arguments.Nickname)
OUT: :my-server.irc 004 Damian my-server.irc oragono-2.4.0 BERTZios CEIMRUabehiklmnoqstuv Iabehkloqv

IN: BanCmd(ircConn, out, pbCmd.Channel, pbCmd.Arguments.Nickname)
OUT: :my-server.irc 005 Damian AWAYLEN=390 BOT=B CASEMAPPING=ascii CHANLIMIT=#:100 CHANMODES=Ibe,k,l,CEMRUimnstu CHANNELLEN=64 CHANTYPES=# ELIST=U EXCEPTS EXTBAN=,m INVEX KICKLEN=390 MAXLIST=beI:60 :are supported by this server

etc.

When I'll "scroll" to the end, I'll start getting the newest output line.
So, how can I get the last output after command execute?

question from:https://stackoverflow.com/questions/66052570/get-instant-output-after-dial-tcp-send

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...