With
 php you can, just like with the java or c + + or any other language, 
implement tcp sockets for the exchange of data according to the 
client-server scheme.I
 show you how you can send a text file using a socket in php from a 
client to a server implementing only 2 files php: Server.php and 
Client.php.We start from the Server:
    <? Php
    set_time_limit (0);
    if (($ sock = @ socket_create (AF_INET, SOCK_STREAM, SOL_TCP))) {
    echo socket_strerror (socket_last_error ($ sock)); exit;
    }
    if ((@ socket_set_option ($ sock, SOL_SOCKET, SO_REUSEADDR, 1))) {echo socket_strerror (socket_last_error ($ sock));
    exit;
    }
    if ((@ socket_bind ($ sock, '10 .0.1.4 ', 1048))) {/ / associate an ip of the machine and bring it to the socket
    echo socket_strerror (socket_last_error ($ sock)); exit;
    }
    if ((@ socket_listen ($ sock))) {/ / put the listening socket
    echo socket_strerror (socket_last_error ($ sock)); exit;
    }
    while (true) {
    if (($ client = @ socket_accept ($ sock))) {/ / locking function: as long as it receives a request
    echo socket_strerror (socket_last_error ($ sock));
    exit;}
    if
 ((@ socket_getpeername ($ client, $ addr))) {/ / we take the IP address
 of the client that is connected echo socket_strerror (socket_last_error
 ($ sock));
    exit;
    }
    echo "Client connection from: $ addr \ n";
    echo "Receiving file."
    if (($ file = @ fopen ("." time ().. "txt", "w"))) {
    echo "File open error. \ n"; exit;
    }
    if (($ rcv = @ socket_read ($ client, 1024, PHP_BINARY_READ))) {
    echo socket_strerror (socket_last_error ($ sock));
    exit;}
    do {
    if ((@ fwrite ($ file, $ rcv, 1024))) {
    echo socket_strerror (socket_last_error ($ sock));
    exit;}
    if ((@ socket_write ($ client, "ok"))) {
    echo socket_strerror (socket_last_error ($ sock));
    exit;
    }
    if (($ rcv = @ socket_read ($ client, 1024, PHP_BINARY_READ))) {echo socket_strerror (socket_last_error ($ sock));
    exit;
    }
    echo "."
    } While ($ rcv! = "- @ END @ -");
    echo "DONE \ n"; socket_close ($ client); break;
    }
    fclose ($ file);
    socket_close ($ sock);
    ?>
And now the client:
    <? Php
    set_time_limit (0); / / Avoid that after a certain time the interpreter blocks php script as php.ini
    if (($ file = @ fopen ("myfile.txt", "r"))) {
    echo "File open error \ n."
    exit;}
    $ Server = array ('address' => 'Enter server ip', 'port' => 1048);
    $ Sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
    if
 (($ connection = @ socket_connect ($ sock, $ server ['address'], $ 
server ['port']))) {echo socket_strerror (socket_last_error ($ sock));
    exit;
    }
    echo "Uploading files."
    do {
    if (feof ($ file)) {
    echo "send complete \ n"; socket_write ($ sock, "- @ END @ -"); break;
    }
    if (($ pkt = @ fread ($ file, 1024))) {
    echo socket_strerror (socket_last_error ($ sock));
    exit;}
    if ((@ socket_write ($ sock, $ pkt, 1024))) {
    echo socket_strerror (socket_last_error ($ sock)); exit;
    }
    echo "."
    if (($ rcv = @ socket_read ($ sock, 1024))) {
    echo socket_strerror (socket_last_error ($ sock)); exit;
    }
    / / Echo $ rcv. "\ N";
    } While (true);
    echo "DONE \ n";
    fclose ($ file); socket_close ($ sock); echo "Exiting ... \ n";
    ?>
First
 start the server socket and then the client socket and you will see 
that the file "myfile.txt" will be sent from the client to the server!
 
Nessun commento:
Posta un commento