Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    [ PHP ] come creare un server?

    come faccio a rimanere una applicazione PHP in ascolto su una porta?
    potete postare una parte del codice... al protocollo poi ci penso io
    C/C++

  2. #2

  3. #3
    Utente di HTML.it L'avatar di PaoDJ
    Registrato dal
    Oct 2001
    Messaggi
    273
    codice:
    <?
    // don't timeout!
    set_time_limit(0);
    
    // set some variables
    $host = "123.123.123.123";
    $port = 10005;
    // create socket
    $socketS = Array();
    $socketS[0] = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    // bind socket to port
    $result = socket_bind($socketS[0], $host, $port) or die("Could not bind to socket\n");
    // start listening for connections
    $result = socket_listen($socketS[0], 3) or die("Could not set up socket listener\n");
    // Array that will hold client information
    $GO=1;
    echo "Waiting for connections...\n";
    $welcome = "welcome\n? ";
    
    // keep looping and looking for client input
    while ($GO>0) {
    $connections=count($socketS);
    for ($i = 0; $i < $connections; $i++){
            if ($socketS[$i]  != null)
                $read[$i] = $socketS[$i];
        }
    
    $ready=socket_select($read,$null=null,$null=null,$null=null);
    
    for ($i = 1; $i <$connections ; $i++){
        if (in_array($socketS[$i] , $read)){
              $input = socket_read($socketS[$i] , 1024);
                echo "Form $i $input\n";
            
            if ($input == null) {
                echo "$i Disconnected\n";
                socket_close($socketS[$i]);
                array_splice($socketS ,$i,1);
                if ($i!=$connections){
                $connections--;};
              }
              }
    }
    if (in_array($socketS[0], $read)) {
        $new_client = socket_accept($socketS[0]) or die("Could not accept incoming connection\n");
        array_push($socketS, $new_client);
        socket_write($new_client, $welcome, strlen ($welcome)) or die("Could not send connect string\n");
        socket_getpeername($new_client,$Host_URL,$Host_PORT);
        echo "\n Received connection request from : $Host_URL:$Host_PORT\n";
           }
    }
    // close primary socket
    socket_close($socketS[0]);
    echo "Socket terminated\n";
    ?>

  4. #4
    Utente di HTML.it L'avatar di PaoDJ
    Registrato dal
    Oct 2001
    Messaggi
    273
    E' un server in grado di accettare più client e che ti può essere molto utile. io l'ho adeguatamente modificato per fare un chat server...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.