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

Most probably, this is a FAQ or RTFM like question. Unfortunately, I did RTFM and I did Super Search but I did not found similar problems.

I'm trying to write a small pop3 proxy server which needs to take away spam mail between the real pop3 server and my mail client.
To achieve this, I'm now writing just a little test server, which I connect to using just a telnet connection. I can send things to the server but the server seems unable to talk back.

ActivePerl 5.6.1 on Windows 98 Second Edition



Update: Oh yeah, there's no firewall blocking server response, because I've written a similar server program in Visual Basic and that one works.

The code:
# just the basics use strict; use warnings; use Socket; # start up the server socket (SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')); setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1); # My book says to use inet_aton($host) # that would be inet_aton('localhost') # but inet_aton seems to return v127.0.0.1 so I prefer to use that my $addr = sockaddr_in(110, v127.0.0.1); bind(SERVER, $addr) or die "Could not bind to port"; listen(SERVER, SOMAXCONN) or die "Could nit listen to port"; my $lines = 0; # how many lines are sent by the client my $line; # current line accept(CLIENT, SERVER); # just to be sure: binmode CLIENT; binmode SERVER; print "Nieuwe verbinding...\n"; # Dutch for 'new connection' # new connection? Send something to client print CLIENT "+OK\n"; # ... code goes on, but the above line seems never sended to the clien +t.

Replies are listed 'Best First'.
Re: `print CLIENT` seems to send nothing with `use Socket`
by matija (Priest) on Apr 03, 2004 at 16:31 UTC
    Two things occur to me:
    1. You might be suffering from buffering - did you try to do select CLIENT; $|=1;?
    2. I always feel nervous writing to pipes with print - my C background insists that one should write to pipes with the closest-to-the-metal functions available, so I always used syswrite, as in:
      $msg="+OK\n"; syswrite(CLIENT,$msg);
      In addition, syswrite will return the number of bytes actually written, which should help you with your debugging.
      Doh! I should have known about $|=1. I just saw your words 'suffering from buffering' and I thought 'maybe I should use $|'... that did the job!

      Thank you!

      You see... it is a FAQ-related question :)

Re: `print CLIENT` seems to send nothing with `use Socket`
by Abigail-II (Bishop) on Apr 03, 2004 at 15:39 UTC
    The code runs fine for me, using Linux. It prints "Nieuwe verbinding..." on STDOUT, and "+OK" to a client connecting to the port.

    Abigail

      Hmm... too strange... and you didn't modify the code in one way or another? I just c-a-n-'-t get it to work. I even tried to 'print print CLIENT "+OK\n";' to check whether the print works. Terribly enough, it's output was '1'. So the 'print CLIENT' should work fine although it doesn't.
        The only thing I changed was the port number, from 110 to 1100 because I was running it as a non-root user.

        Abigail