Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Fellow monks, I've recently taken an interest in sockets, but during my investigation, I find myself running into one rather simple problem... I don't really know what they are.

Based upon the examples in IO::Socket and IO::Select, I've pieced together enough to say that I have a vague idea how things SHOULD work... but thinking you know how it should work, doesn't mean when you try to use it that way, it will work. So to start with, can anyone point me toward some useful information on sockets?

As an experement, I wrote this, and I am definately not getting the results I expected out of it:

#!/usr/bin/perl #use threads; #use threads::shared; #use DBI; use IO::Socket::INET; use IO::Select; use constant { PORT => 1001, }; use strict; my $lsn = IO::Socket::INET->new( LocalPort => PORT, Listen => 1, Proto => 'tcp', ); print 'Waiting for connections on '.PORT."\n\n"; my $sel = IO::Select->new($lsn); my @users; while(my @available = $sel->can_read){ foreach my $fh (@available){ if($lsn == $fh){ #The listen socket has a connection my $new = $fh->accept; print 'Connection accepted from'.$fh->connected."\n"; $sel->add($new); #push(@users,$new); }else{ local $/; my $data = <$fh>; if($fh->connected){ print $fh->connected.' = '.$data."\n"; }else{ $sel->remove($fh); $fh->close; } } } }

After running this one one system, I run the following on another, those with a clue about this stuff probably can already see some of my problem:

#!/usr/bin/perl use IO::Socket::INET; use strict; my $sock = IO::Socket::INET->new( PeerAddr => '', PeerPort => 1001, Proto => 'tcp', #Broadcast => 1, #LocalAddr => '', ); while(chomp(my $txt = <>)){ print $sock $txt; $sock->flush; }

when I run both of them, I get the "Connection Accepted from " message (though nothing follows it, can you retrieve the ip? I've been playing guess and check looking for that...) In the client window, I type something at random such as:
All the while, nothing is happening on the server, then I force-quit the client and I get something like this:

 + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =  + =

My first problem, is detecting disconnects, as can be seen by the long set of nothing messages (I assume what I actually typed appeared, but was forced off the screen too quickly.) Second is getting the message to actually be sent before closing the client. Third is seeing if I can figure out an IP address. Now, as I mentioned, I'm pretty clueless, so please don't simply give the answer to the question, if you can, please explain why it's the answer.

Thank you.

My code doesn't have bugs, it just develops random features.

Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

In reply to Using Sockets by Flame

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2018-06-19 22:17 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (115 votes). Check out past polls.