Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Using Sockets

by Flame (Deacon)
on Jun 08, 2003 at 04:55 UTC ( #264067=perlquestion: print w/replies, xml ) Need Help??
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 => '', 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)

Replies are listed 'Best First'.
Re: Using Sockets
by Dog and Pony (Priest) on Jun 08, 2003 at 10:04 UTC
    I can't help but thinking that you are overdoing it a bit. Here is some very simplistic code for a listen server that works with your sender (if you remove the chomp):
    #!/usr/bin/perl -w use strict; use IO::Socket; my $listener = IO::Socket::INET->new ( Listen => 5, LocalAddr => 'localhost', LocalPort => 1001, Proto => 'tcp' ); if(defined(my $connection = $listener->accept)) { print "New connection\n"; while(my $line = $connection->getline()) { last if($line =~ /^quit/); print $line; } $connection->close; undef $connection; }
    I copy/pasted this from some other code I've written, so maybe there are some logical mistakes, but I've tried to run it at least. All it does is to create a socket and then it listens for a connection - this version only accepts one connection, you would probably like to spawn some processing threads and go back to listening if you want to accept several connections.

    Then, when it gets a connection it starts to read lines from the sender - that is why the chomp must die, so newlines gets sent along with the rest. If the line starts with "quit", it exits, otherwise it prints the line received. I added the "quit" because these kinds of programs tend to be hard to get out of in Windows, unless one uses the Task Manager.

    I am still not vertain why exactly your program does what it does (and it does it here too), because I didn't look to hard. I thought I'd rather provide something really basic to start from, so you could build upon that. I've never used IO::Select and friends for this stuff, so I don't think that should be in there at all. But there may be a reason I am missing, since I haven't used it myself. :)

    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Using Sockets
by Thelonius (Priest) on Jun 08, 2003 at 14:29 UTC
    The problem is here:
    local $/; my $data = <$fh>;
    This slurp mode means that the read won't return until end of file on $fh. If you want to read everything that's available, use read or sysread. When read or sysread returns false (0 or undef), you have reached end of file (i.e. socket close).
Re: Using Sockets (what are sockets)
by ybiC (Prior) on Jun 08, 2003 at 17:53 UTC
    As for the implied question, "what are sockets", you might find RFC 147 and port numbers to be of some value.

    Coming from a networking background (as opposed to a programming background), I'd describe a socket as the combination of both an IP address and a port number, used to uniquely identify a specific network application for remote connections.   This applies to both client and server applications.

    Anymonk what can articulate it better/more_clearly, please expand on or correct my hipshoot description.
      striving toward Perl Adept
      (it's pronounced "why-bick")
Re: Using Sockets
by vek (Prior) on Jun 08, 2003 at 18:05 UTC

    As a side note I thought I'd point out a resource that has certainly helped me grok certain socket concepts. Lincoln Stein's Network Programming With Perl is a really good read and I thoroughly recommend it.

    -- vek --

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://264067]
Approved by dws
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-02-26 00:11 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (369 votes). Check out past polls.