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

Hi, I have an old and normally very reliable piece of code that sends mail via smtp. Currently a customer is having trouble with it: after it connects to the socket, it hangs while trying to read the first line. Using ethereal, I can see that the mail server is indeed sending me the first line, "220 ... \r\n". Connecting manually with telnet to the same server/port works perfectly, and the beginning ethereal output for the telnet connection looks the same as the perl script session to me.

I'm at a loss as to what could cause this. If it's a firewall or something like that, I think it would block the whole connection. I think a firewall would also block my manual telnet test.

I have two questions, really:
1. Can anyone think of a cause or explanation for this?
2. Do you think that rewriting my code to use IO::Socket might help?

This is a stripped down version of my code to illustrate what it does:

my $smtp = '167.206.5.250'; # part 1, connect to server: use Socket; my $proto = (getprotobyname('tcp'))[2]; my $port = (getservbyname('smtp', 'tcp'))[2]; my $smtpaddr = pack('C4',$1,$2,$3,$4) if ( $smtp =~ /^(\d{1,3})\.(\d{1 +,3})\.(\d{1,3})\.(\d{1,3})$/ ); socket(S, AF_INET, SOCK_STREAM, $proto) or die("socket failed: $!"); connect(S, pack('Sna4x8', AF_INET, $port, $smtpaddr)) or die("connect +failed: $!");; # part 2, read first line: local($oldfh) = select(S); $| = 1; select($oldfh); while (my $line = <S>) { print "--> $line\n"; last; }
Thanks,
Joe

P.S. I'm using perl 5.8.1 on windows, in case the version/os makes a difference here.

Replies are listed 'Best First'.
Re: hung reading first line of socket
by madizen (Sexton) on Feb 23, 2006 at 05:41 UTC

    At first I thought some app-specific Windows firewall on the Perl host (e.g., telnet.exe allowed, perl.exe not). But you're getting the whole connection set up to the point of getting the 220 back from the server... seems to indicate that's not really it (check anyway).

    A PIX firewall can foil SMTP midstream but to my knowledge usually not before you HELO (also, I think it 500's you). Any chance of getting the mail server admin involved? Its logs might tell something. Check also system and security logs on the Windows host.

    If there is any kind of Windows firewall, try to make explicit permission rules for Perl to both send and receive.

Re: hung reading first line of socket
by spiritway (Vicar) on Feb 23, 2006 at 04:10 UTC

    If your code works for others, then I doubt that rewriting the script would be necessary. You might want to see what is different about the client's machine.

    I ran into a similar problem when a server changed SMTP ports from 25 to 26, but you say you are using the same port with telnet. It would be nice of you could get an error message.