Heim  >  Artikel  >  Backend-Entwicklung  >  php socket如何发送

php socket如何发送

WBOY
WBOYOriginal
2016-06-23 13:53:451313Durchsuche

<?php date_default_timezone_set("PRC"); ?><?phpclass WS {	var $master;	var $sockets = array();	var $debug = false;	var $handshake = false;	function __construct($address, $port){		$this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed");		socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1)  or die("socket_option() failed");		socket_bind($this->master, $address, $port)                    or die("socket_bind() failed");		socket_listen($this->master,20)                                or die("socket_listen() failed");				$this->sockets[] = $this->master;		$this->say("Server Started : ".date('Y-m-d H:i:s'));		$this->say("Listening on   : ".$address." port ".$port);		$this->say("Master socket  : ".$this->master."\n");				while(true){			$socketArr = $this->sockets;			$write = NULL;			$except = NULL;			socket_select($socketArr, $write, $except, NULL);  //自动选择来消息的socket 如果是握手 自动选择主机			foreach ($socketArr as $socket){				if ($socket == $this->master){  //主机					$client = socket_accept($this->master);					if ($client < 0){						$this->log("socket_accept() failed");						continue;					} else{						$this->connect($client);					}				} else {					$this->log("^^^^");					$bytes = @socket_recv($socket,$buffer,2048,0);					$this->log("^^^^");					if ($bytes == 0){						$this->disConnect($socket);					}					else{						if (!$this->handshake){							$this->doHandShake($socket, $buffer);						}						else{							$buffer = $this->decode($buffer);							$this->send($socket, $buffer); 						}					}				}			}		}	}		function send($client, $msg){		$this->log("> " . $msg);		$msg = $this->frame($msg);		socket_write($client, $msg, strlen($msg));		$this->log("! " . strlen($msg));	}	function connect($socket){		array_push($this->sockets, $socket);		$this->say("\n" . $socket . " CONNECTED!");		$this->say(date("Y-n-d H:i:s"));	}	function disConnect($socket){		$index = array_search($socket, $this->sockets);		socket_close($socket);		$this->say($socket . " DISCONNECTED!");		if ($index >= 0){			array_splice($this->sockets, $index, 1); 		}	}	function doHandShake($socket, $buffer){		$this->log("\nRequesting handshake...");		$this->log($buffer);		list($resource, $host, $origin, $key) = $this->getHeaders($buffer);		$this->log("Handshaking...");		$upgrade  = "HTTP/1.1 101 Switching Protocol\r\n" .					"Upgrade: websocket\r\n" .					"Connection: Upgrade\r\n" .					"Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n\r\n";  //必须以两个回车结尾		$this->log($upgrade);		$sent = socket_write($socket, $upgrade, strlen($upgrade));		$this->handshake=true;		$this->log("Done handshaking...");		return true;	}	function getHeaders($req){		$r = $h = $o = $key = null;		if (preg_match("/GET (.*) HTTP/"              ,$req,$match)) { $r = $match[1]; }		if (preg_match("/Host: (.*)\r\n/"             ,$req,$match)) { $h = $match[1]; }		if (preg_match("/Origin: (.*)\r\n/"           ,$req,$match)) { $o = $match[1]; }		if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)) { $key = $match[1]; }		return array($r, $h, $o, $key);	}	function calcKey($key){		//基于websocket version 13		$accept = base64_encode(sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));		return $accept;	}	function decode($buffer) {		$len = $masks = $data = $decoded = null;		$len = ord($buffer[1]) & 127;		if ($len === 126) {			$masks = substr($buffer, 4, 4);			$data = substr($buffer, 8);		} 		else if ($len === 127) {			$masks = substr($buffer, 10, 4);			$data = substr($buffer, 14);		} 		else {			$masks = substr($buffer, 2, 4);			$data = substr($buffer, 6);		}		for ($index = 0; $index < strlen($data); $index++) {			$decoded .= $data[$index] ^ $masks[$index % 4];		}		return $decoded;	}	function frame($s){		$a = str_split($s, 125);		if (count($a) == 1){			return "\x81" . chr(strlen($a[0])) . $a[0];		}		$ns = "";		foreach ($a as $o){			$ns .= "\x81" . chr(strlen($o)) . $o;		}		return $ns;	}		function say($msg = ""){		echo $msg . "\n";	}	function log($msg = ""){		if ($this->debug){			echo $msg . "\n";		} 	}}	$wwss=new WS('localhost', 4000);

如果我要写发给客户端 send 第一个参数怎么写


回复讨论(解决方案)

$client = socket_accept($this->master);//27行

36 行的 $bytes = @socket_recv($socket,$buffer,2048,0); 是接收客户端发送的数据
47 当的 $this->send($socket, $buffer); 是向客户端发送数据

这是一个测试程序,只是原样发回客户端发送的数据。离实用还远呢
你需要写一个程序,分析处理用户发送的数据
依据处理的结果,返回相应的数据
这段程序要在 46、47 行之间调用

36 行的 $bytes = @socket_recv($socket,$buffer,2048,0); 是接收客户端发送的数据
47 当的 $this->send($socket, $buffer); 是向客户端发送数据

这是一个测试程序,只是原样发回客户端发送的数据。离实用还远呢
你需要写一个程序,分析处理用户发送的数据
依据处理的结果,返回相应的数据
这段程序要在 46、47 行之间调用


有没有实用点得代码 我找了1天了 没看到靠谱的 各种问题    只要 客户端发送a 然后服务端接收到数据 如果是a 那就原样返回a

你的这个程序不就是这样的吗?

问题是你的客户端

你的这个程序不就是这样的吗?

问题是你的客户端

客户端我用c++写 写好的 我现在在写服务端

那你下载一个  《Android和PHP开发最佳实践》源码. 下载频道就有
都出书了,应该没有问题了吧。
这里有配置说明  http://blog.csdn.net/binyao02123202/article/details/11761931

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn