How to create stable TCPS files?

I have a server that sends two messages to a client in a row:

require 'socket'
require 'thread'
connections = []
server = TCPServer.new(9998)
loop do
  Thread.start(server.accept) do |client|
    client.print 'Once'
    client.print 'Upon a time.'
  end #eo Thread
end #eo infinte loop

Customer:

require 'socket'
client = TCPSocket.new('localhost', 9998)
2.times { print client.read }
client.close

The client "stops" until I turn off the server, and only then prints messages. I know that adding client.closeto the server will fix the suspension, but I do not want to close the socket.

I know that some applications reuse TCPSocket. Thus, it can be Client > Server, Server > Client, Client > Server x2, etc. I am sure there is a way to do this in Ruby; I just can't figure out how.

So my list of questions:

  • How to create a persistent Server ↔ Client connection as described above?
  • Is there a better way to keep the server open without use loop?
  • , client.close ? ?
+1
2
  • Server ↔ Client, .

: , "" , , . client. , fork(2) ( , Unix-, Windows fork(2) ). select(2) poll(2) epoll(4), , , .

, . , -small-, 20-30 , . , , EventMachine libevent, . ( .)

2) , [ , , )

:), signal(7) .

3) , .close ? ?

, - , - TCP - , , , - .

, TCP/IP , , . ( .) , client.print() TCP-. client.read() , , , . , TCP- , TCP_NODELAY . ( , TCP_CORK, "" , . , , .)

, ; , , " ", , .

, / ASCII UTF-8, "\n"; print puts \n read() readline(). ( IO .) + .

+4

puts. socket.flush, , , . .

+1

All Articles