Gorby has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks,

Why doesn't this code work? All it does is receive the data from the client but it doesn't send back data to the client:

$server = IO::Socket::INET->new ( LocalPort => 8001, Type => SOCK_STREAM, Reuse => 1, Listen => 5 ); while ($client = $server->accept()) { $line=<$client>; print "$line\n"; print $client "Hello there"; }


Thanks in advance!

Gorby

Replies are listed 'Best First'.
Re: Socket Server without forking?
by fmerges (Chaplain) on Sep 14, 2007 at 08:10 UTC

    Hi,

    Because for sending you forgot the \r\n, or simply \n.

    print $client "Hello there\n";

    Regards,

    fmerges at irc.freenode.net
      still doesn't work
        This worked! Now my question is, what text do i send back to the client so that if the client is a web browser, it will be able to display what i send back? Thanks in advance for your wisdom!
Re: Socket Server without forking?
by salva (Canon) on Sep 14, 2007 at 08:50 UTC
    Because you are using buffered IO.

    Use sysread and syswrite to "talk" through the socket instead.

      This worked! Now my question is, what text do i send back to the client so that if the client is a web browser, it will be able to display what i send back? Thanks in advance for your wisdom!
Re: Socket Server without forking?
by dk (Chaplain) on Sep 14, 2007 at 09:13 UTC
    Works for me, actually:

    $ nc localhost 8001
    hi
    Hello there

Re: Socket Server without forking?
by suaveant (Parson) on Sep 14, 2007 at 14:47 UTC
    moritz rightly suggests you should have some sort of web server...

    If you are looking for a non-forking solution that feeds back web pages I would definately suggest looking at POE, which is very well suited to that, and has a component web server POE::Component::Server::HTTP

                    - Ant
                    - Some of my best work - (1 2 3)