Examples of TCP server side and client side functions implemented by PHP programming

  • 2021-09-20 19:37:42
  • OfStack

This paper describes the TCP server and client functions realized by PHP programming. Share it for your reference, as follows:

1. Modify php. ini and open extension=php_sockets. dll

2. Server program SocketServer. php


<?php
// Ensure that you do not time out when connecting to the client 
set_time_limit(0);
// Settings IP And port number 
$address = "127.0.0.1";
$port = 3046;
/**
 *  Create 1 A SOCKET
 * AF_INET= Yes ipv4  If you use ipv6 The parameter is  AF_INET6
 * SOCK_STREAM For socket Adj. tcp Type, if it is UDP Use the SOCK_DGRAM
*/
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() fail:" . socket_strerror(socket_last_error()) . "/n");
// Blocking mode 
socket_set_block($sock) or die("socket_set_block() fail:" . socket_strerror(socket_last_error()) . "/n");
// Bind to socket Port 
$result = socket_bind($sock, $address, $port) or die("socket_bind() fail:" . socket_strerror(socket_last_error()) . "/n");
// Start listening 
$result = socket_listen($sock, 4) or die("socket_listen() fail:" . socket_strerror(socket_last_error()) . "/n");
echo "OK\nBinding the socket on $address:$port ... ";
echo "OK\nNow ready to accept connections.\nListening on the socket ... \n";
do { // never stop the daemon
  // It receives the connection request and calls the 1 Sub-connection Socket To process the information between the client and the server 
  $msgsock = socket_accept($sock) or die("socket_accept() failed: reason: " . socket_strerror(socket_last_error()) . "/n");
  while(1){
    // Reading client data 
    echo "Read client data \n";
    //socket_read Function will 1 Read client data directly , Until I met \n,\t Or \0 Character .PHP The script treats this write character as the terminator of the input .
    $buf = socket_read($msgsock, 8192);
    echo "Received msg: $buf  \n";
    if($buf == "bye"){
      // After receiving the end message, close the connection and wait 1 Connections 
      socket_close($msgsock);
      continue;
    }
    // Data transmission   Write the return result to the client 
    $msg = "welcome \n";
    socket_write($msgsock, $msg, strlen($msg)) or die("socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n");
  }
} while (true);
socket_close($sock);
?>

3. Client program SocketClient. php


<?php
set_time_limit(0);
$host = "127.0.0.1";
$port = 3046;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)or die("Could not create socket\n");
$connection = socket_connect($socket, $host, $port) or die("Could not connet server\n");
socket_write($socket, "hello socket") or die("Write failed\n");
while ($buff = socket_read($socket, 1024, PHP_NORMAL_READ)) {
  echo("Response was:" . $buff . "\n");
  echo("input what you want to say to the server:\n");
  $text = fgets(STDIN);
  socket_write($socket, $text);
}
socket_close($socket);
?>

4. Test

Run the server-side program: C:\ wamp\ bin\ php\ php5.4. 16\ php. exe C:\ wamp\ www\ SocketServer. php

Run the client program: C:\ wamp\ bin\ php\ php5.4. 16\ php. exe C:\ wamp\ www\ SocketClient. php

If encountered

Fatal error: Call to undefined function socket_create ().

1. Find php. ini and look extension=php_gd2.dll And extension=php_sockets.dll Whether the extension is open;

STEP 2 Look phpInfo() In the displayed content, whether the socket module is enable;;

I checked it once and found it all matched. But mistakes still occur? What's going on?

Later, I found out that it was me phpInfo() The php you see in the cmd window is not the same thing as the php used in the cmd window.

The reason is that I have installed php many times. The previous php registered path in the environment variable of the system. So the previous php is used in the cmd window phpInfo() Shows the current php settings.

The solution is very simple. In the path of the system environment variable, the path pointing to the old Php is changed to the path pointing to the Php being used. In this way, php in cmd and php in browser are the same thing.

There it goes.

5. Its flow is very similar to C language. In fact, it is socket that encapsulates C language.

For more readers interested in PHP related content, please check the topics on this site: "php socket Usage Summary", "php String (string) Usage Summary", "PHP Mathematical Operation Skills Summary", "php Object-Oriented Programming Introduction Tutorial", "PHP Array (Array) Operation Skills Complete Book", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "PHP Network Programming Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: