help need in chat client/server

Discussion in 'Ruby' started by HadsS, Apr 14, 2008.

  1. HadsS

    HadsS Guest

    Hey...
    Im new to this network programmin and I need help with this code i
    copied and changed a little.
    this is a client/server chat..
    -----------------------
    Server code:

    require "socket"
    PORT = 12321
    server = TCPServer.new(PORT)
    flag=true
    while (session = server.accept)
    while flag==true
    print "Sending:"
    line=gets.chomp!
    session.puts line
    end
    end
    ----------------

    Client Code:
    require "socket"

    PORT = 12321
    HOST = ARGV[0] || "localhost"
    flag=true
    session = TCPSocket.new(HOST, PORT)
    while flag==true
    line = session.gets
    puts "Recived:#{line} "
    end
    ---------

    i dont know what i did but its sort of doing a one way communication..
    can someone please help me with creating two clients and making them
    chat 2way amongst them..
    please.. i would really be thankfulll
    waiting for a reply
     
    HadsS, Apr 14, 2008
    #1
    1. Advertising

  2. HadsS

    HadsS Guest

    PLEASE I NEED HELP
     
    HadsS, Apr 14, 2008
    #2
    1. Advertising

  3. HadsS

    Glen Holcomb Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Apr 14, 2008 at 11:25 AM, HadsS <> wrote:

    > Hey...
    > Im new to this network programmin and I need help with this code i
    > copied and changed a little.
    > this is a client/server chat..
    > -----------------------
    > Server code:
    >
    > require "socket"
    > PORT = 12321
    > server = TCPServer.new(PORT)
    > flag=true
    > while (session = server.accept)
    > while flag==true
    > print "Sending:"
    > line=gets.chomp!
    > session.puts line
    > end
    > end
    > ----------------
    >
    > Client Code:
    > require "socket"
    >
    > PORT = 12321
    > HOST = ARGV[0] || "localhost"
    > flag=true
    > session = TCPSocket.new(HOST, PORT)
    > while flag==true
    > line = session.gets
    > puts "Recived:#{line} "
    > end
    > ---------
    >
    > i dont know what i did but its sort of doing a one way communication..
    > can someone please help me with creating two clients and making them
    > chat 2way amongst them..
    > please.. i would really be thankfulll
    > waiting for a reply
    >
    >

    There are a few logical/structural problems here:

    Firstly, it is only one way because one end is sending and the other
    listening but the sending end never listens and the listening end never
    sends.

    Secondly, control in this is going to be painful as it is. You probably
    want to thread your program so that each end can "talk" and "listen" at the
    same time. You could make it clunky and just switch between the two but
    that would be rather un-intuative.

    I would offer up some sample code but haven't done any socket programming in
    Ruby and am currently at work I bet someone will come along with more
    useful info though (I offer this as something to get you going in the right
    direction until then).

    --
    "Hey brother Christian with your high and mighty errand, Your actions speak
    so loud, I can't hear a word you're saying."

    -Greg Graffin (Bad Religion)
     
    Glen Holcomb, Apr 14, 2008
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    Hi HadsS,

    There is some example code in Peter Coopers book (Beginning Ruby From Novice
    to Professional):

    http://www.springerlink.com/content/ph5611n3q3uu0619/

    Not sure you'll have access to it. I can forward you the chapter if you
    like.

    I am working on some network programming in ruby as well. Maybe we can
    collaborate a little. It's not the easiest thing to grasp :(

    Vince

    On Mon, Apr 14, 2008 at 2:13 PM, Glen Holcomb <> wrote:

    > On Mon, Apr 14, 2008 at 11:25 AM, HadsS <> wrote:
    >
    > > Hey...
    > > Im new to this network programmin and I need help with this code i
    > > copied and changed a little.
    > > this is a client/server chat..
    > > -----------------------
    > > Server code:
    > >
    > > require "socket"
    > > PORT = 12321
    > > server = TCPServer.new(PORT)
    > > flag=true
    > > while (session = server.accept)
    > > while flag==true
    > > print "Sending:"
    > > line=gets.chomp!
    > > session.puts line
    > > end
    > > end
    > > ----------------
    > >
    > > Client Code:
    > > require "socket"
    > >
    > > PORT = 12321
    > > HOST = ARGV[0] || "localhost"
    > > flag=true
    > > session = TCPSocket.new(HOST, PORT)
    > > while flag==true
    > > line = session.gets
    > > puts "Recived:#{line} "
    > > end
    > > ---------
    > >
    > > i dont know what i did but its sort of doing a one way communication..
    > > can someone please help me with creating two clients and making them
    > > chat 2way amongst them..
    > > please.. i would really be thankfulll
    > > waiting for a reply
    > >
    > >

    > There are a few logical/structural problems here:
    >
    > Firstly, it is only one way because one end is sending and the other
    > listening but the sending end never listens and the listening end never
    > sends.
    >
    > Secondly, control in this is going to be painful as it is. You probably
    > want to thread your program so that each end can "talk" and "listen" at
    > the
    > same time. You could make it clunky and just switch between the two but
    > that would be rather un-intuative.
    >
    > I would offer up some sample code but haven't done any socket programming
    > in
    > Ruby and am currently at work I bet someone will come along with more
    > useful info though (I offer this as something to get you going in the
    > right
    > direction until then).
    >
    > --
    > "Hey brother Christian with your high and mighty errand, Your actions
    > speak
    > so loud, I can't hear a word you're saying."
    >
    > -Greg Graffin (Bad Religion)
    >
     
    Vince Forgetta, Apr 14, 2008
    #4
  5. Siep Korteling, Apr 14, 2008
    #5
  6. On 14 Apr 2008, at 18:25, HadsS wrote:
    > Hey...
    > Im new to this network programmin and I need help with this code i


    Visit the URL in my sig and download the Anchoring Trust presentation:
    it has examples of writing all TCP and UDP client and server systems
    along with some explanatory notes. There are probably bugs but along
    with what you've already got it should be enough to teach you all the
    basics of Ruby network programming.

    Ignore all the other bits in there though - it'll only confuse
    matters...


    Ellie

    Eleanor McHugh
    Games With Brains
    http://slides.games-with-brains.net
    ----
    raise ArgumentError unless @reality.responds_to? :reason
     
    Eleanor McHugh, Apr 15, 2008
    #6
  7. HadsS

    HadsS Guest

    Can someone please help me with threading.. i read about it but i dont
    really get it..
    Glen suggested it to use it to built two way communication..
    If i culd get some code it would be reallly helpfull...
     
    HadsS, Apr 15, 2008
    #7
  8. HadsS

    Jano Svitok Guest

    On Mon, Apr 14, 2008 at 7:25 PM, HadsS <> wrote:
    > Hey...
    > Im new to this network programmin and I need help with this code i
    > copied and changed a little.
    > this is a client/server chat..
    > ...


    Did you try EventMachine? Once I wrote a TCP echo server under 30 minutes
    (without any previous knowledge of EM, and this includes installation
    of EM and reading its docs). Really nice.

    J.
     
    Jano Svitok, Apr 15, 2008
    #8
  9. HadsS

    Glen Holcomb Guest

    [Note: parts of this message were removed to make it a legal post.]

    HadsS,

    Here are a couple of pieces on threading in Ruby:

    http://www.rubycentral.com/pickaxe/tut_threads.html
    http://corelib.rubyonrails.org/classes/Thread.html

    Hopefully this will help if you want to go in the thread direction, it is
    what I would do but I come from a C background.

    On Tue, Apr 15, 2008 at 5:52 AM, Jano Svitok <> wrote:

    > On Mon, Apr 14, 2008 at 7:25 PM, HadsS <> wrote:
    > > Hey...
    > > Im new to this network programmin and I need help with this code i
    > > copied and changed a little.
    > > this is a client/server chat..
    > > ...

    >
    > Did you try EventMachine? Once I wrote a TCP echo server under 30 minutes
    > (without any previous knowledge of EM, and this includes installation
    > of EM and reading its docs). Really nice.
    >
    > J.
    >
    >



    --
    "Hey brother Christian with your high and mighty errand, Your actions speak
    so loud, I can't hear a word you're saying."

    -Greg Graffin (Bad Religion)
     
    Glen Holcomb, Apr 15, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,236
  2. Jaspreet
    Replies:
    0
    Views:
    704
    Jaspreet
    Sep 18, 2004
  3. Marek Zawadzki
    Replies:
    1
    Views:
    1,190
    Ashtar
    Jul 28, 2006
  4. Dhiraj Girdhar

    Chat server telnet client problem

    Dhiraj Girdhar, Jul 5, 2007, in forum: Ruby
    Replies:
    0
    Views:
    169
    Dhiraj Girdhar
    Jul 5, 2007
  5. deadpickle

    Chat client/server print failed

    deadpickle, Jan 16, 2008, in forum: Perl Misc
    Replies:
    14
    Views:
    340
    zentara
    Jan 27, 2008
Loading...

Share This Page