okay!
please explain this (see source below)
– with gets –
“thread: startup”
“accept new socket”
"-- get data —"
“get cmd: [hello, nice to see you\n]”
"-- get data —"
“get cmd: [hello, nice to see you\n]”
"-- get data —"
“get cmd: [hello, nice to see you\n]”
“socket is gone”
“thread: shutdown”
– with read –
“thread: startup”
“accept new socket”
"-- get data —"
“get cmd: [hello, nice to see you\nhello, nice to see you\nhello, nice
to see you\n]”
“socket is gone”
“thread: shutdown”
the code
-------------------- srv -----------------------------
require 'socket’
require ‘tk’
$srv = TCPServer.new(“127.0.0.1”, 4010)
exit if $srv.nil?
addr = $srv.addr
addr.shift
p sprintf(“server is on %s\n”, addr.join(":"))
$srv.sync= true
$socks = [$srv]
$bEnd = false
#------------------ thread srv
$a = Thread.new do
p "thread: startup"
while $bEnd == false
sleep(0.050)
nsock = select($socks, nil, nil, 0);
next if nsock == nil
for s in nsock[0]
if s == $srv
ns = s.accept
ns.sync= true
$socks.push(ns)
p "accept new socket"
elsif s.eof?
s.close
$socks.delete(s)
p "socket is gone"
else
p "-- get data ---"
str = s.read; # read or gets
p sprintf("get cmd: [%s]", str)
end
end
end
p "thread: shutdown"
end
Tk.mainloop
$bEnd = true
$a.join
------------------- client ----------------------
require ‘socket’
p "trying to connect…"
begin
$sock = TCPSocket.new(“127.0.0.1”, 4010)
rescue
p "no connection"
exit
end
p “connected”
3.times do
sleep(1)
p “send”
$sock.send(“hello, nice to see you\n”, 0)
end
daniel