# Create server

**URL:** <https://rubytalk.org/t/create-server/45874>\
**Category:** ruby-talk\
**Created:** [14 April 2008 18:05 UTC](https://rubytalk.org/t/create-server/45874 "2008-04-14T18:05:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vince\_Forgetta](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@Vince\_Forgetta](https://rubytalk.org/u/Vince_Forgetta)\
**Post date:** [14 April 2008 18:05 UTC](https://rubytalk.org/t/create-server/45874/1 "2008-04-14T18:05:26Z")

</div>

Hi all,

I am interested in using GServer but the server hangs after ~5-10 attempts  
(per max connections to allow). Example code I used is from Peter Coopers  
book, Beginning Ruby From Novice to Professional:

[http://www.springerlink.com/content/ph5611n3q3uu0619/](http://www.springerlink.com/content/ph5611n3q3uu0619/)

Is this normal?

Thanks.

Vince

---

<div class="post-metadata">

**Author:** ![Vince\_Forgetta](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@Vince\_Forgetta](https://rubytalk.org/u/Vince_Forgetta)\
**Post date:** [14 April 2008 18:16 UTC](https://rubytalk.org/t/create-server/45874/2 "2008-04-14T18:16:10Z")

</div>

Figured it out.

I need to sleep between successive calls to TCPSocket.new (see simple  
example below):

require 'socket'  
for i in 0..20  
&nbsp;&nbsp;session = TCPSocket.new('localhost', '1234')  
&nbsp;&nbsp;session.puts "test"  
&nbsp;&nbsp;dat = session.gets  
&nbsp;&nbsp;p dat  
&nbsp;&nbsp;session.close  
&nbsp;&nbsp;sleep 0.5  
end

Using 'sleep 0' cause the server to hang. Is there a way around this?

Vince

> **···**
>
> On Mon, Apr 14, 2008 at 2:05 PM, Vince Forgetta \<forgetta@gmail.com\> wrote:
> 
> > Hi all,
> > 
> > I am interested in using GServer but the server hangs after ~5-10 attempts  
> > (per max connections to allow). Example code I used is from Peter Coopers  
> > book, Beginning Ruby From Novice to Professional:
> > 
> > [http://www.springerlink.com/content/ph5611n3q3uu0619/](http://www.springerlink.com/content/ph5611n3q3uu0619/)
> > 
> > Is this normal?
> > 
> > Thanks.
> > 
> > Vince

---

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [14 April 2008 18:46 UTC](https://rubytalk.org/t/create-server/45874/3 "2008-04-14T18:46:08Z")

</div>

Perhaps then you should paste server code, rather than client code.

> **···**
>
> On Tue, 2008-04-15 at 03:16 +0900, Vince Forgetta wrote:
> 
> > Figured it out.
> > 
> > I need to sleep between successive calls to TCPSocket.new (see simple  
> > example below):
> > 
> > require 'socket'  
> > for i in 0..20  
> > &nbsp;&nbsp;session = TCPSocket.new('localhost', '1234')  
> > &nbsp;&nbsp;session.puts "test"  
> > &nbsp;&nbsp;dat = session.gets  
> > &nbsp;&nbsp;p dat  
> > &nbsp;&nbsp;session.close  
> > &nbsp;&nbsp;sleep 0.5  
> > end
> > 
> > Using 'sleep 0' cause the server to hang. Is there a way around this?

---

<div class="post-metadata">

**Author:** ![Vince\_Forgetta](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@Vince\_Forgetta](https://rubytalk.org/u/Vince_Forgetta)\
**Post date:** [14 April 2008 18:54 UTC](https://rubytalk.org/t/create-server/45874/4 "2008-04-14T18:54:36Z")

</div>

Sorry, here is the code:

require 'gserver'  
class HelloServer \< GServer  
&nbsp;&nbsp;def serve(io)  
&nbsp;&nbsp;&nbsp;&nbsp;line = io.gets  
&nbsp;&nbsp;&nbsp;&nbsp;io.puts("#{line.chomp}")  
&nbsp;&nbsp;end  
end  
server = HelloServer.new(1234, '127.0.0.1', 1)  
server.audit = true  
server.start  
server.join

> **···**
>
> On Mon, Apr 14, 2008 at 2:46 PM, hemant kumar \<gethemant@gmail.com\> wrote:
> 
> > On Tue, 2008-04-15 at 03:16 +0900, Vince Forgetta wrote:  
> > \> Figured it out.  
> > \>  
> > \> I need to sleep between successive calls to TCPSocket.new (see simple  
> > \> example below):  
> > \>  
> > \> require 'socket'  
> > \> for i in 0..20  
> > \> session = TCPSocket.new('localhost', '1234')  
> > \> session.puts "test"  
> > \> dat = session.gets  
> > \> p dat  
> > \> session.close  
> > \> sleep 0.5  
> > \> end  
> > \>  
> > \> Using 'sleep 0' cause the server to hang. Is there a way around this?  
> > \>
> > 
> > Perhaps then you should paste server code, rather than client code.

---

<div class="post-metadata">

**Author:** ![Vince\_Forgetta](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@Vince\_Forgetta](https://rubytalk.org/u/Vince_Forgetta)\
**Post date:** [16 April 2008 13:41 UTC](https://rubytalk.org/t/create-server/45874/5 "2008-04-16T13:41:56Z")

</div>

Solved my problem. Used select instead of threads to implement the server.  
Got the code from "The Ruby Programming Language". What a great book!

Vince

> **···**
>
> On Mon, Apr 14, 2008 at 2:54 PM, Vince Forgetta \<forgetta@gmail.com\> wrote:
> 
> > Sorry, here is the code:
> > 
> > require 'gserver'  
> > class HelloServer \< GServer  
> > def serve(io)  
> > &nbsp;&nbsp;&nbsp;line = io.gets  
> > &nbsp;&nbsp;&nbsp;io.puts("#{line.chomp}")  
> > end  
> > end  
> > server = HelloServer.new(1234, '127.0.0.1', 1)  
> > server.audit = true  
> > server.start  
> > server.join
> > 
> > On Mon, Apr 14, 2008 at 2:46 PM, hemant kumar \<gethemant@gmail.com\> wrote:
> > 
> > \>  
> > \> On Tue, 2008-04-15 at 03:16 +0900, Vince Forgetta wrote:  
> > \> \> Figured it out.  
> > \> \>  
> > \> \> I need to sleep between successive calls to TCPSocket.new (see simple  
> > \> \> example below):  
> > \> \>  
> > \> \> require 'socket'  
> > \> \> for i in 0..20  
> > \> \> session = TCPSocket.new('localhost', '1234')  
> > \> \> session.puts "test"  
> > \> \> dat = session.gets  
> > \> \> p dat  
> > \> \> session.close  
> > \> \> sleep 0.5  
> > \> \> end  
> > \> \>  
> > \> \> Using 'sleep 0' cause the server to hang. Is there a way around this?  
> > \> \>  
> > \>  
> > \> Perhaps then you should paste server code, rather than client code.  
> > \>  
> > \>  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Daniel\_Finnie](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Daniel\_Finnie](https://rubytalk.org/u/Daniel_Finnie)\
**Post date:** [16 April 2008 21:59 UTC](https://rubytalk.org/t/create-server/45874/6 "2008-04-16T21:59:27Z")

</div>

Hi,

I'm glad you solved your problem, I just had two comments:

- "#{line.chomp}" is equivalent to line.chomp.  
- sleep 0 by definition sleeps forever:  
puts "A"  
sleep 0  
puts "B"

B is never written

Dan

> **···**
>
> On Wed, Apr 16, 2008 at 9:41 AM, Vince Forgetta \<forgetta@gmail.com\> wrote:
> 
> > Solved my problem. Used select instead of threads to implement the server.  
> > Got the code from "The Ruby Programming Language". What a great book!
> > 
> > Vince
> > 
> > On Mon, Apr 14, 2008 at 2:54 PM, Vince Forgetta \<forgetta@gmail.com\> wrote:
> > 
> > \> Sorry, here is the code:  
> > \>  
> > \> require 'gserver'  
> > \> class HelloServer \< GServer  
> > \> def serve(io)  
> > \> line = io.gets  
> > \> io.puts("#{line.chomp}")  
> > \> end  
> > \> end  
> > \> server = HelloServer.new(1234, '127.0.0.1', 1)  
> > \> server.audit = true  
> > \> server.start  
> > \> server.join  
> > \>  
> > \> On Mon, Apr 14, 2008 at 2:46 PM, hemant kumar \<gethemant@gmail.com\> wrote:  
> > \>  
> > \> \>  
> > \> \> On Tue, 2008-04-15 at 03:16 +0900, Vince Forgetta wrote:  
> > \> \> \> Figured it out.  
> > \> \> \>  
> > \> \> \> I need to sleep between successive calls to TCPSocket.new (see simple  
> > \> \> \> example below):  
> > \> \> \>  
> > \> \> \> require 'socket'  
> > \> \> \> for i in 0..20  
> > \> \> \> session = TCPSocket.new('localhost', '1234')  
> > \> \> \> session.puts "test"  
> > \> \> \> dat = session.gets  
> > \> \> \> p dat  
> > \> \> \> session.close  
> > \> \> \> sleep 0.5  
> > \> \> \> end  
> > \> \> \>  
> > \> \> \> Using 'sleep 0' cause the server to hang. Is there a way around this?  
> > \> \> \>  
> > \> \>  
> > \> \> Perhaps then you should paste server code, rather than client code.  
> > \> \>  
> > \> \>  
> > \> \>  
> > \> \>  
> > \>
