php tcp server - sockets - how to handle more clients at once ?

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default php tcp server - sockets - how to handle more clients at once ?

    Hello

    I've modified the tcp-server example from [url]http://www.php.net/sockets[/url]
    The server waits for a connection, then for \r\n and disconnect
    the remote host. My question is, how to modify this code,
    to make the server available for more than one remote host
    at once? I mean, that more clients could connect at the
    same time? Im new with socket programming.

    Thanks for any help.

    #!/bin/php-cmd -q
    <?php
    error_reporting(E_ALL);
    set_time_limit(0);
    ob_implicit_flush();
    $address = '192.168.1.1';
    $port = 99;

    echo "Creating...\n";
    if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
    echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
    }
    echo "Binding...\n";
    if (($ret = socket_bind($sock, $address, $port)) < 0) {
    echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
    }

    echo "Listening...\n";
    if (($ret = socket_listen($sock, 5)) < 0) {
    echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
    }

    do {
    echo "Waiting for a connection...\n";
    if (($msgsock = socket_accept($sock)) < 0) {
    echo "socket_accept() failed: reason: " .
    socket_strerror($msgsock) . "\n";
    }
    socket_getpeername($msgsock,$ip, $port);
    echo "Incoming connection from {$ip}:{$port}\n";
    $msg = "+OK {$ip}:{$port}\r\n";
    socket_write($msgsock, $msg, strlen($msg));

    $buf = socket_read($msgsock, 8192, PHP_NORMAL_READ);
    echo $buf;

    $msg = "+OK Received: $buf\r\n";
    socket_write($msgsock, $msg, strlen($msg));
    socket_close($msgsock);
    echo "Connection closed...\n";
    } while(true);

    socket_close($sock);
    ?>

    websafe Guest

  2. Similar Questions and Discussions

    1. Handle disconnected clients?
      Hello, I have a WebMethod that returns a large array of bytes (actually some files, but i don't think it matters). How can i know on the...
    2. Retrieving clients ip adress on the server
      Hello, I have looked a lot but not found how to do this: I have a Web Services server and from this I want to see what IP adress the Client...
    3. AIX DHCP Server and W2K Clients: help!
      Hi, I am trying to set up a "simple" AIX DHCP server directly connected to W2K/NT Clients (no routers). Everything is properly connected, but the...
    4. WTT: NT Server w/ 10 Clients
      Have Windows NT 4.0 Server with 10 Client licenses. Fully registerable. Wanted Dreamweaver/Flash/Fireworks package. Newest/Registerable only. ...
    5. NT-Server and 2 XP-Clients no shares??
      Hallo all, i'm close to going crazy trying to link two XP Pro clients to an NT 4 server, so that the two clients can communicate with each other!!...
  3. #2

    Default Re: php tcp server - sockets - how to handle more clients at once ?

    i'm like you,this code will accept more than one client,but it can't receive data from them,i'm trying to solve that,hope this one will help
    <?php
    //Server
    set_time_limit(0);
    $host='127.0.0.1';
    $port=9997;
    $socket=socket_create(AF_INET,SOCK_STREAM,0);
    socket_bind($socket,$host,$port);
    socket_listen($socket);

    $client=array($socket);
    while (true) {
    //$read=$client;
    $cliid=0;
    socket_select($client,$write=NULL,$except=NULL,$tv _sec=NULL);
    if(in_array($socket,$client)) {
    for($i=1;$i<5;$i++)
    {
    if(!isset($client[$i]))
    {
    $client[$i]['socket']=socket_accept($socket);
    $cliid+=1;
    socket_getpeername($client[$i]['socket'], $ip);
    echo "Client #".$cliid." With IP {$ip} Connected\n";
    $msg="\nWelcome to the PHP Test Server. \n" .
    "To quit, type 'quit'.\n";
    socket_write($client[$i]['socket'],$msg);
    } //inside if
    } //for
    break;
    }
    } //while
    socket_close($socket); //for accepting
    ?>
    Majid Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139