Sockets with PHP
I need help =( I made a simple TCP/IP server that will talk to someone, but for some reason when typing it will only allow one letter and then stop letting you type. How do I get it so people can type a sentance or more?
Also, it will only allow one person to talk to it. How do I get it to accept lots of connections? (Like up to 10)?
And is there a turorial somewhere on PHP sockets?
Thanks in advance,
Zalbag
Oh and here is the code I am currently using (adapted from the php manual), also if you know of a more efficent way of doing this please tell me
<?php
dl('php_sockets.dll');
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
$address = 'xxx.xxx.x.xxx';
$port = 10000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port);
socket_listen($sock, 5);
while (true)
{
$msgsock = socket_accept($sock);
$do_again = \"y\";
while ($do_again == \"y\")
{
$talkback = \"\n\rServer: Please input data...\n\r\";
socket_write($msgsock, $talkback, strlen ($talkback));
$text = socket_read($msgsock, 2);
$talkback = \"\n\rServer: You said '$text'.\n\r\";
socket_write($msgsock, $talkback, strlen ($talkback));
echo \"$text\n\";
$talkback = \"\n\rServer: Would you like to do that again ?\n\r\";
socket_write($msgsock, $talkback, strlen($talkback));
$choice = socket_read($msgsock, 1);
$choice = trim($choice);
if ($choice == \"n\")
{
$talkback = \"\n\rServer: No?...quiting...\n\r\";
socket_write($msgsock, $talkback, strlen($talkback));
break;
}
elseif ($choice == \"y\")
{
}
else
{
$talkback = \"\n\rServer: Invalid answer...quiting...\n\r\";
socket_write($msgsock, $talkback, strlen($talkback));
break;
}
}
socket_close ($msgsock);
}
socket_close ($sock);
?>
Wil posted this at 10:40 — 22nd April 2002.
They have: 601 posts
Joined: Nov 2001
Do the following links help you at all?
http://www.phpbuilder.com/columns/armel20010427.php3
http://en.webmaster.bankhacker.com/php/sockets.phtml
http://www.zend.com/manual/ref.sockets.php
- wil
Mark Hensler posted this at 21:42 — 22nd April 2002.
He has: 4,048 posts
Joined: Aug 2000
I think he's already fixed this...
http://webmaster-forums.com/showthread.php?s=&threadid=18269
Wil posted this at 08:41 — 23rd April 2002.
They have: 601 posts
Joined: Nov 2001
Aha. Great. My bad :-\
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.