0

I am trying to write a simple IO::Socket connection in perl. However, I am running into some problems. Here is the code on the server side:

    my $listener = 
      IO::Socket::INET->new( LocalPort => 8000, Listen => 1, Reuse => 1 );

    die "Can't create socket for listening: $!" unless $listener;
    print "Listening for connections on port 8000\n";

    while(1) {
      while ($client = $listener->accept()) {
        while ( <$client>) {
            my @arguments = split(/ /, $_ );
            my $result = "something" ;# here we do something in my code
            warn $result;
            print $client $result;
            close $client;
        }   
      }
    }

And the client code:

use IO::Socket;
my $sock = new IO::Socket::INET (
        PeerAddr => 'xx.xxx.xxx.xxx',
        PeerPort => '8000',
        Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sock;
$sock->autoflush(1);
print $sock "somethin something";
print "sent\n";
while ( <$sock> ) { print }
close $sock;

My problem now is that the data seems to be only sent from the client to teh sever when I close the client Perl program. I get the "sent" message on the client side, but the "something" message on the server side does not appear until after I have manually closed the client side.

Also, I want to get the server response. Thus far, since I have to close the script manually, the response does not et to the client side.

Can anyone help?

1 Answer 1

1

while ( <$sock> )
-- waits for a line. That is for a string, ended by "\n" character.

You must add "\n" to strings, or use 'read' function instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that works! I can't believe I didn't find that information before... Must have missed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.