What am I doing wrong? -Chat server code-

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 methodconnection_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! :slight_smile:

Curtis Zimmerman wrote:

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 methodconnection_accept’ for #ServerBrutus:0x282af80 (NameError)

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

/ … snipped code listing

The code you listed has a rather obvious syntax error. Was it really
copied
directly from the IBM site, or did you transcribe it?

What am I doing wrong?

The code is broken. Try indenting the code using a beautifier, and you
will
discover that the carefully worked out indentations are wrong, they are
marked incorrectly as well, and the code is missing one “end” statement
within “runme”.

The remedy is to avoid commenting an “end” statement until you have
actually
matched it up with its corresponding beginning statement, by following
that
level of indentation to its origin.

Look at this:

def runme
while true
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 … # snipped
broadcast_string(str, socket)
socket.close
socketThreads.delete(socket)
else
str = sprintf(”[%s|%s] … #snipped
broadcast_string(str, socket)
end # if socket.eof
end # if socket == @cx
end # for socket in conChk[0]
end # if conChk != nil then
end # while true
end # runme

See the difference? This is accurately indented, and the error popped
out
right away.

Also, use “true” rather than “1”, for good form and portability.

If I do not include the last ‘end’, it generates
a syntax error on the last line (‘myChatServer.runme’).

Don’t apply patches without understanding why the problem came up in the
first place. That only adds to the confusion.

Again, is your code really copied directly, unchanged, from the IBM
site?

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.

The code you listed has a rather obvious syntax error. Was it really
copied
directly from the IBM site, or did you transcribe it?

Again, is your code really copied directly, unchanged, from the IBM
site?

Curtis Zimmerman wrote:

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?

I wanted to know whether the IBM site contained code with an error in
it, or
the error came in after the code was downloaded.

Would I imply that I
(me, myself) was doing something wrong if the code had been taken
directly from an IBM site?

I don’t care about that. I only wanted to know where the error
originated.
If the code on the IBM site was in error, I would want to let them know.
That way, tomorrow, we won’t be answering the same query about the same
code.

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.

Here is what you said in your original message:

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

With no other comments, certainly not the word “transcribed”. This
strongly
suggests that the code was “taken from a tutorial site by IBM.” In fact,
it
is difficult to give it any other interpretation.

Please don’t take this the wrong way. Errors are common in computer
programming. I just wanted to know where the error originated.

Curtis Zimmerman wrote:

I am running ruby 1.8.6 on WinXP.
Really? What does ruby -v say?

@socketThreads.push(@cx)
        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.