http://qs321.pair.com?node_id=264067

Flame has asked for the wisdom of the Perl Monks concerning the following question:

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)