Ejemplo SockEjemplo Socket PHP.docxet PHP

download Ejemplo SockEjemplo Socket PHP.docxet PHP

of 7

Transcript of Ejemplo SockEjemplo Socket PHP.docxet PHP

  • 7/26/2019 Ejemplo SockEjemplo Socket PHP.docxet PHP

    1/7

    http://php.net/manual/es/sockets.examples.phpEjemplo #1 Ejemplo de socket: Servidor TCP/IP sencillo

    Ejemplos

    Ejemplo #1 Ejemplo de socket: Servidor TCP/IP sencillo

    Este ejemplo muestra una respuesta de servidor simple. Cambie las variablesaddressyportpara ajustar su

    configuracin y ejectelo. Debe despus conectar el servidor con un comando similar a: telnet 192.168.1.53

    10000(donde la direccin y el puerto deben coincidir con su cofiguracin). Cualquier cosa que escriba ser impresa en

    el lado del servidor, y vuelta a repetir (echo) para usted. Para desconectar, introduzca 'quit'.

    #!/usr/local/bin/php -q

  • 7/26/2019 Ejemplo SockEjemplo Socket PHP.docxet PHP

    2/7

    @ara salir escriba ,quit, ara cerrar el ser%i&or escriba ,shut&oIn,Dn@; soc4et_Irite(sgsoc4 sg strlen(sg));

    &o i (alse +++ (bu + soc4et_rea&(sgsoc4 $J1 H_7:=>AL_=

    EAG))) echo @soc4et_rea&() allB raCnB @ soc4et_strerror(soc4et_last_error(sgsoc4)) @Dn@; brea4 ; i (!bu + tri(bu)) continue; i (bu ++ ,quit,) brea4; i (bu ++ ,shut&oIn,)

    soc4et_close(sgsoc4); brea4 ; tal4bac4 + @HB Kste& &io ,bu,Dn@; soc4et_Irite(sgsoc4 tal4bac4 strlen(tal4bac4)); echo @buDn@; Ihile (true); soc4et_close(sgsoc4); Ihile (true);

    soc4et_close(soc4);?M

    Ejemplo #2 Ejemplo de socket: Cliente TCP/IP sencillo

    Este ejemplo muestra un simple, nico cliente HTTP. Simplemente se conecta a una pgina, enva una peticin HEAD,

    repite la rplica, y sale.

  • 7/26/2019 Ejemplo SockEjemplo Socket PHP.docxet PHP

    3/7

    @;result + soc4et_connect(soc4et a&&ress ser%ice_port);i (result +++ alse) echo @soc4et_connect() allDn=aCnB (result) @ soc4et_strerror(soc4et_last_error(soc4et)) @Dn@; else

    echo @:Dn@;

    in + @HEAG / H88/DrDn@;in + @HostB IIIe"aplecoDrDn@;in + @onnectionB loseDrDnDrDn@;out + ,,;

    echo @En%ian&o peticin H88 HEAG @;soc4et_Irite(soc4et in strlen(in));echo @:Dn@;

    echo @LeOen&o respuestaBDnDn@;Ihile (out + soc4et_rea&(soc4et $J1)) echo out;

    echo @erran&o soc4et@;soc4et_close(soc4et);echo @:DnDn@;?M

  • 7/26/2019 Ejemplo SockEjemplo Socket PHP.docxet PHP

    4/7

    COMENTARIOS

    javier,javiern at gmail dot com

    3 years ago

    Pou can easilO e"ten& the irst e"aple to han&le anO nuber oconnections instea& o sut one

    #!/usr/bin/en% php

  • 7/26/2019 Ejemplo SockEjemplo Socket PHP.docxet PHP

    5/7

    i (in_arraO(soc4 rea&))

    i ((sgsoc4 + soc4et_accept(soc4)) +++ alse) echo @soc4et_accept() allB raCnB@ soc4et_strerror(soc4et_last_error(soc4)) @Dn@; brea4;

    clientsRS + sgsoc4; 4eO + arraO_4eOs(clients sgsoc4); /* En%iar instrucciones */ sg + @DnFien%eni&o al 9er%i&or Ge rueba &e H Dn@ @Kste& es el cliente nueroB 4eOR$SDn@ @ara salir escriba ,quit, ara cerrar el ser%i&or escriba,shut&oIn,Dn@; soc4et_Irite(sgsoc4 sg strlen(sg));

    // Han&le 6nput oreach (clients as 4eO +M client) // or each clienti (in_arraO(client rea&))

    i (alse +++(bu + soc4et_rea&(client $J1 H_7:=>AL_=EAG))) echo @soc4et_rea&() allB raCnB@ soc4et_strerror(soc4et_last_error(client)) @Dn@; brea4 ; i (!bu + tri(bu)) continue; i (bu ++ ,quit,)

    unset(clientsR4eOS); soc4et_close(client); brea4; i (bu ++ ,shut&oIn,) soc4et_close(client); brea4 ; tal4bac4 + @liente 4eOB Kste& &io ,bu,Dn@; soc4et_Irite(client tal4bac4 strlen(tal4bac4)); echo @buDn@;

    Ihile (true);

    soc4et_close(soc4);?M

    up

    down

    -35

    Anonymous

    1 year ago

    A ore obect oriente& e"aple o the ser%er

    #!/usr/bin/en% php

  • 7/26/2019 Ejemplo SockEjemplo Socket PHP.docxet PHP

    6/7

    class >O9oc4et9er%er protecte& soc4et; protecte& clients + RS; protecte& change&;

    unction __construct(host + ,localhost, port + .$$$)

    set_tie_liit($); soc4et + soc4et_create(A5_67E8 9:_98=EA> 9:L_8); soc4et_set_option(soc4et 9:L_9:E8 9:_=EK9EAGG= );

    //bin& soc4et to speciie& host soc4et_bin&(soc4et $ port); //listen to port soc4et_listen(soc4et); this-Msoc4et + soc4et;

    unction __&estruct()

    oreach(this-Mclients as client) soc4et_close(client); soc4et_close(this-Msoc4et);

    unction run() Ihile(true)

    this-MIait5orhange(); this-Mchec47eIlients(); this-Mchec4>essage=ecie%e&(); this-Mchec4Gisconnect();

    unction chec4Gisconnect() oreach (this-Mchange& as change&_soc4et) bu +Tsoc4et_rea&(change&_soc4et $J H_7:=>AL_=EAG);

    i (bu !++ alse) // chec4 &isconnecte& client continue; // reo%e client or clients arraO oun&_soc4et + arraO_search(change&_soc4et this-Mclients); soc4et_getpeernae(change&_soc4et ip); unset(this-MclientsRoun&_soc4etS); response + ,client , ip , has &isconnecte&,; this-Msen&>essage(response);

    unction chec4>essage=ecie%e&()

  • 7/26/2019 Ejemplo SockEjemplo Socket PHP.docxet PHP

    7/7

    oreach (this-Mchange& as 4eO +M soc4et) buer + null; Ihile(soc4et_rec%(soc4et buer $J $) M+ ) this-Msen&>essage(tri(buer) H_E:L); unset(this-Mchange&R4eOS); brea4;

    unction Iait5orhange() //reset change& this-Mchange& + arraO_erge(Rthis-Msoc4etS this-Mclients); //%ariable call tie pass bO reerence req o soc4et_select null + null; //this ne"t part is bloc4ing so that Ie &ont run aIaO Iith cpu soc4et_select(this-Mchange& null null null);

    unction chec47eIlients()

    i (!in_arraO(this-Msoc4et this-Mchange&)) return; //no neI clients soc4et_neI + soc4et_accept(this-Msoc4et); //accept neIsoc4et irst_line + soc4et_rea&(soc4et_neI $J); this-Msen&>essage(,a neI client has connecte&, H_E:L); this-Msen&>essage(,the neI client saOs, tri(irst_line) H_E:L);

    this-MclientsRS + soc4et_neI; unset(this-Mchange&R$S);

    unction sen&>essage(sg) oreach(this-Mclients as client) Tsoc4et_Irite(clientsgstrlen(sg)); return true;

    (neI >O9oc4et9er%er())-Mrun();?M

    up

    down

    -119

    Anonymous

    2 years ago

    8he %ariable in in 8 client e"ape reallO nee&s to en& Iith ,Dn, orotherIise it &oesn,t ire the rea& e%ent in the ser%er si&e (at leastin O Gelphi progra)

    http://php.net/manual/vote-note.php?id=113232&page=sockets.examples&vote=uphttp://php.net/manual/vote-note.php?id=113232&page=sockets.examples&vote=downhttp://php.net/manual/es/sockets.examples.php#113232http://php.net/manual/es/sockets.examples.php#113232http://php.net/manual/vote-note.php?id=113232&page=sockets.examples&vote=uphttp://php.net/manual/vote-note.php?id=113232&page=sockets.examples&vote=downhttp://php.net/manual/es/sockets.examples.php#113232http://php.net/manual/es/sockets.examples.php#113232