# What am I doing wrong? -Chat server code-

**URL:** <https://www.ruby-forum.com/t/what-am-i-doing-wrong-chat-server-code/66576>\
**Category:** Ruby\
**Created:** [August 27, 2006, 6:09am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-chat-server-code/66576 "2006-08-27T06:09:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![l0j1k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/59ef9b/32.png) [@l0j1k](https://www.ruby-forum.com/u/l0j1k)\
**Post date:** [August 27, 2006, 6:09am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-chat-server-code/66576/1 "2006-08-27T06:09:07Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![l0j1k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/59ef9b/32.png) [@l0j1k](https://www.ruby-forum.com/u/l0j1k)\
**Post date:** [August 27, 2006, 6:41am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-chat-server-code/66576/2 "2006-08-27T06:41:08Z")

</div>

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 method`connection\_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?

---

<div class="post-metadata">

**Author:** ![l0j1k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/59ef9b/32.png) [@l0j1k](https://www.ruby-forum.com/u/l0j1k)\
**Post date:** [August 27, 2006, 7:26am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-chat-server-code/66576/3 "2006-08-27T07:26:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![l0j1k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/59ef9b/32.png) [@l0j1k](https://www.ruby-forum.com/u/l0j1k)\
**Post date:** [August 27, 2006, 9:15am UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-chat-server-code/66576/4 "2006-08-27T09:15:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![l0j1k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/59ef9b/32.png) [@l0j1k](https://www.ruby-forum.com/u/l0j1k)\
**Post date:** [August 27, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong-chat-server-code/66576/5 "2006-08-27T18:14:50Z")

</div>

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.
