Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Using Sockets

by Dog and Pony (Priest)
on Jun 08, 2003 at 10:04 UTC ( #264083=note: print w/ replies, xml ) Need Help??


in reply to Using Sockets

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.


Comment on Re: Using Sockets
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://264083]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-09-22 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (188 votes), past polls