Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Using Sockets by Dog and Pony
in thread Using Sockets by Flame

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-18 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found