# TcpIp, Sockets, Gserver question

**URL:** <https://rubytalk.org/t/tcpip-sockets-gserver-question/58499>\
**Category:** ruby-talk\
**Created:** [12 May 2010 16:45 UTC](https://rubytalk.org/t/tcpip-sockets-gserver-question/58499 "2010-05-12T16:45:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pito\_Salas](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Pito\_Salas](https://rubytalk.org/u/Pito_Salas)\
**Post date:** [12 May 2010 16:45 UTC](https://rubytalk.org/t/tcpip-sockets-gserver-question/58499/1 "2010-05-12T16:45:36Z")

</div>

I'm writing a simple pair of client and server apps to demonstrate a  
concept and it's doing something weird. I will show the source code  
below but basically there seems to be a syncing problem or a cache  
flushing problem. When text is sent from the client to the server it  
doesn't seem to receive it in one shot, or vice versa when the server  
responds the client doesn't see it when I expect, and as a result,  
things get out of sync in weird ways. Again I think it's some kind of  
cache flushing problem. I hope someone on this list has tried something  
like this and might be able to help me explain what's going on.

Thanks a lot for taking a look. I hope it's something really simple like  
some flag or setting somewhere. Thanks!!

Pito

Here's the problem code reduced to a simple test case:

CLIENT:

require 'socket'

tcp\_socket = TCPSocket.new('0.0.0.0', 8888)  
tcp\_socket.puts "Hi there server"  
servers\_resp = tcp\_socket.gets.chomp  
puts servers\_resp

loop do  
&nbsp;&nbsp;print "\> "  
&nbsp;&nbsp;command\_string = gets  
&nbsp;&nbsp;break if command\_string =~ /x/  
&nbsp;&nbsp;tcp\_socket.puts command\_string  
&nbsp;&nbsp;servers\_resp = tcp\_socket.gets.chomp  
&nbsp;&nbsp;puts servers\_resp  
end

SERVER:

require 'gserver'

class MyServer \< GServer  
&nbsp;&nbsp;def serve(io\_object)  
&nbsp;&nbsp;&nbsp;&nbsp;# Increment the client ID so each client gets a unique ID  
&nbsp;&nbsp;&nbsp;&nbsp;io\_object.sync = true

&nbsp;&nbsp;&nbsp;&nbsp;io\_object.puts("Welcome to the server")  
&nbsp;&nbsp;&nbsp;&nbsp;puts "client attached."  
&nbsp;&nbsp;&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = io\_object.readline  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /^t/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;io\_object.puts "The time of day is #{Time.now}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /^x/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Exiting!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /^f/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "received line #{line}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;io\_object.puts "What does #{line} mean, anyway?"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

puts "Starting to listen for a connection on port 8888"  
server = MyServer.new(8888)  
server.start  
server.join

SHELL TRANSCRIPT ON SERVER SIDE:

ruby deleteme.rb  
Starting to listen for a connection on port 8888  
client attached.  
received line Hi there server  
received line lalal  
received line dadad  
received line wowowow  
received line  
received line  
received line  
received line  
received line  
received line  
received line  
received line  
received line

AND ON THE CLIENT SIDE:  
$ ruby myclient.rb  
Welcome to the server

> lalal

What does Hi there server

> dadad

mean, anyway?

> wowowow

What does lalal

> 

mean, anyway?

> 

What does dadad

> 

mean, anyway?

> 

What does wowowow

> 

mean, anyway?

> 

What does

> 

mean, anyway?

> 

What does

> 

mean, anyway?

> **···**
>
> > 
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [12 May 2010 17:29 UTC](https://rubytalk.org/t/tcpip-sockets-gserver-question/58499/2 "2010-05-12T17:29:13Z")

</div>

> things get out of sync in weird ways. Again I think it's some kind of  
> cache flushing problem. I hope someone on this list has tried something  
> like this and might be able to help me explain what's going on.

in general if you were to a recv(1024) it might receive "half" of an  
incoming string, however, doing a gets \*should\* buffer up till the next  
newline and return it.

changing the client to puts servers\_resp.inspect  
yields this:

Welcome to the server

> 

"What does Hi there server\n"

> 

" mean, anyway?\n"

which may be a useful clue for ya.  
GL.  
-rp

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
