Page 1 of 1

Never ending while loop

Posted: Tue Jan 02, 2007 7:08 pm
by aliasxneo
Here is my code:

Code: Select all

<?php

error_reporting(E_ALL);
ob_implicit_flush();


$service_port = getservbyname('smtp', 'tcp');
$address = gethostbyname('smtp.gmail.com');


$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);

while ($msg = socket_read($socket, 2048)) {
   echo $msg;
}

echo "Done...";
?>
If you havn't guessed I'm trying to connect to an smtp server. The code works fine, it connects, and the SMTP server responds correctly, the only problem is the while loop will not finish. When using implicit flushing I get everything printed except for the "Done..." part, which tells me the while loop will not terminate. I know this is because message is always true because it always contains content. My question is how can I tell when I have read all the content from the stream? Is there a PHP function that will tell me how many bytes are left? Thanks.

Cheers,
- Josh

Posted: Tue Jan 02, 2007 7:16 pm
by RobertGonzalez
According to the manual:
Note: socket_read() returns a zero length string ("") when there is no more data to read.

Posted: Tue Jan 02, 2007 7:20 pm
by aliasxneo
Well that doesn't make much sense. You can test the script yourself, the while loop will not end, am I doing something wrong?

Posted: Tue Jan 02, 2007 7:21 pm
by RobertGonzalez
I'm thinking maybe something like

Code: Select all

<?php
while ( ($msg = socket_read($socket, 2048)) !== '') {
   echo $msg;
} 
?>

Posted: Tue Jan 02, 2007 9:15 pm
by Ollie Saunders
http://php.net/manual/en/types.comparisons.php
'' is considered false already.

Do a search for the "Done..." string in the source that the script generates.

Posted: Tue Jan 02, 2007 10:10 pm
by aliasxneo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="ole"]http://php.net/manual/en/types.comparisons.php
'' is considered false already.

Do a search for the "Done..." string in the source that the script generates.[/quote]

I am confused by what you mean. The data is printed out fine..

[quote]Attempting to connect to '209.85.133.111' on port '25'...220 mx.google.com ESMTP d38sm24762106and[/quote]

That's when it stops though and my browser just keeps saying "Loading Page.." and it never finishes. I tried the following code:

Code: Select all

<?php

error_reporting(E_ALL);
ob_implicit_flush();


$service_port = getservbyname('smtp', 'tcp');
$address = gethostbyname('smtp.gmail.com');


$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);

while ( strlen($msg = socket_read($socket, 2048)) !== 1) {
   echo $msg;
   echo "test";
}

echo "Done...";
?>
And got:
Attempting to connect to '209.85.133.111' on port '25'...220 mx.google.com ESMTP d38sm24762106and test
And then it did the same thing before, got stuck and my browser kept saying "Loading Page..".


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jan 02, 2007 10:44 pm
by feyd
Google's smtp servers don't communicate on port 25. :? Last I checked they're on a secure channel only. Plus you actually have to talk to it for it to send you more.

Posted: Tue Jan 02, 2007 10:59 pm
by brendandonhue
Gmail also requires TLS, you'll probably be better off using a library like SwiftMailer than implementing it yourself.

Posted: Tue Jan 02, 2007 11:19 pm
by aliasxneo
Well Telnet worked fine with it.

Code: Select all

telnet smtp.gmail.com 25
EHLO mx.google.com
STARTTLS
MAIL FROM: ...
MAIL TO: .....
And so on.