Sockets and threads...

Discussion in 'Perl Misc' started by Mark Seger, Nov 5, 2008.

  1. Mark Seger

    Mark Seger Guest

    I want to open some sockets in one thread and write to them from my main
    process and think this is very doable. I've been looking around a lot
    for answers to this and I think I've figure it out but would like to
    hear if my thinking is indeed correct:

    Sockets are global to the process/threads already and so you don't have
    to pass around file descriptors to make them visible to the main
    process. I had tried (and failed) to share descriptors and failed
    because they're not simple variables.

    Looking at some code snippets it feels like all I need to do is get the
    file number from $descriptor->fileno() and share that.

    One way to convert a file number, assuming it's in $fn back to a
    descriptor is the following:

    open ($fd, ">&$fn);

    and now I can syswrite() to it back in my main code. At least it seems
    to be working correctly. Is this indeed the 'preferred' way to do this?
    Am I missing something? Is there a better way?

    -mark
    Mark Seger, Nov 5, 2008
    #1
    1. Advertising

  2. Mark Seger

    zentara Guest

    On Wed, 05 Nov 2008 08:38:45 -0500, Mark Seger <>
    wrote:

    >I want to open some sockets in one thread and write to them from my main
    >process and think this is very doable. I've been looking around a lot
    >for answers to this and I think I've figure it out but would like to
    >hear if my thinking is indeed correct:
    >
    >Sockets are global to the process/threads already and so you don't have
    >to pass around file descriptors to make them visible to the main
    >process. I had tried (and failed) to share descriptors and failed
    >because they're not simple variables.
    >
    >Looking at some code snippets it feels like all I need to do is get the
    >file number from $descriptor->fileno() and share that.
    >
    >One way to convert a file number, assuming it's in $fn back to a
    >descriptor is the following:
    >
    >open ($fd, ">&$fn);
    >
    >and now I can syswrite() to it back in my main code. At least it seems
    >to be working correctly. Is this indeed the 'preferred' way to do this?
    > Am I missing something? Is there a better way?
    >
    >-mark


    Sharing filhandles thru the fileno is the way to go with threads.

    See http://perlmonks.org?node_id=662931

    for some ideas.

    zentara
    zentara, Nov 5, 2008
    #2
    1. Advertising

  3. Mark Seger

    Guest

    Mark Seger <> wrote:
    > I want to open some sockets in one thread and write to them from my main
    > process and think this is very doable.


    Open a socket to what? Are you opening sockets "internally" from one
    thread to another within the same process? Is the "open" doing the accept,
    or the connect?

    > I've been looking around a lot
    > for answers to this and I think I've figure it out but would like to
    > hear if my thinking is indeed correct:
    >
    > Sockets are global to the process/threads already and so you don't have
    > to pass around file descriptors to make them visible to the main
    > process. I had tried (and failed) to share descriptors and failed
    > because they're not simple variables.


    Can you show code to illustrate this problem?

    > Looking at some code snippets it feels like all I need to do is get the
    > file number from $descriptor->fileno() and share that.
    >
    > One way to convert a file number, assuming it's in $fn back to a
    > descriptor is the following:
    >
    > open ($fd, ">&$fn);


    That only works if you only need to write on the socket. I can't
    figure out how to open if you need both read and write.

    > and now I can syswrite() to it back in my main code. At least it seems
    > to be working correctly. Is this indeed the 'preferred' way to do this?
    > Am I missing something? Is there a better way?


    In most cases I can dream up in which I might want to use this, I suspect
    I'd prefer to arrange the code differently in the first place.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Nov 5, 2008
    #3
  4. Mark Seger

    Mark Seger Guest


    > Sharing filhandles thru the fileno is the way to go with threads.

    yes, clearly the way to go! I now have a thread that listens for
    connections, accepts them and writes the fileno into a share array.
    -mark
    Mark Seger, Nov 8, 2008
    #4
    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. Jupiter5F

    Threads and Sockets

    Jupiter5F, Nov 14, 2003, in forum: C++
    Replies:
    4
    Views:
    2,308
    Jupiter5F
    Nov 14, 2003
  2. Julia Goolia

    tkinter, sockets and threads together

    Julia Goolia, Sep 11, 2003, in forum: Python
    Replies:
    4
    Views:
    535
    Steve Holden
    Sep 12, 2003
  3. Gonçalo Rodrigues

    Help needed in problem with Threads and sockets.

    Gonçalo Rodrigues, Dec 29, 2003, in forum: Python
    Replies:
    0
    Views:
    273
    Gonçalo Rodrigues
    Dec 29, 2003
  4. Rod Stephenson
    Replies:
    0
    Views:
    444
    Rod Stephenson
    Jun 22, 2004
  5. Ajay

    threads and sockets

    Ajay, Oct 13, 2004, in forum: Python
    Replies:
    3
    Views:
    292
    Elbert Lev
    Oct 14, 2004
Loading...

Share This Page