Home  >  Article  >  Backend Development  >  Socket request and receive examples

Socket request and receive examples

WBOY
WBOYOriginal
2016-08-08 09:21:38932browse

Client

error_reporting(E_ALL);
set_time_limit(0);
echo "

TCP/IP Connection

n";
$port = 1935;
$ip = "127.0.0.1";
/*
+----------------------------------
* * @socket connection entire 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 "Attempting to connect to '$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 OKn";
}
$in = "Did you see our namern";
$out = '';
if(!socket_write($socket, $in, strlen($in))) {
  echo "socket_write() failed: reason: " . socket_strerror($socket) . "n";
}else {
echo "The information sent to the server was successfully sent! n";
}
while($out = socket_read($socket, 8192)) {
echo "The information sent back by the server was successfully received! n";
  echo "The accepted content is:",$out;
}
echo "Close SOCKET...n";
socket_close($socket);
echo "Close OKn";

?>

Server side

//Ensure that there will be no timeout when connecting to the client
set_time_limit(0);
$ip = '127.0.0.1';
$port = 1935;
/*
+--- ----------------------------
* @The whole process of socket communication
+------------- ------------------
* * @socket_create
* * @socket_bind
* * @socket_listen
* * @socket_accept
* * @socket_read
* * @socket_write
* * @socket_close
+-- ----------------------------------
*/
/*-------------- -- The following operations are 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( ) The reason for failure is: ".socket_strerror($ret)."n";
}
$count = 0;
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo " Socket_accept () failed: release: ". Socket_Strerror ($ msgsock)." n ";
Break;
} else {
// See it to the client
$ msg =" See, your name is! n";
          socket_write($msgsock, $msg, strlen($msg)); bufn";
echo $talkback;
                                                                          _close($msgsock);
} while (true);
socket_close ($sock);
?>


The above introduces socket request and receive examples, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn