php Telnet IRC Server

From NAT, 7 Years ago, written in PHP, viewed 786 times.
URL https://code.nat.moe/view/9db883d4 Embed
Download Paste or View Raw
  1. <?php
  2. if(!extension_loaded('sockets')){
  3.   die('[ERROR}The sockets extension is not loaded!');
  4. }
  5. const PORT=23;
  6. $socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP) or die('[ERROR]Socket create error!');
  7. socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,1);
  8. socket_bind($socket,0,PORT);
  9. socket_listen($socket);
  10. echo 'listen on port '.PORT.'...'.PHP_EOL;
  11. $clients=array($socket);
  12. while(TRUE){
  13.     $read=$clients;
  14.     $write=$except=array();
  15.     if(socket_select($read,$write,$except,0)<1){
  16.         continue;
  17.     }
  18.     if(in_array($socket,$read)){
  19.         $clients[]=$newsocket=socket_accept($socket);
  20.         socket_write($newsocket,"Welcome to NAT-IRC!\nThere are ".(count($clients)-1)." client here\n");
  21.         socket_getpeername($newsocket,$ip);
  22.         echo "[INFORM]New client connected:$ip\n";
  23.         $key=array_search($newsocket,$read);
  24.         unset($read[$key]);
  25.     }
  26.     foreach($read as $read_socket){
  27.         $data=@socket_read($read_socket,1024,PHP_NORMAL_READ);
  28.         if($data===false){
  29.             $key=array_search($read_socket,$clients);
  30.             unset($clients[$key]);
  31.             echo "[INFORM]Client disconnectd.\n";
  32.             continue;
  33.         }
  34.         $data=trim($data);
  35.         if(!empty($data)){
  36.             foreach($clients as $write_socket){
  37.                 if($write_socket==$socket||$write_socket==$read_socket){
  38.                     continue;
  39.                 }
  40.                 socket_write($write_socket,"$data\n");
  41.             }
  42.         }
  43.     }
  44. }
  45. socket_close($socket);
  46. ?>

Reply to "php Telnet IRC Server"

Here you can reply to the paste above

captcha