Php socket programming example

Source: Internet
Author: User

2 php test files
Server. php

Copy codeThe Code is as follows:
<? Php
// Phpinfo ();
// Ensure that no timeout occurs when you connect to the client
Set_time_limit (0 );

$ Ip = '1970. 0.0.1 ';
$ Port = 1935;

/*
+ -------------------------------
* @ Socket: entire communication process
+ -------------------------------
* @ Socket_create
* @ Socket_bind
* @ Socket_listen
* @ Socket_accept
* @ Socket_read
* @ Socket_write
* @ Socket_close
+ --------------------------------
*/

/* ---------------- The following operations are all in the manual -------------------*/
If ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) <0 ){
Echo "socket_create () failed because:". socket_strerror ($ sock). "\ n ";
}

If ($ ret = socket_bind ($ sock, $ ip, $ port) <0 ){
Echo "socket_bind () failed because:". socket_strerror ($ ret). "\ n ";
}

If ($ ret = socket_listen ($ sock, 4) <0 ){
Echo "socket_listen () failed because:". socket_strerror ($ ret). "\ n ";
}

$ Count = 0;

Do {
If ($ msgsock = socket_accept ($ sock) <0 ){
Echo "socket_accept () failed: reason:". socket_strerror ($ msgsock). "\ n ";
Break;
} Else {

// Send to client
$ Msg = "test successful! \ N ";
Socket_write ($ msgsock, $ msg, strlen ($ msg ));

Echo "test succeeded \ n ";
$ Buf = socket_read ($ msgsock, 8192 );


$ Talkback = "received message: $ buf \ n ";
Echo $ talkback;

If (++ $ count> = 5 ){
Break;
};


}
// Echo $ buf;
Socket_close ($ msgsock );

} While (true );

Socket_close ($ sock );
?>


Client. php
Copy codeThe Code is as follows:
<? Php
Error_reporting (E_ALL );
Set_time_limit (0 );
Echo "

$ Port = 1935;
$ Ip = "127.0.0.1 ";

/*
+ -------------------------------
* @ Socket connection process
+ -------------------------------
* @ Socket_create
* @ Socket_connect
* @ Socket_write
* @ Socket_read
* @ Socket_close
+ --------------------------------
*/

$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
If ($ socket <0 ){
Echo "socket_create () failed: reason:". socket_strerror ($ socket). "\ n ";
} Else {
Echo "OK. \ n ";
}

Echo "trying to connect '$ ip' port' $ port'... \ n ";
$ Result = socket_connect ($ socket, $ ip, $ port );
If ($ result <0 ){
Echo "socket_connect () failed. \ nReason: ($ result)". socket_strerror ($ result). "\ n ";
} Else {
Echo "Connect OK \ n ";
}

$ In = "Ho \ r \ n ";
$ In. = "first blood \ r \ n ";
$ Out = '';

If (! Socket_write ($ socket, $ in, strlen ($ in ))){
Echo "socket_write () failed: reason:". socket_strerror ($ socket). "\ n ";
} Else {
Echo "sent to server information! \ N ";
Echo "sent content: <font color = 'red'> $ in </font> <br> ";
}

While ($ out = socket_read ($ socket, 8192 )){
Echo "received server return message successful! \ N ";
Echo "received content:", $ out;
}

Echo "Disable SOCKET... \ n ";
Socket_close ($ socket );
Echo "Disable OK \ n ";
?>

Listen
Copy codeThe Code is as follows:
#/Usr/local/php/bin/php/usr/local/apache2/htdocs/server. php


 

RequestCopy codeThe Code is as follows:
#/Usr/local/php/bin/php/usr/local/apache2/htdocs/client. php


Socket is not enabled in PHP by default.
 Copy codeThe Code is as follows:
# Cd./ext/sockets/
#/Usr/local/php/bin/phpize
#./Configure -- enable-sockets -- with-php-config =/usr/local/php/bin/php-config
# Make
# Make install


Php. ini modify configuration
Add
Copy codeThe Code is as follows:
Extension = sockets. so


Restart apache
Copy codeThe Code is as follows:
#/Usr/local/apache2/bin/apachectl restart


Configuration in Windows
Modify php. ini
Copy codeThe Code is as follows:
Extension = php_sockets.dll

Restart apache
2 under cmd
Php installation directory

 Copy codeThe Code is as follows:
C:/php/php.exe F: web/server. php
C:/php/php.exe F: web/client. php

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.