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

Re: Using Sockets

by Dog and Pony (Priest)
on Jun 08, 2003 at 10:04 UTC ( [id://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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2024-04-19 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found