Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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 => '192.168.1.10', PeerPort => 1001, Proto => 'tcp', #Broadcast => 1, #LocalAddr => '192.168.1.11', ); 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:
Hi
Out
There
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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2014-08-21 08:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (128 votes), past polls