以前经常在C/C++里面遇到socket,没想到PHP也有这样的功能,使用他内部的函数也可以进行远程通信,下面讲一下客户端的实例,包括 create,connect,send,recv, <?php class ClientSocket { var $host; var $port; var $socket; var $errCode; var $errDesc; function __construct($host,$port) { $this->errCode = 0; $this->errDesc = ''; $this->socket = NULL; $this->host = $host; $this->port = $port; $this->init(); } private function close() { socket_close($this->socket); $this->socket = NULL; } function SetErrMsg($code,$desc) { $this->errCode = $code; $this->errDesc = 'Warning:'.$desc.'<br>Message:'.socket_strerror(socket_last_error($this->socket)); } function init() { $this->socket = @socket_create(AF_INET,SOCK_STREAM,SOL_TCP); if($this->socket == NULL) { $this->SetErrMsg(1,'Create socket failed'); $this->sckServer = NULL; return false; } $result = @socket_connect($this->socket, $this->host, $this->port); if($result == false) { $this->SetErrMsg(2,'Connect server failed'); $this->close(); return false; } } function send($sendBuf) { $result = @socket_write($this->socket,$sendBuf,strlen($sendBuf)); if($result == false) { $this->SetErrMsg(3,'Send data failed'); return false; } return true; } function recv(){ $recv=''; while($recvBuf = @socket_read($this->socket,8129)){ $recv .= $recvBuf; } return $recv; } } $header = "GET / HTTP/1.0/r/n"; $header.= "Host: 127.0.0.1/r/n"; $header.= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; $haeder.= "Accept: */*/r/n"; $header.= "Cookie: /r/n"; $header.= "Accept-Language: zh-cn,zh;/r/n"; $header.= "Connection: Close/r/n/r/n"; $client = new ClientSocket('127.0.0.1',80); if($client->errCode!=0){ echo $client->errDesc; exit; } $client->send($header); $html = $client->recv(); $client->close(); echo $html; ?> 新手小刀,还望指点。。。。