Simple socketing

K

Knut Mathiesen

Hi there, I recently started with Ruby and I am trying to make a very
simple irc bot, but I am having some slight problems though.

This is what I came up with:

require 'socket'
class Irc
def connect(server, port = 6667)
@socket = TCPsocket.new(server, port)
end
def to_server(text)
@socket.gets(text)
return true
end
def from_server()
return @socket.puts
end
end
irc = Irc.new
irc.connect("irc.homelien.no", 6667)
irc.to_server('NICK: testbot')
irc.to_server('USER: testbot "" "irc.efnet.net" :testbot')
go = true
while go == true
if (text = irc.from_server)
puts(text)
end
end


Doesn't work though, I just get the loop.
 
P

Phillip Hutchings

def to_server(text)
@socket.gets(text)
return true
end
def from_server()
return @socket.puts
end

You've got gets and puts the wrong way around. Read them as Get String
and Put String, gets reads from the socket and puts writes to it.

Also, there's a protocol error there, you want to_server to read like this:

def to_server(text)
@socket.puts(text.to_s+"\r\n") # The line breaks are important, must be CRLF.
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,221
Latest member
TrinidadKa

Latest Threads

Top