Never ending while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Never ending while loop

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

According to the manual:
Note: socket_read() returns a zero length string ("") when there is no more data to read.
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I'm thinking maybe something like

Code: Select all

<?php
while ( ($msg = socket_read($socket, 2048)) !== '') {
   echo $msg;
} 
?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

Gmail also requires TLS, you'll probably be better off using a library like SwiftMailer than implementing it yourself.
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post 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.
Post Reply