私はgolangをいじくり回していて、最初のコードは次のコードを持つ単純なIRCボットです。
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)\n", 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\n", line)
}
}
このコードを実行すると、ターミナルに次の出力が表示されます。
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)
接続がタイムアウトし続ける理由は何ですか?