Okay, so I have a really simple GServer server and an even simpler TCPSocket client (I looked into writing my own async TCPServer, but that turned out to be bigger shoes than I can fill at the moment, so I stumbled across the wonderul GServer).
Now I have this code in my server.rb:
require 'gserver'
class PServer < GServer
def initialize
super(9998)
end
def serve client_msg
for byte in client_msg.bytes
print byte.chr #Just playing, heh
end
print "\n"
client_msg.print 'Lala'
end
end
s = PServer.new
s.audit = true
s.debug = true
s.start
s.join
So, when I run it, I get no problems (I think) and get this output:
[Date and stuff, too long to type] PServer 127.0.0.1:9998 start
The client.rb code:
require 'socket'
client = TCPSocket.new('localhost', 9998)
client.print "Hi!"
p client.gets
client.close
Now, when I run the client, the server reports that it connected, the “Hi!” message. The client ‘floats’ open (sorry for the lack of a better expression) the whole time, leaving the connection open (however “Lala” is not printed out).
When I run the client again, omitting the line p client.gets; the server reports a connection, the “Hi!” and the disconnection of the client. The client just executes and I am returned to the good old prompt. Now, I’m wondering why this is happening. Is it the GServer, the Client, am I doing something dreadfully wrong? Please tell meee. I’m in a metaphorical agony right now.
Thanks for even reading this gigantic text (which is probably full of typos).
xo
I think you have the wrong idea about what the parameter to
serveis. It’s not a message, it is an IO object. Callingbyteson the IO object will try to read it until there is nothing left to read (that is, until the connection is closed). You should usegetsfor line-based communication, orreadfor binary protocols.Try replacing your
servemethod with this one: