[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Client/server with threads

andreas.alin

8/19/2006 1:53:00 PM

Yow!
I'm trying to make a script that will act as a server (server 1), and
when a connection to the server server 1 is made, it will start a
thread that will connect to another server (server 2), and it will keep
the connection to server 2, even if the client disconnects.

The thing is that, as I've tried, my client/server will not connect to
another server, and if it does, the client can not disconnect.

Any ideas?

Thanks,
Andreas

----------------------

require 'socket'

class Session < Thread
def initalize(session_id)
super
@session_id = session_id
end

def session_id
@session_id
end

def send_message( message
# This method should send a message to
# server 2
puts message
end
end

threads = []

server = TCPServer.new("0.0.0.0", 12121)

while (session = server.accept)
session_id, message = session.gets.split(";", 2)

t = threads.map {|t| t if t["session_id"] == session_id }.delete_if
{|s| s == nil}.first
puts t
if t
puts "Found thread with existing session_id: #{ session_id }"
t.send :send_message, message
else
threads << Session.new(session) do |t|
Thread.current["session_id"] = session_id
puts "Creating thread for #{ session_id }"
end
end
session.close
end

17 Answers

Francis Cianfrocca

8/19/2006 2:01:00 PM

0

On 8/19/06, andreas.alin@gmail.com <andreas.alin@gmail.com> wrote:
> Yow!
> I'm trying to make a script that will act as a server (server 1), and
> when a connection to the server server 1 is made, it will start a
> thread that will connect to another server (server 2), and it will keep
> the connection to server 2, even if the client disconnects.
>
> The thing is that, as I've tried, my client/server will not connect to
> another server, and if it does, the client can not disconnect.
>
> Any ideas?
>
> Thanks,
> Andreas
>
> ----------------------
>
> require 'socket'
>
> class Session < Thread
> def initalize(session_id)
> super
> @session_id = session_id
> end
>
> def session_id
> @session_id
> end
>
> def send_message( message
> # This method should send a message to
> # server 2
> puts message
> end
> end
>
> threads = []
>
> server = TCPServer.new("0.0.0.0", 12121)
>
> while (session = server.accept)
> session_id, message = session.gets.split(";", 2)
>
> t = threads.map {|t| t if t["session_id"] == session_id }.delete_if
> {|s| s == nil}.first
> puts t
> if t
> puts "Found thread with existing session_id: #{ session_id }"
> t.send :send_message, message
> else
> threads << Session.new(session) do |t|
> Thread.current["session_id"] = session_id
> puts "Creating thread for #{ session_id }"
> end
> end
> session.close
> end
>
>
>


First, make sure your constructor is getting called. You spelled it
"initalize" instead of "initialize"

andreas.alin

8/19/2006 2:28:00 PM

0

Oh, thanks. :) Didn't see that. :)

But, now, there are some problems. The program gets stuck in the loop
on line 12, which I want the program to do in the background. (and it
shouldn't get stuck) :)

require 'socket'

class Session < Thread
def initialize(session)
super
puts session
@session_id, @nick, @channels = session.split(";", 3)
@host = "wowow"
@channels = @channels.split(",")
@connected = 0
@connection = TCPSocket.new("127.0.0.1", 12122)
until @connection.closed?
print "a"
if @connected == 0
login
@connected = 1
end
raw = @connection.gets
parse( raw.strip ) unless raw.nil?
end
end

def session_id
@session_id
end

def send_message( message )
puts message
end

def irc_join( channel )
irc_raw( "JOIN #{ channel }" )
end

def irc_nick( nick )
irc_raw( "NICK #{ nick }" )
@nick = nick
end

private

def irc_raw( msg )
@connection.send( "#{ msg }\r\n", 0 )
end

def login
irc_raw( "NICK #{ @nick }" )
irc_raw( "USER #{ @nick } #{ @nick } #{ @host } : #{ @nick } " )
end

def parse( line )
input = line.split(" ")
if input[0][0].chr == ":"
case input[1]
when "001"
@channels.each {|c| irc_join(c) }
when "433"
irc_nick( "#{ @nick }_" )
end
end
end

end

threads = []

server = TCPServer.new("0.0.0.0", 12121)

while (session = server.accept)
session_id, message = session.gets.split(";", 2)

t = threads.map {|t| t if t["session_id"] == session_id }.delete_if
{|s| s == nil}.first
puts t
if t
puts "Found thread with existing session_id: #{session_id}"
t.send :send_message, message
else
threads << Session.new("#{session_id};#{message}") do |t|
Thread.current["session_id"] = session_id
puts "Creating thread for #{ session_id }"
end
end
session.close
end


Francis Cianfrocca wrote:
> On 8/19/06, andreas.alin@gmail.com <andreas.alin@gmail.com> wrote:
> > Yow!
> > I'm trying to make a script that will act as a server (server 1), and
> > when a connection to the server server 1 is made, it will start a
> > thread that will connect to another server (server 2), and it will keep
> > the connection to server 2, even if the client disconnects.
> >
> > The thing is that, as I've tried, my client/server will not connect to
> > another server, and if it does, the client can not disconnect.
> >
> > Any ideas?
> >
> > Thanks,
> > Andreas
> >
> > ----------------------
> >
> > require 'socket'
> >
> > class Session < Thread
> > def initalize(session_id)
> > super
> > @session_id = session_id
> > end
> >
> > def session_id
> > @session_id
> > end
> >
> > def send_message( message
> > # This method should send a message to
> > # server 2
> > puts message
> > end
> > end
> >
> > threads = []
> >
> > server = TCPServer.new("0.0.0.0", 12121)
> >
> > while (session = server.accept)
> > session_id, message = session.gets.split(";", 2)
> >
> > t = threads.map {|t| t if t["session_id"] == session_id }.delete_if
> > {|s| s == nil}.first
> > puts t
> > if t
> > puts "Found thread with existing session_id: #{ session_id }"
> > t.send :send_message, message
> > else
> > threads << Session.new(session) do |t|
> > Thread.current["session_id"] = session_id
> > puts "Creating thread for #{ session_id }"
> > end
> > end
> > session.close
> > end
> >
> >
> >
>
>
> First, make sure your constructor is getting called. You spelled it
> "initalize" instead of "initialize"

Francis Cianfrocca

8/19/2006 3:39:00 PM

0

unknown wrote:
> Oh, thanks. :) Didn't see that. :)
>
> But, now, there are some problems. The program gets stuck in the loop
> on line 12, which I want the program to do in the background. (and it
> shouldn't get stuck) :)
>

Please forgive me, I don't intend to be rude, but this program is not
well thought out. You should reconsider your use of threads in the first
place. Try an implementation with fork until you get it right. (And fork
after you accept, not after you block on a gets from the accepted
socket.)

I think your program gets "stuck" because you're executing your loop in
a subclassed constructor. The new thread of control that you think
you're getting never actually starts running. To prove it, put a print
statement immediately after the accept, and then connect to your program
twice, from two different shells. I predict the second client will
appear to connect (because the TCP server picks it up in the kernel) but
your accept call will not execute more than once.

--
Posted via http://www.ruby-....

andreas.alin

8/19/2006 4:28:00 PM

0


Francis Cianfrocca wrote:
> unknown wrote:
> > Oh, thanks. :) Didn't see that. :)
> >
> > But, now, there are some problems. The program gets stuck in the loop
> > on line 12, which I want the program to do in the background. (and it
> > shouldn't get stuck) :)
> >
>
> Please forgive me, I don't intend to be rude, but this program is not
> well thought out. You should reconsider your use of threads in the first
> place. Try an implementation with fork until you get it right. (And fork
> after you accept, not after you block on a gets from the accepted
> socket.)
>
> I think your program gets "stuck" because you're executing your loop in
> a subclassed constructor. The new thread of control that you think
> you're getting never actually starts running. To prove it, put a print
> statement immediately after the accept, and then connect to your program
> twice, from two different shells. I predict the second client will
> appear to connect (because the TCP server picks it up in the kernel) but
> your accept call will not execute more than once.

Thanks for the advice, and for explaining :)
I'll take a look at fork and see if I can get this working :)

>
> --
> Posted via http://www.ruby-....

Ramon F Herrera

5/21/2013 2:43:00 PM

0

On May 21, 9:01 am, r...@somis.org (•RLMeasures) wrote:

> •  The problem in the Clinton Administration was suction, not
corruption.

"I have no idea how Monica was able to fit. Trust me: I have been down
there is there is not much space".

http://patriot.net/~ramon/misc/Not-Much-Room-for-Ma...

- John-John Kennedy

(real quote, maybe not verbatim)

Yoorghis

5/21/2013 4:20:00 PM

0

On Tue, 21 May 2013 07:01:54 -0700, r@somis.org (?RLMeasures) wrote:

>In article
><d82d382b-1615-453c-b108-767c00633ca6@v9g2000yqo.googlegroups.com>,
>Tracey12 <tracey12email@gmail.com> wrote:
>
>> On May 20, 10:22=A0pm, Yoorg...@Jurgis.net wrote:
>>
>> > You did it just 15 years ago $200,000,000 spent---and not one credible
>> > thing did you find

For the TraceyLoon:

>> If you lied to a federal grand jury what would be the penalty?

Well, Raygun got proven a perjuer. Clinton did not lie---and was
never charged or convicted of perjury. His denial of a "DEFINITION"
was accepted and he won the Jones Civil Suit

That same "denial" was reversed because he "settled" instead of
"re-winning" (which he would have). When he settled, the "denial"
became "inoperative" and therefore FORCED Judge Wright to FINE him.

Filing a "false declaration" is no a felony.
(and NOT an "impeachable offense like Raygun's subversion of govt and
Perjury)

>> If you were a lawyer and you lied to a federal grand jury what would
>> be the penalty?

He didn't. I just explained it.

>> If you were president of the United States of America and an attorney
>> and you lied to a federal grand jury what would be the penalty?

He didn't. So the Judge awarded him the Judgment in the Jones Civil
Action.

The testimony never changed, which shoots down your inept, ignorant
beliefs

>> Whatever we spent to remove Clinton corruption from the WH was worth
>> it.

Not one shred of credible, provable, sourced (first hand) evidence
EVER was produced to suggest Clinton was "corrupt"

What he was, However---was the most efficient righwing ass-kicker
since FDR and/or Kennedy

*************************************************************

>? The problem in the Clinton Administration was suction, not corruption.

"Problem"?

A consensual blowjob from a lady surely can't be problematic to you,
can it?

gazelle

5/21/2013 4:25:00 PM

0

In article <cq6np8p07qjnfjao4peeejl85v7asb16f1@4ax.com>,
<Yoorghis@Jurgis.net> wrote:
....
>*************************************************************
>
>>? The problem in the Clinton Administration was suction, not corruption.
>
>"Problem"?
>
>A consensual blowjob from a lady surely can't be problematic to you,
>can it?

As I often say, it doesn't matter to sophisticated and educated types like
you and me, but it matters a lot to the man-on-the-street. I.e., the
typical Republican base voter don't know much about the economy or
politics, but he knows what a BJ is.

--
One of the best lines I've heard lately:

Obama could cure cancer tomorrow, and the Republicans would be
complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it. We've constructed an economy in which eliminating
cancer would be a horrible disaster. There are many other such examples.)

BR549

5/21/2013 4:57:00 PM

0

On May 21, 11:24 am, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> In article <cq6np8p07qjnfjao4peeejl85v7asb1...@4ax.com>, <Yoorg...@Jurgis.net> wrote:
>
> ...
>
> >*************************************************************
>
> >>•  The problem in the Clinton Administration was suction, not corruption.
>
> >"Problem"?
>
> >A consensual blowjob from a lady surely can't be problematic to you,
> >can it?
>
> As I often say, it doesn't matter to sophisticated and educated types like
> you and me, but it matters a lot to the man-on-the-street.  I.e., the
> typical Republican base voter don't know much about the economy or
> politics, but he knows what a BJ is.
>
> --
> One of the best lines I've heard lately:
>
>     Obama could cure cancer tomorrow, and the Republicans would be
>     complaining that he had ruined the pharmaceutical business.
>
HUH ?? YOU MEAN HE CAN'T ??? OOOHHHHHH !! THE HUMANITY !!!!


> (Heard on Stephanie Miller = but the sad thing is that there is an awful lot
> of direct truth in it.  We've constructed an economy in which eliminating
> cancer would be a horrible disaster.  There are many other such examples.)

Smoking. If tobacco was as bad as liberal busy bodies say then why
isn' it banned from the U.S ? Wouldn't have anything to do with tax
revenue generated by the sale of tobacco products would it?

dzweibach

5/21/2013 7:08:00 PM

0

gazelle@shell.xmission.com (Kenny McCormack) wrote:
> In article <cq6np8p07qjnfjao4peeejl85v7asb16f1@4ax.com>,
> <Yoorghis@Jurgis.net> wrote:
> ...
> >*************************************************************
> >
> >>? The problem in the Clinton Administration was suction, not
> >>corruption.
> >
> >"Problem"?
> >
> >A consensual blowjob from a lady surely can't be problematic to you,
> >can it?
>
> As I often say, it doesn't matter to sophisticated and educated types
> like you and me, but it matters a lot to the man-on-the-street. I.e.,
> the typical Republican base voter don't know much about the economy or
> politics, but he knows what a BJ is.


"I hate to say this," said my attorney as we sat down at the Merry-Go-Round
Bar on the second balcony, "but this place is getting to me. I think I'm
getting the Fear."

r

5/21/2013 8:28:00 PM

0

In article <cq6np8p07qjnfjao4peeejl85v7asb16f1@4ax.com>,
Yoorghis@Jurgis.net wrote:

> On Tue, 21 May 2013 07:01:54 -0700, r@somis.org (?RLMeasures) wrote:
>
> >In article
> ><d82d382b-1615-453c-b108-767c00633ca6@v9g2000yqo.googlegroups.com>,
> >Tracey12 <tracey12email@gmail.com> wrote:
> >
> >> On May 20, 10:22=A0pm, Yoorg...@Jurgis.net wrote:
> >>
> >> > You did it just 15 years ago $200,000,000 spent---and not one credible
> >> > thing did you find
>
> For the TraceyLoon:
>
> >> If you lied to a federal grand jury what would be the penalty?
>
> Well, Raygun got proven a perjuer. Clinton did not lie---and was
> never charged or convicted of perjury. His denial of a "DEFINITION"
> was accepted and he won the Jones Civil Suit

? In hindsight Bill Clinton would have fared better to have instead said
I did not have Coitus with that woman, Monica Lewinsky since Bill's semen
deposit on the blue dress proves it beyond a shadow of doubt.
>
> That same "denial" was reversed because he "settled" instead of
> "re-winning" (which he would have). When he settled, the "denial"
> became "inoperative" and therefore FORCED Judge Wright to FINE him.
>
> Filing a "false declaration" is no a felony.
> (and NOT an "impeachable offense like Raygun's subversion of govt and
> Perjury)
>
> >> If you were a lawyer and you lied to a federal grand jury what would
> >> be the penalty?
>
> He didn't. I just explained it.
>
> >> If you were president of the United States of America and an attorney
> >> and you lied to a federal grand jury what would be the penalty?
>
> He didn't. So the Judge awarded him the Judgment in the Jones Civil
> Action.
>
> The testimony never changed, which shoots down your inept, ignorant
> beliefs
>
> >> Whatever we spent to remove Clinton corruption from the WH was worth
> >> it.
>
> Not one shred of credible, provable, sourced (first hand) evidence
> EVER was produced to suggest Clinton was "corrupt"
>
> What he was, However---was the most efficient righwing ass-kicker
> since FDR and/or Kennedy
>
> *************************************************************
>
> >? The problem in the Clinton Administration was suction, not corruption.
>
> "Problem"?
>
> A consensual blowjob from a lady surely can't be problematic to you,
> can it?