Okay, please explain this (see source below)

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

-- with gets --

[...]

-- with read --

You seems to make confusion between IO methods and Socket methods.

#read and #gets are IO methods and they have the same semantic than for a
File object, for example #gets try to read a line terminated by \n

#read without argument try to read all the data

pigeon% ri IO#read
---------------------------------------------------------------- IO#read
     ios.read( [anInteger] ) -> aString or nil

···

------------------------------------------------------------------------
     Reads at most anInteger bytes from the I/O stream, or to the end of
     file if anInteger is omitted. Returns nil if called at end of file.
        f = File.new("testfile")
        f.read(16) #=> "This is line one"

pigeon%

pigeon% ri IO#gets
---------------------------------------------------------------- IO#gets
     ios.gets( aSepString=$/ ) -> aString or nil
------------------------------------------------------------------------
     Reads the next ``line'' from the I/O stream; lines are separated by
     aSepString. A separator of nil reads the entire contents, and a
     zero-length separator reads the input a paragraph at a time (two
     successive newlines in the input separate paragraphs). The stream
     must be opened for reading or an IOerror will be raised. The line
     read in will be returned and also assigned to $_. Returns nil if
     called at end of file.
        File.new("testfile").gets #=> "This is line one\n"
        $_ #=> "This is line one\n"

pigeon%

The low level socket methods are #send and #recv

Guy Decoux