in reply to Using Sockets
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.#!/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; }
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.
|
---|