socket_read() Timeout

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
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

socket_read() Timeout

Post by neel_basu »

Code: Select all

................
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$connection = socket_connect($socket,'localhost',25);
$greet = socket_read($socket, 1024);
echo $greet;
.................
This above Code Works And Shows The Greeting Messege Where as.
This Following Code Is making time out nothing else . It is Not Showing The Reply.

Code: Select all

....................
echo socket_write($socket,$text.'\r\n',strlen($text)+2)."<br />\n";
$reply = socket_read($socket, 1024);
echo $reply;
.........................
But This Code

Code: Select all

...................
echo socket_write($socket,$text.'\r\n',strlen($text)+2)."<br />\n";
//$reply = socket_read($socket, 1024);
//echo $reply;
.....................
Shows Result 16
any solutions ??
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

It looks like the server is not answering anything.
Show the server code that replies to that message.
Maybe it expects a zero-terminated string? In any case you can safely omit the third parameter of socket_write.

Also, you gotta make sure all functions succeeded, check the return values after each call, RTSM for details.

P.S. In the English language you're not expected to capitalise every word of the sentence, the first one and the occasional name and "I" are enough!
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

It Showing "220" as on Greeting.
Mordred wrote:It looks like the server is not answering anything.
No server is answering . fread can read it and if i use fread() in the place of socket_read() it works smoothly.
Mordred wrote:Maybe it expects a zero-terminated string? In any case you can safely omit the third parameter of socket_write.
No It accepts \r\n Terminated Strings.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

No server is answering . fread can read it and if i use fread() in the place of socket_read() it works smoothly.
fread() can read from a file or a socket.
socket_read() can only read from a socket.

What are you trying to do here?
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Re: socket_read() Timeout

Post by neel_basu »

neel_basu wrote:

Code: Select all

................
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$connection = socket_connect($socket,'localhost',25);
$greet = socket_read($socket, 1024);
echo $greet;
.................
Obviously from a socket.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Then show us the server code and returned values after each socket_*() call. What did you mean by "No server is answering"?
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

neel_basu wrote:It Showing "220" as on Greeting.
Its Is Showing 220 and Its Correct.
I've Told in The Previous Posts
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

State your problem and give all relevant code. Until you do so nobody can help you.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Re: socket_read() Timeout

Post by neel_basu »

I am writting it again
neel_basu wrote:

Code: Select all

................
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$connection = socket_connect($socket,'localhost',25);
$greet = socket_read($socket, 1024);
echo $greet;
.................
This above Code Works And Shows The Greeting Messege Where as.
This Following Code Is making time out nothing else . It is Not Showing The Reply.

Code: Select all

....................
echo socket_write($socket,$text.'\r\n',strlen($text)+2)."<br />\n";
$reply = socket_read($socket, 1024);
echo $reply;
.........................
But This Code

Code: Select all

...................
echo socket_write($socket,$text.'\r\n',strlen($text)+2)."<br />\n";
$reply = socket_read($socket, 1024);//Timeout Happens For This Line
echo $reply;//Actually It Shoul Show 250
.....................
Shows Result 16
any solutions ??
Look

Code: Select all

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); 
$connection = socket_connect($socket,'mail.bluebottle.com',25); 
$greet = socket_read($socket, 1024);//This socket_read() Works and Sockets Replies With 220
echo $greet;//It Also Works And Shows The Reply Text With 220 Code
//220 fe1.bluebottle.com ESMTP Sendmail 8.13.1/8.13.1; Thu, 8 Feb 2007 02:54:38 -0800 
$text = "HELO localhost";
echo socket_write($socket,$text.'\r\n',strlen($text)+2)."<br />\n"; //This Also Works and returns 26
$reply = socket_read($socket, 1024); //But This Line Doesn't Work Just Makes Timeout Nothing else
echo $reply;
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Ah, you're connecting to an existing remote server. As I said before, the server isn't answering, that's why your read fails.

I finally see the reason why: '\r\n' should be "\r\n"
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Ya Its Working Now
But Why " Instead of '
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Thanks
Post Reply