Home  >  Article  >  Backend Development  >  php socket如何测试

php socket如何测试

WBOY
WBOYOriginal
2016-06-13 12:02:081051browse

php socket怎么测试

本帖最后由 d6965921d 于 2014-07-13 09:57:41 编辑
<?php<br />error_reporting(E_ALL);<br />set_time_limit(0);<br />//ob_implicit_flush();<br /><br />$address = '127.0.0.1';<br />$port = 10005;<br />//创建端口<br />if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {<br />	echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";<br />}<br /><br />//绑定<br />if (socket_bind($sock, $address, $port) === false) {<br />	echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";<br />}<br /><br />//监听<br />if (socket_listen($sock, 5) === false) {<br />	echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";<br />}<br /><br />do {<br />	//得到一个链接<br />	if (($msgsock = socket_accept($sock)) === false) {<br />		echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";<br />		break;<br />	}<br />	//welcome  发送到客户端<br />	$msg = "<font color='red'>server send:welcome</font><br/>";<br />	socket_write($msgsock, $msg, strlen($msg));<br />	echo 'read client message\n';<br />	$buf = socket_read($msgsock, 8192);<br />	$talkback = "received message:$buf\n";<br />	echo $talkback;<br />	if (false === socket_write($msgsock, $talkback, strlen($talkback))) {<br />		echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n";<br />	} else {<br />		echo 'send success';<br />	}<br />	socket_close($msgsock);<br />} while(true);<br />//关闭socket<br />socket_close($sock);<br /><br /><br />?>

这个代码怎么运行呢 我打开页面一直打不开。。
这个我写好的socket 我怎么测试效果。。。。。。。。我想测试成功再写socket客户端  以前http直接输入网址就知道了。。。socket还真不知道怎么测试呢。。
------解决方案--------------------
换个端口试试
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn