Socket Progamming - Beginner questions

Discussion in 'Ruby' started by Gin Mendi, Nov 4, 2008.

  1. Gin Mendi

    Gin Mendi Guest

    Hi,

    I'm trying to learn Ruby and Socket programming. So far I managed to
    find some tutorials about Ruby and Socket programming and have made
    really simple applications like a Time server sending the time to a
    client. Now I wanted to try something a little more advanced. I want to
    make a client that will connect to server and receive messages and at
    the same time send messages to the server when certain events happen
    like when there's a file in a certain directory or a record in the
    database.

    So far from the tutorials I've studied, the clients all just wait for a
    response from the server. From what I understand once I do a gets I'll
    just be waiting for a reponse and will put the rest of the state waiting
    on that response. Is it possible to do both reading and writing on the
    socket at the same time?

    I also did some digging on threads. I had the idea of making 2 threads
    one to receive messages from the server and one to check the
    directory/database then send a message. Again I don't see though how I
    can do that based on what I done in the tutorials. It seems like my
    threads will just hang when I try to read from the server. Is this the
    proper approach? Are there other methods aside from using threads? I saw
    topic on a multiplexing server but all the examples were just waiting
    again on a read from the client.

    I know these are pretty broad questions and topics but hoping someone
    can help clarify these issues and point me at the right direction.

    Thanks!
    --
    Posted via http://www.ruby-forum.com/.
     
    Gin Mendi, Nov 4, 2008
    #1
    1. Advertising

  2. Gin Mendi

    Adam Bender Guest

    Threads can work, but most networking code I've written uses
    Kernel#select. It's an incredibly powerful, yet somewhat hard to
    master, method that allows you to read and write on many sockets
    (actually IO objects) at the same time. It sounds like you want two
    read objects, one for the server and one for the database, and that
    you want to write to the network socket (yes, can you can read and
    write to the same socket - just use "puts" as well as "gets", or
    whatever equivalent methods you like) in the part of the code that
    handles what happens when the database socket is ready to be read.

    http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.select

    HTH,

    Adam

    On Tue, Nov 4, 2008 at 1:49 AM, Gin Mendi <> wrote:
    > Hi,
    >
    > I'm trying to learn Ruby and Socket programming. So far I managed to
    > find some tutorials about Ruby and Socket programming and have made
    > really simple applications like a Time server sending the time to a
    > client. Now I wanted to try something a little more advanced. I want to
    > make a client that will connect to server and receive messages and at
    > the same time send messages to the server when certain events happen
    > like when there's a file in a certain directory or a record in the
    > database.
    >
    > So far from the tutorials I've studied, the clients all just wait for a
    > response from the server. From what I understand once I do a gets I'll
    > just be waiting for a reponse and will put the rest of the state waiting
    > on that response. Is it possible to do both reading and writing on the
    > socket at the same time?
    >
    > I also did some digging on threads. I had the idea of making 2 threads
    > one to receive messages from the server and one to check the
    > directory/database then send a message. Again I don't see though how I
    > can do that based on what I done in the tutorials. It seems like my
    > threads will just hang when I try to read from the server. Is this the
    > proper approach? Are there other methods aside from using threads? I saw
    > topic on a multiplexing server but all the examples were just waiting
    > again on a read from the client.
    >
    > I know these are pretty broad questions and topics but hoping someone
    > can help clarify these issues and point me at the right direction.
    >
    > Thanks!
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Adam Bender, Nov 8, 2008
    #2
    1. Advertising

  3. Gin Mendi

    Roger Pack Guest

    Roger Pack, Nov 8, 2008
    #3
  4. Gin Mendi

    Gin Mendi Guest

    Adam Bender wrote:
    > Threads can work, but most networking code I've written uses
    > Kernel#select. It's an incredibly powerful, yet somewhat hard to
    > master, method that allows you to read and write on many sockets
    > (actually IO objects) at the same time. It sounds like you want two
    > read objects, one for the server and one for the database, and that
    > you want to write to the network socket (yes, can you can read and
    > write to the same socket - just use "puts" as well as "gets", or
    > whatever equivalent methods you like) in the part of the code that
    > handles what happens when the database socket is ready to be read.
    >
    > http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.select
    >
    > HTH,
    >
    > Adam


    Thanks Adam!

    I looked for examples using select and I mostly found chat servers. I
    tried it out and it does look like an effective and powerful approach. I
    still though don't understand how to trigger a read from another socket
    if I want to monitor a directory or a database. I wish though I could
    find better examples in the internet. How would you set up a socket to
    monitor a directory or database or is my understanding of the matter
    off?

    I checked out EventMachine as well which looks great just that since I
    want to learn I want to try out making something from scratch.


    --
    Posted via http://www.ruby-forum.com/.
     
    Gin Mendi, Nov 10, 2008
    #4
  5. Gin Mendi

    Jeff Moore Guest

    > How would you set up a socket to
    > monitor a directory or database or is my understanding of the matter
    > off?
    >


    Sockets are just a conduit for data. You can use their blocking
    capability (via select, etc) to control some portion of the interaction
    between the client and server (or whatever relationship you deem
    appropriate between the end points).

    Regardless, you still require an active process/thread to 'put' the data
    into one end of the socket and another to active process/thread to 'get'
    it at the other end.

    It's much easier if you maintain the separation of client/network/server
    activities in your mind and in the code.

    Another consideration is that this a glorious intersection of things
    (both threads and sockets) that are similar across multiple platforms
    but only similar. Each platform has it's own features (quirks).

    The source code for webrick and the net library in lib provide plenty of
    examples of how threads and sockets interact (or not) in an internet
    application.

    It's one of the beauties of Ruby. You get the code.

    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Moore, Nov 10, 2008
    #5
  6. Gin Mendi

    Gin Mendi Guest

    Thanks Jeff! I'll check out the code and study them. Hopefully a novice
    like me can pick it up quickly.
    --
    Posted via http://www.ruby-forum.com/.
     
    Gin Mendi, Nov 13, 2008
    #6
    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. Apollo

    Python progamming certification

    Apollo, Dec 9, 2003, in forum: Python
    Replies:
    15
    Views:
    3,895
    Apollo
    Dec 13, 2003
  2. Rony Steelandt

    Progamming python without a keyboard

    Rony Steelandt, May 4, 2006, in forum: Python
    Replies:
    3
    Views:
    320
    Benji York
    May 5, 2006
  3. David Shaw

    API Progamming

    David Shaw, Jul 13, 2005, in forum: C++
    Replies:
    3
    Views:
    305
    Serge Paccalin
    Jul 13, 2005
  4. CR
    Replies:
    5
    Views:
    371
    Greg Comeau
    Nov 11, 2005
  5. Concepts Systems
    Replies:
    0
    Views:
    293
    Concepts Systems
    Jul 1, 2008
Loading...

Share This Page