What am I doing wrong? -Chat server code-

C

Curtis Zimmerman

I am running ruby 1.8.6 on WinXP.

I get the following error when I try to run this code:

httpserv.rb:22:in `runme': undefined local variable or method
`connection_accept' for #<ServerBrutus:0x282af80> (NameError)

Here's the code (taken from a tutorial site by IBM):

--==--

#!/usr/bin/ruby

require 'socket'

class ServerBrutus

def initialize(servAddr, servPort)
@servPort = servPort
@servAddr = servAddr
@socketThreads = Array::new
@cx = TCPServer.new("", servPort)
printf("Server started on port: %d\n", servPort)
@socketThreads.push(@cx)
end # initialize

def runme
while 1
conChk = select(@socketThreads, nil, nil, nil)
if conChk != nil then
for socket in conChk[0]
if socket == @cx then
connection_accept
else
if socket.eof? then
str = sprintf("Client left
%s%s\n",socket.peeraddr[2],socket.peeraddr[1])
broadcast_string(str, socket)
socket.close
@socketThreads.delete(socket)
else
str = sprintf("[%s|%s]:
%s",socket.peeraddr[2],socket.peeraddr[1],socket.gets())
broadcast_string(str, socket)
end # if cx
end # for sock
end # if conChk
end # while infinite
end # runme

def broadcast_string(str, omit_sock)
@socketThreads.each do |clientSocket|
if clientSocket != @cx && clientSocket != omit_sock
clientSocket.write(str)
end
end
end # broadcast_string

def connection_accept
newSocket = @cx.accept
@socketThreads.push(newSocket)
newSocket.write("You're connected to the server!\n")
str = sprintf("Client joined
%s%s\n",newSocket.peeraddr[2],newSocket.peeraddr[1])
broadcast_string(str, newSocket)
end # connection_accept

end # class ServerBrutus

servAddr = 'localhost'
servPort = 180
myChatServer = ServerBrutus.new(servAddr, servPort)
myChatServer.runme
end

--==--

What am I doing wrong? I'm using FreeRIDE, but it poops the same error
on the command prompt. If I do not include the last 'end', it generates
a syntax error on the last line ('myChatServer.runme').

Any help please? Thanks ahead of time! :)
 
C

Curtis Zimmerman

First of all, thanks for helping me out. I had a problem that I
personally couldn't solve, and needed help. Second, I hadn't touched
Ruby before about six hours ago, so this is completely new territory.
Despite the fact that it was a miserably simple mistake, it didn't pop
right out at me. Third, I'm not sure where you're going with your
insistence about querying the origins of this code? Would I imply that I
(me, myself) was doing something wrong if the code had been taken
*directly* from an IBM site? I didn't bill myself as being in any way
associated with IBM. I thought this was "rather obvious" that it was
"transcribed", modified code, that it wasn't taken directly from an IBM
site.
 
A

Alex Young

Curtis said:
I am running ruby 1.8.6 on WinXP.
Really? What does ruby -v say?
I get the following error when I try to run this code:

httpserv.rb:22:in `runme': undefined local variable or method
`connection_accept' for #<ServerBrutus:0x282af80> (NameError)

Here's the code (taken from a tutorial site by IBM):

--==--

#!/usr/bin/ruby

require 'socket'

class ServerBrutus

def initialize(servAddr, servPort)
@servPort = servPort
@servAddr = servAddr
@socketThreads = Array::new
@cx = TCPServer.new("", servPort)
printf("Server started on port: %d\n", servPort)
@socketThreads.push(@cx)
end # initialize

def runme
while 1
conChk = select(@socketThreads, nil, nil, nil)
if conChk != nil then
for socket in conChk[0]
if socket == @cx then
connection_accept
else
if socket.eof? then
str = sprintf("Client left
%s%s\n",socket.peeraddr[2],socket.peeraddr[1])
broadcast_string(str, socket)
socket.close
@socketThreads.delete(socket)
else
str = sprintf("[%s|%s]:
%s",socket.peeraddr[2],socket.peeraddr[1],socket.gets())
broadcast_string(str, socket)

end # if eof?

I think that's what you're missing. Without it, broadcast_string and
connection_accept end up being interpreted as nested methods within
runme, which is why you get an unrecognized method error.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top