最近在做一个项目,用VC++语言,里面涉及到用线程获取ip包。由于以前用php做项目比较多,故想知道php能否获取ip包?或开发应用程序?放在互联网上的应用程序多好,世界任何一个地方用浏览器就可以访问到。还有我在这种项目中采用了MVC框架,想不通为什么没有人在VC++上开发像framework这样的框架呢,不是同样可以开源吗,把代码公开就可以了。

解决方案 »

  1.   

    应该不可以吧,php触摸不到这些底层的东西
      

  2.   

    php如果要获取ip包,首先得支持多线程,不知道可不可以将ip包用http协议发到服务器端,然后由服务器提供一接口给php调用。
      

  3.   

    不能,这就是PHP没有成为应用最广泛的语言的原因之一吧。
      

  4.   

    刚网上找了一下,原来php也是可以实现多线程的
      

  5.   

    下面的内容是转载过来的
    问题:
    有没有办法在php中实现多线程呢?假设你正在写一个基于多台服务器的php应用,理想的情况时同时向多台服务器发送请求,而不是一台接一台。
    可以实现吗?
    回答:当有人想要实现并发功能时,他们通常会想到用fork或者spawn threads,但是当他们发现php不支持多线程的时候,大概会转换思路去用一些不够好的语言,比如perl。其实的是大多数情况下,你大可不必使用fork或者线程,并且你会得到比用fork或thread更好的性能。假设你要建立一个服务来检查正在运行的n台服务器,以确定他们还在正常运转。你可能会写下面这样的代码:<?php
    $hosts = array("host1.sample.com", "host2.sample.com", "host3.sample.com");
    $timeout = 15;
    $status = array();
    foreach ($hosts as $host) {
    $errno = 0;
    $errstr = "";
    $s = fsockopen($host, 80, $errno, $errstr, $timeout);
    if ($s) {
       $status[$host] = "Connectedn";
       fwrite($s, "HEAD / HTTP/1.0rnHost: $hostrnrn");
       do {
        $data = fread($s, 8192);
        if (strlen($data) == 0) {
        break;
        }
        $status[$host] .= $data;
       } while (true);
       fclose($s);
    } else {
       $status[$host] = "Connection failed: $errno $errstrn";
    }
    }
    print_r($status);
    ?>它运行的很好,但是在fsockopen()分析完hostname并且建立一个成功的连接(或者延时$timeout秒)之前,扩充这段代码来管理大量服务器将耗费很长时间。
    因此我们必须放弃这段代码;我们可以建立异步连接-不需要等待fsockopen返回连接状态。PHP仍然需要解析hostname(所以直接使用ip更加明智),不过将在打开一个连接之后立刻返回,继而我们就可以连接下一台服务器。
    有两种方法可以实现;PHP5中可以使用新增的stream_socket_client()函数直接替换掉fsocketopen()。PHP5之前的版本,你需要自己动手,用sockets扩展解决问题。下面是PHP5中的解决方法:
    <?php
    $hosts = array("host1.sample.com", "host2.sample.com", "host3.sample.com");
    $timeout = 15;
    $status = array();
    $sockets = array();
    /* Initiate connections to all the hosts simultaneously */
    foreach ($hosts as $id => $host) {
    $s = stream_socket_client("$host:80", $errno, $errstr, $timeout,
       STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
    if ($s) {
       $sockets[$id] = $s;
       $status[$id] = "in progress";
    } else {
       $status[$id] = "failed, $errno $errstr";
    }
    }
    /* Now, wait for the results to come back in */
    while (count($sockets)) {
    $read = $write = $sockets;
    /* This is the magic function - explained below */
    $n = stream_select($read, $write, $e = null, $timeout);
    if ($n > 0) {
       /* readable sockets either have data for us, or are failed
       * connection attempts */
       foreach ($read as $r) {
          $id = array_search($r, $sockets);
          $data = fread($r, 8192);
          if (strlen($data) == 0) {
        if ($status[$id] == "in progress") {
         $status[$id] = "failed to connect";
        }
        fclose($r);
        unset($sockets[$id]);
          } else {
        $status[$id] .= $data;
          }
       }
       /* writeable sockets can accept an HTTP request */
       foreach ($write as $w) {
        $id = array_search($w, $sockets);
        fwrite($w, "HEAD / HTTP/1.0rnHost: "
         . $hosts[$id] . "rnrn");
        $status[$id] = "waiting for response";
       }
    } else {
       /* timed out waiting; assume that all hosts associated
       * with $sockets are faulty */
       foreach ($sockets as $id => $s) {
        $status[$id] = "timed out " . $status[$id];
       }
       break;
    }
    }
    foreach ($hosts as $id => $host) {
    echo "Host: $hostn";
    echo "Status: " . $status[$id] . "nn";
    }?>我们用stream_select()等待sockets打开的连接事件。stream_select()调用系统的select(2)函数来工作:前面三个参数是你要使用的streams的数组;你可以对其读取,写入和获取异常(分别针对三个参数)。stream_select()可以通过设置$timeout(秒)参数来等待事件发生-事件发生时,相应的sockets数据将写入你传入的参数。下面是PHP4.1.0之后版本的实现,如果你已经在编译PHP时包含了sockets(ext/sockets)支持,你可以使用根上面类似的代码,只是需要将上面的streams/filesystem函数的功能用ext/sockets函数实现。主要的不同在于我们用下面的函数代替stream_socket_client()来建立连接:
    <?php
    // This value is correct for Linux, other systems have other values
    define('EINPROGRESS', 115);
    function non_blocking_connect($host, $port, &$errno, &$errstr, $timeout) {
    $ip = gethostbyname($host);
    $s = socket_create(AF_INET, SOCK_STREAM, 0);
    if (socket_set_nonblock($s)) {
       $r = @socket_connect($s, $ip, $port);
       if ($r || socket_last_error() == EINPROGRESS) {
        $errno = EINPROGRESS;
        return $s;
       }
    }
    $errno = socket_last_error($s);
    $errstr = socket_strerror($errno);
    socket_close($s);
    return false;
    }
    ?>现在用socket_select()替换掉stream_select(),用socket_read()替换掉fread(),用socket_write()替换掉fwrite(),用socket_close()替换掉fclose()就可以执行脚本了!
    PHP5的先进之处在于,你可以用stream_select()处理几乎所有的stream-例如你可以通过include STDIN用它接收键盘输入并保存进数组,你还可以接收通过proc_open()打开的管道中的数据。
    如果你想让PHP4.3.x自身拥有处理streams的功能,我已经为你准备了一个让fsockopen可以异步工作的patch。不赞成使用该补丁,该补丁不会出现在官方发布的PHP版本中,我在补丁中附带了stream_socket_client()函数的实现,通过它,你可以让你的脚本兼容PHP5。
    附件:
    documentation for stream_select()
    documentation for socket_select()
    patch for PHP 4.3.2 and script to emulate stream_socket_client(). (might work with later 4.3.x versions).------------------------------------经测试,确实为多线程,弄了整个下午,终于弄好了~~~
    $request = array("http://10.1.30.218/test/server.php","http://10.1.30.28/server.php");foreach($request as $r) {
    $temp = parse_url($r);
    $scheme[] = $temp['scheme'];
    $hosts[] = $temp['host'];
    $paths[] = isset($temp['path']) ? $temp['path'] : "" ;
    }//$hosts = array("www.bit.edu.cn");$timeout = 5;
    $status = array();
    $sockets = array();
    // Initiate connections to all the hosts simultaneously 
    foreach ($hosts as $id => $host) {
    $s = stream_socket_client("$host:80", $errno, $errstr, $timeout, STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
    if ($s) {
       $sockets[$id] = $s;
       $status[$id] = "in progress";
    } else {
       $status[$id] = "failed, $errno $errstr";
    }
    }
    //print_r($sockets);print_r($status);
    //*
    // Now, wait for the results to come back in 
    while (count($sockets)) {
    $read = $write = $sockets;
    // This is the magic function - explained below 
    $ret = stream_select($read, $write, $e = null, $timeout);
    if ($ret > 0) {
       // readable sockets either have data for us, or are failed connection attempts 
       foreach ($read as $r) {
          $id = array_search($r, $sockets);
        $data = fread($r, 8192);
        if (strlen($data) == 0) {
         if ($status[$id] == "in progress") {
          $status[$id] = "failed to connect";
         }
         fclose($r);
         unset($sockets[$id]);
        } else {
         if ($status[$id] == "in progress") {
          $status[$id] = $data;
         } else {
          $status[$id] .= $data;
         }
        }
       }
       // writeable sockets can accept an HTTP request
       foreach ($write as $w) {
        $id = array_search($w, $sockets);
        //fwrite($w, "HEAD / HTTP/1.0\r\nHost: " . $hosts[$id] . "\r\n\r\n");
        fwrite($w, "GET /".$paths[$id]." HTTP/1.0\r\nHost: " . $hosts[$id] . "\r\n\r\n");
        //$status[$id] = "waiting for response";
       }} else {
       // timed out waiting; assume that all hosts associated with $sockets are faulty 
       foreach ($sockets as $id => $s) {
        $status[$id] = "timed out\r\n\r\n" . $status[$id];
       }
       break;
    }
    }
    foreach ($hosts as $id => $host) {
    echo "Host: $host\n";
    echo '<pre>'.$status[$id].'</pre>';
    /*
    $pos = strpos($status[$id],"\r\n\r\n");
    $content[$id] = substr($status[$id],$pos);
    $status[$id] = substr($status[$id],0,$pos);
    //echo "Status: " . $status[$id] . "\n\n";
    echo $content[$id] . "\n\n" ;
    */
    }
      

  6.   

    获取IP包是一个时时的过程,而且秀底层,PHP不是不能做,而是挺难。建议还是用VC做吧!
      

  7.   

    The socket extension implements a low-level interface to the socket communication functions based on the popular BSD sockets, providing the possibility to act as a socket server as well as a client. 
    这是php手册里的一句原话,在Socket Functions里面有提供服务器和客户端的代码例子,服务端用do...while(true)循环来接收客户端发来的数据,服务器端也可以发数据到客户端。可惜我用里面的代码测试没有成功。
      

  8.   

    你是要抓包还是要socket通信涅?
      

  9.   

    由一台硬件设备用TCP/IP协议发送数据,服务器端解析这些数据。应该是socket通信吧。