Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sockets and threads...

Reply
Thread Tools

Sockets and threads...

 
 
Mark Seger
Guest
Posts: n/a
 
      11-05-2008
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
 
Reply With Quote
 
 
 
 
zentara
Guest
Posts: n/a
 
      11-05-2008
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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      11-05-2008
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.
 
Reply With Quote
 
Mark Seger
Guest
Posts: n/a
 
      11-08-2008

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sockets and pipes and bad results Marilyn Davis Python 0 09-25-2004 01:01 AM
how to communicate server/client using tcpip and /or sockets =?Utf-8?B?U3llZCBBdW4gQWxp?= ASP .Net 1 08-07-2004 08:27 AM
Carriage Returns and sockets Iceberg Perl 1 09-06-2003 04:49 PM
java.net.sockets and MS Proxy Jack Vamvas Java 1 07-14-2003 05:50 PM
Java and Unix Domain sockets Stuart Clayman Java 2 07-10-2003 04:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57