I have a thread that manages sockets by listening for connections and then opening/closing them as appropriate. I'm doing my actual communications in the main script by using the technique mentioned in several monk postings on how to share sockets, namely sharing the file number rather than the socket handle itself and doing an open in the main line using that number as in

open ($fd, ">&$fnum") or die;

$fd is now a descriptor that I can write to and it works like a champ. Sort of, and hence my question. I'm hoping I just have the open syntax wrong in which case the fix might be a simple one.

First I run the script and monitor the open sockets with netstat. Then I run a test script that simply connects to that socket and does a sysread, prints the results, closes the connection and exits.

Netstat shows the initial connection and then the socket goes into FIN_WAIT2 and hangs around for quite awhile or until I close my main script. On the other hand if I run the code non-threaded and do all the communications inline, I see the socket go to FIN_WAIT2 and then fairly quickly goes away.

My guess is that this is when the socket is closed by the test script, that close is detected and acknowledged by the thread where it was created, but copy of the socket that was created with the open() is not completing the communications handshake and I don't know enough about these mechanisms to know what to do.

Any help will be greatly appreciated.

btw - I also tried using the open technique in some non-threaded code and it too works correctly but leaves a connection in FIN_WAIT2 as well, so I'm guessing this has nothing to do with threads and more to do with having to close that second handle.

-mark


In reply to Sharing sockets between the main script and thread by markseger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.