Chinaunix首页 | 论坛 | 博客
  • 博客访问: 122903
  • 博文数量: 16
  • 博客积分: 383
  • 博客等级: 一等列兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-14 11:43
文章分类

全部博文(16)

文章存档

2014年(1)

2012年(3)

2011年(12)

分类: 系统运维

2011-03-25 16:10:05

server端代码:

  1. <?php

  2. $addr = "127.0.0.1";
  3. $port = 1003;

  4. $remoteIP = "";
  5. $remotePort = "";

  6. $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
  7. if($socket < 0) {
  8.         echo "Socket create:".$socket_strerror($socket)."\n";
  9.         exit;
  10. }
  11. if ( ($ret = socket_bind($socket, $addr, $port)) < 0 ) {
  12.     echo "socket bind:".socket_strerror()."\n";
  13.     exit;
  14. }
  15. if ( ($ret = socket_listen($socket, 5)) < 0 ) {
  16.     echo "socket listen:".socket_strerror()."\n";
  17.     exit;
  18. }

  19. socket_set_nonblock($socket);

  20. echo "Wainting fro a connection:\n";

  21. $clients = array($socket);
  22. while(true) {
  23.     $read = $clients;
  24.     if(socket_select($read, $writes=NULL, $execs=NULL, 3) < 1){
  25.         //echo ".";

  26.         continue;
  27.     }
  28.     if(in_array($socket,$read)) {
  29.         $clients[] = $newsock = socket_accept($socket);
  30.         socket_set_nonblock($newsock);
  31.         socket_getpeername($newsock,&$remoteIP,&$remotePort);
  32.         $key = array_search($socket,$read);
  33.         unset($read[$key]);
  34.         $msg = "Server: $addr\r\n".
  35.                 "Notice:Welcome the user from ".$remoteIP.":".$remotePort.".\r\n";
  36.         echo $remoteIP.":".$remotePort." conected in.\r\n";
  37.         socket_write($newsock,$msg);
  38.     }

  39.     foreach($read as $read_sock) {
  40.         $data = @socket_read($read_sock,1024,PHP_BINARY_READ);
  41.         if($data == false) {
  42.             $key = array_search($read_sock,$clients);
  43.             print_r($clients[$key]);
  44.             unset($clients[$key]);
  45.             socket_close($read_sock);
  46.             echo "Client disconnected.\n";
  47.             continue;
  48.         }
  49.         
  50.         $command = strtoupper ($data);
  51.         
  52.         switch ($command)
  53.         {
  54.             case "LIST":
  55.                 $writer = print_r($clients,1);
  56.                 break;
  57.             case "HELLO":
  58.                 $writer = "Hello Everybody!";
  59.                 break;
  60.             case "QUIT":
  61.                 $writer = "Bye-Bye";
  62.                 break;
  63.             case "SLEEP":
  64.                 sleep(30);
  65.                 $writer = "sleeped 10 seconds!";
  66.                 break;
  67.             case "HELP":
  68.                 $writer = "HELLO\tQUIT\tHELP";
  69.                 break;
  70.             default:
  71.                 $writer = "Error Command!\r\nType help for help message!";
  72.         }

  73.         socket_write($read_sock, $writer);
  74.         //exit;

  75.     }
  76. }
  77. socket_close($socket);

  78. ?>

 

client端源代码:

  1. <?php
  2. // Client

  3. // 设置错误处理

  4. error_reporting (E_ALL);

  5. $ip = "127.0.0.1"; // IP 地址

  6. $port = 1003; // 端口号


  7. $socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // 创建一个SOCKET

  8. if ($socket)
  9.     echo "socket_create() successed!\n";
  10. else
  11.     echo "socket_create() failed:".socket_strerror ($socket)."\n";

  12. $conn = socket_connect ($socket, $ip, $port); // 建立SOCKET的连接

  13. if ($conn)
  14.     echo "Success to connection![".$ip.":".$port."]\n";
  15. else
  16.     echo "socket_connect() failed:".socket_strerror ($conn)."\n";

  17. echo socket_read ($socket, 1024);

  18. $stdin = fopen ('php://stdin', 'r');
  19. while (true)
  20. {
  21.     $command = trim (fgets ($stdin, 1024));
  22.     socket_write ($socket, $command, strlen ($command));
  23.     $msg = trim (socket_read ($socket, 1024));
  24.     echo $msg."\n";
  25.     if ($msg == "Bye-Bye")
  26.         break;
  27. }
  28. fclose ($stdin);
  29. socket_close ($socket);
  30. ?>
阅读(1217) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~