JRuby - How to make a server socket MT

Hi,

How to make a socket to listen a port and make it multi-thread (one thread per client connection) with JRUBY ?

Anyone have examples or can give me a code?

Ruby has a Thread class. t = Thread.new do   ... end

Depending on whether you want to act as a socket server you can make use of ruby's TCPServer, or if you want to connect to a (socket) server there's the TCPSocket class.

What exactly do you want to do, what is your use case? Why do you need (long-living?) threads (per session or, even worse, "click"?)? Maybe BJ (http://codeforpeople.rubyforge.org/svn/bj/trunk/README) or delayed_job (http://github.com/tobi/delayed_job) suffice?

Hope this helps

I have a game server to my game in flash, but the server is in pure java, using sockets.

So i want port it do jruby, because i think its very fast and easy to make database operations and all ruby facilities.

What you think about it? Is a good solution?

I have make a sample, to test ruby thread, but no success. it run like a single thread.

t = Thread.new() do |n| puts “==>START THREAD” sleep 15 puts “==>END THREAD” end

t.join

arr = [1,2,3,4,5,6,7,8,9,10] for item in arr puts "==> Show item: " + item.to_s sleep 1 end

Thx.

the "t.join" synchronizes the spawned thread with the main thread (rendez-vous)

-> Ruby - Socket Programming

maybe this (in particular the "Multi-Client TCP Servers"-section) helps?

BTW: what's this to do with rails? :wink: