tcp multiple client / server... for each connection write

  • Thread starter Bigmac Turdsplash
  • Start date
B

Bigmac Turdsplash

im putting together a server with multiple clients establishing a
connection... I need each client connected to retreave a string from the
server then the client's will process this string... string = gets, this
is how the server sends out the string but only the first client will
process this string...
I need the server to do something like this...

for each.connection.write(string)

does that make sense? im not sure how to accomplish this...

here is the source...

#server.rb
require 'socket'

server = TCPServer.new(1234)

loop do
Thread.start(server.accept) do |connection|
print connection
str = gets
connection.write(str)# this needs to stay alive or actave for
each connection
end
end
#---------------end of server-----------


#Client.rb
require 'socket'

while true
sleep 1
streamSock = TCPSocket.new( "127.0.0.1", 1234 )
var1,var2,var3 = streamSock.recv( 100 ).chomp.split

if var1 == 'example'
print var2+"\n"
print var3+"\n"


elsif var1 == 'test'
print var2+"\n"
print var3+"\n"

end
end
#-----------------end client-------------
 
R

Roger Pack

for each.connection.write(string)

I'd probably make the server less multi-threaded, something like

all_cons = []
all_cons << server.accept
all_cons << server.accept # now it has two connections.

Also look into the use of select.
Cheers!
-=r
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top