php socket怎么测试

WBOY
Release: 2016-06-23 13:53:53
Original
1117 people have browsed it

<?phperror_reporting(E_ALL);set_time_limit(0);//ob_implicit_flush();$address = '127.0.0.1';$port = 10005;//创建端口if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {	echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";}//绑定if (socket_bind($sock, $address, $port) === false) {	echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}//监听if (socket_listen($sock, 5) === false) {	echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}do {	//得到一个链接	if (($msgsock = socket_accept($sock)) === false) {		echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";		break;	}	//welcome  发送到客户端	$msg = "<font color='red'>server send:welcome</font><br/>";	socket_write($msgsock, $msg, strlen($msg));	echo 'read client message\n';	$buf = socket_read($msgsock, 8192);	$talkback = "received message:$buf\n";	echo $talkback;	if (false === socket_write($msgsock, $talkback, strlen($talkback))) {		echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n";	} else {		echo 'send success';	}	socket_close($msgsock);} while(true);//关闭socketsocket_close($sock);?>
Copy after login

这个代码怎么运行呢 我打开页面一直打不开。。
这个我写好的socket 我怎么测试效果。。。。。。。。我想测试成功再写socket客户端 以前http直接输入网址就知道了。。。socket还真不知道怎么测试呢。。


回复讨论(解决方案)

加入你这个文件叫 socket_test.php

在终端运行

php socket_test.php
Copy after login
Copy after login
即可

socket 要在终端测试!

加入你这个文件叫 socket_test.php

在终端运行

php socket_test.php
Copy after login
Copy after login
 即可



Warning: socket_bind(): unable to bind address [48]: Address already in use in /Applications/XAMPP/xamppfiles/htdocs/socket/index.php on line 14
socket_bind() failed :reason:Address already in use
直接报错。。

换个端口试试

现在就是运行了 直接就这样了 这样就代表好了??怎么测试呢?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template