亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 7078 | 回復(fù): 11
打印 上一主題 下一主題

[SOCKET] PHP 中如何使用ICMP? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2005-12-03 16:58 |只看該作者 |倒序?yàn)g覽
只知道用socket_create創(chuàng)建一個,用socket_connect嘗試連接。

使用TCP的80可以了,但是使用ICMP的方法卻不會。

在php.net上看了手冊了沒有找到,朋友們幫忙呀。

[ 本帖最后由 HonestQiao 于 2005-12-3 19:11 編輯 ]

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2005-12-03 18:54 |只看該作者
只能exec("ping")吧

論壇徽章:
1
技術(shù)圖書徽章
日期:2013-12-05 23:25:45
3 [報(bào)告]
發(fā)表于 2005-12-03 19:10 |只看該作者
http://www.php.net/manual/zh/ref.sockets.php

  1. <?php

  2. class Net_Ping
  3. {
  4.   var $icmp_socket;
  5.   var $request;
  6.   var $request_len;
  7.   var $reply;
  8.   var $errstr;
  9.   var $time;
  10.   var $timer_start_time;
  11.   function Net_Ping()
  12.   {
  13.    $this->icmp_socket = socket_create(AF_INET, SOCK_RAW, 1);
  14.    socket_set_block($this->icmp_socket);
  15.   }

  16.   function ip_checksum($data)
  17.   {
  18.      for($i=0;$i<strlen($data);$i += 2)
  19.      {
  20.          if($data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]);
  21.          else $bits = unpack('C*',$data[$i]);
  22.          $sum += $bits[1];
  23.      }
  24.    
  25.      while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16);
  26.      $checksum = pack('n1',~$sum);
  27.      return $checksum;
  28.   }

  29.   function start_time()
  30.   {
  31.    $this->timer_start_time = microtime();
  32.   }

  33.   function get_time($acc=2)
  34.   {
  35.    // format start time
  36.    $start_time = explode (" ", $this->timer_start_time);
  37.    $start_time = $start_time[1] + $start_time[0];
  38.    // get and format end time
  39.    $end_time = explode (" ", microtime());
  40.    $end_time = $end_time[1] + $end_time[0];
  41.    return number_format ($end_time - $start_time, $acc);
  42.   }

  43.   function Build_Packet()
  44.   {
  45.    $data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data
  46.    $type = "\x08"; // 8 echo message; 0 echo reply message
  47.    $code = "\x00"; // always 0 for this program
  48.    $chksm = "\x00\x00"; // generate checksum for icmp request
  49.    $id = "\x00\x00"; // we will have to work with this later
  50.    $sqn = "\x00\x00"; // we will have to work with this later

  51.    // now we need to change the checksum to the real checksum
  52.    $chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

  53.    // now lets build the actual icmp packet
  54.    $this->request = $type.$code.$chksm.$id.$sqn.$data;
  55.    $this->request_len = strlen($this->request);
  56.   }

  57.   function Ping($dst_addr,$timeout=5,$percision=3)
  58.   {
  59.    // lets catch dumb people
  60.    if ((int)$timeout <= 0) $timeout=5;
  61.    if ((int)$percision <= 0) $percision=3;
  62.   
  63.    // set the timeout
  64.    socket_set_option($this->icmp_socket,
  65.      SOL_SOCKET,  // socket level
  66.      SO_RCVTIMEO, // timeout option
  67.      array(
  68.        "sec"=>$timeout, // Timeout in seconds
  69.        "usec"=>0  // I assume timeout in microseconds
  70.        )
  71.      );

  72.    if ($dst_addr)
  73.    {
  74.      if (@socket_connect($this->icmp_socket, $dst_addr, NULL))
  75.      {
  76.    
  77.      } else {
  78.        $this->errstr = "Cannot connect to $dst_addr";
  79.        return FALSE;
  80.      }
  81.      $this->Build_Packet();
  82.      $this->start_time();
  83.      socket_write($this->icmp_socket, $this->request, $this->request_len);
  84.      if (@socket_recv($this->icmp_socket, &$this->reply, 256, 0))
  85.      {
  86.        $this->time = $this->get_time($percision);
  87.        return $this->time;
  88.      } else {
  89.        $this->errstr = "Timed out";
  90.        return FALSE;
  91.      }
  92.    } else {
  93.      $this->errstr = "Destination address not specified";
  94.      return FALSE;
  95.    }
  96.   }
  97. }

  98. $ping = new Net_Ping;
  99. $ping->ping("www.google.ca");

  100. if ($ping->time)
  101.   echo "Time: ".$ping->time;
  102. else
  103.   echo $ping->errstr;

  104. ?>
復(fù)制代碼

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2005-12-05 09:25 |只看該作者
版主的方法我試過了。
有錯誤報(bào)告:
[function.socket-create]: Unable to create socket [0]: 以一種訪問權(quán)限不允許的方式做了一個訪問套接字的嘗試。


CU沒有人做過這事嗎?

論壇徽章:
1
技術(shù)圖書徽章
日期:2013-12-05 23:25:45
5 [報(bào)告]
發(fā)表于 2005-12-05 09:44 |只看該作者
原帖由 78020281 于 2005-12-5 09:25 發(fā)表
版主的方法我試過了。
有錯誤報(bào)告:
[function.socket-create]: Unable to create socket [0]: 以一種訪問權(quán)限不允許的方式做了一個訪問套接字的嘗試。


CU沒有人做過這事嗎?



我在Windows測試都可以的啊。

你在最開頭加上error_reporting(E_ALL);

記住,如果是windows,需要在php.ini把socket的dll打開的哦。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2005-12-05 15:50 |只看該作者
我是直接復(fù)制過去運(yùn)行的,不知道行不行?


sockets.dll 是打開的。

論壇徽章:
1
技術(shù)圖書徽章
日期:2013-12-05 23:25:45
7 [報(bào)告]
發(fā)表于 2005-12-05 15:52 |只看該作者
原帖由 78020281 于 2005-12-5 15:50 發(fā)表
我是直接復(fù)制過去運(yùn)行的,不知道行不行?


sockets.dll 是打開的。



我一開始也是出現(xiàn)立的錯誤。

后來我加了最開投的那句話,看到了錯誤提示說不支持,我就打開socket,他就好了的。

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2005-12-07 16:06 |只看該作者
我試上面代碼.

php.ini中已經(jīng) sockets.use_system_read = On

執(zhí)行頁面時出現(xiàn)錯誤提示:
Warning: socket_create() Unable to create socket [1]: Operation not permitted in /www/test/Net_Ping.php on line 14

Warning: socket_set_block() expects parameter 1 to be resource, boolean given in /www/test/Net_Ping.php on line 15

Warning: socket_set_option() expects parameter 1 to be resource, boolean given in /www/test/Net_Ping.php on line 79
Cannot connect to www.google.com

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2005-12-07 16:13 |只看該作者
呵呵, 因?yàn)?SOCKET_RAW 也就是原始套接字必須 root 才能建立的.

所以 web 運(yùn)行的話并不一定是 root (管理員權(quán)限), 版主可能是在 cli 模式下測試

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2005-12-07 16:23 |只看該作者
如果我不提升web的權(quán)限有沒有辦法解決這個問題 .?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP