Sockets with PHP

They have: 2 posts

Joined: Apr 2002

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 Smiling

<?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);
?>

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

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.