dslfjrmz70457 2012-11-12 10:33
浏览 79
已采纳

简单的golang IRC机器人保持超时

I'm tinkering with golang and my first code is a simple IRC bot with the following code:

package main

import ("net"
        "log"
        "bufio"
        "fmt"
        "net/textproto"
      )
type Bot struct{
        server string
        port string
        nick string
        user string
        channel string
        pass string
        pread, pwrite chan string
        conn net.Conn
}

func NewBot() *Bot {
        return &Bot{server: "irc.freenode.net",
                    port: "6667",
                    nick: "subsaharan",
                    channel: "#rapidsms", 
                    pass: "",
                    conn: nil,
                    user: "blaze"}
}
func (bot *Bot) Connect() (conn net.Conn, err error){
  conn, err = net.Dial("tcp",bot.server + ":" + bot.port)
  if err != nil{
    log.Fatal("unable to connect to IRC server ", err)
  }
  bot.conn = conn
  log.Printf("Connected to IRC server %s (%s)
", bot.server, bot.conn.RemoteAddr())
  return bot.conn, nil
}



func main(){
  ircbot := NewBot()
  conn, _ := ircbot.Connect()
  conn.Write([]byte("NICK " + ircbot.nick))
  conn.Write([]byte("JOIN " + ircbot.channel))
  defer conn.Close()

  reader := bufio.NewReader(conn)
  tp := textproto.NewReader( reader )
  for {
        line, err := tp.ReadLine()
        if err != nil {
            break // break loop on errors    
        }
        fmt.Printf("%s
", line)
    }

}

When I run this code I get the following output on the terminal:

2012/11/12 13:31:20 Connected to IRC server irc.freenode.net (193.219.128.49:6667)
:sendak.freenode.net NOTICE * :*** Looking up your hostname...
:sendak.freenode.net NOTICE * :*** Checking Ident
:sendak.freenode.net NOTICE * :*** Couldn't look up your hostname
:sendak.freenode.net NOTICE * :*** No Ident response
ERROR :Closing Link: 127.0.0.1 (Connection timed out)

Any reason why the connection keeps timing out?

  • 写回答

2条回答 默认 最新

  • douzhi7754 2012-11-12 12:03
    关注

    All commands sent to an IRC server have a maximum of 255 512 bytes and must be terminated with a carriage return and linefeed .

    conn.Write([]byte("NICK " + ircbot.nick + "
    "))
    conn.Write([]byte("JOIN " + ircbot.channel + "
    "))
    

    Additionally, freenode expects the USER command to be first thing it sees from you.

    conn.Write([]byte("USER "+ircbot.nick+" 8 * :" + ircbot.nick + "
    "))
    conn.Write([]byte("NICK " + ircbot.nick + "
    "))
    conn.Write([]byte("JOIN " + ircbot.channel + "
    "))
    

    As a side note, you can make your life a little easier by using fmt.Fprintf to format and send the data:

    fmt.Fprintf(conn, "USER %s 8 * :%s
    ", ircbot.nick, ircbot.nick)
    fmt.Fprintf(conn, "NICK %s
    ", ircbot.nick)
    fmt.Fprintf(conn, "JOIN %s
    ", ircbot.channel)
    

    The first parameter of fmt.Fprintf must be any type that satisfies the io.Writer interface. The net.Conn implementations all do this. As such you can pass them into any function which expects an io.Writer (or io.Reader for that matter) implementation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大