# TcpIp, Sockets, Gserver question

**URL:** <https://www.ruby-forum.com/t/tcpip-sockets-gserver-question/188484>\
**Category:** Ruby\
**Created:** [May 12, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/tcpip-sockets-gserver-question/188484 "2010-05-12T18:45:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [May 12, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/tcpip-sockets-gserver-question/188484/1 "2010-05-12T18:45:24Z")

</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  
print "\> "  
command\_string = gets  
break if command\_string =~ /x/  
tcp\_socket.puts command\_string  
servers\_resp = tcp\_socket.gets.chomp  
puts servers\_resp  
end

SERVER:

require ‘gserver’

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

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

```

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?

>

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [May 12, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/tcpip-sockets-gserver-question/188484/2 "2010-05-12T19:29:11Z")

</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
