范例
例子 1. Socket example: Simple TCP/IP serverThis example shows a simple talkback server. Change the address and port variables to suit your setup and execute. You may then connect to the server with a command similar to: telnet 192.168.1.53 10000 (where the address and port match your setup). Anything you type will then be output on the server side, and echoed back to you. To disconnect, enter 'quit'. #!/usr/local/bin/php -q
<?php
error_reporting (E_ALL);/* Allow the script to hang around waiting for connections. */
set_time_limit (0);/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush ();$address = '192.168.1.53';
$port = 10000;if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
}if (($ret = socket_bind ($sock, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
}if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
}do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg)); do {
if (FALSE === ($buf = socket_read ($msgsock, 2048))) {
echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\n";
break 2;
}
if (!$buf = trim ($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close ($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write ($msgsock, $talkback, strlen ($talkback));
echo "$buf\n";
} while (true);
socket_close ($msgsock);
} while (true);socket_close ($sock);
?>
例子 2. Socket example: Simple TCP/IP clientThis example shows a simple, one-shot HTTP client. It simply connects to a page, submits a HEAD request, echoes the reply, and exits. <?php
error_reporting (E_ALL);echo "<h2>TCP/IP Connection</h2>\n";/* Get the port for the WWW service. */
$service_port = getservbyname ('www', 'tcp');/* Get the IP address for the target host. */
$address = gethostbyname ('www.example.com');/* Create a TCP/IP socket. */
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($socket) . "\n";
} else {
echo "OK.\n";
}echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
} else {
echo "OK.\n";
}$in = "HEAD / HTTP/1.0\r\n\r\n";
$out = '';echo "Sending HTTP HEAD request...";
socket_write ($socket, $in, strlen ($in));
echo "OK.\n";echo "Reading response:\n\n";
while ($out = socket_read ($socket, 2048)) {
echo $out;
}echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";
?>
例子 1. Socket example: Simple TCP/IP serverThis example shows a simple talkback server. Change the address and port variables to suit your setup and execute. You may then connect to the server with a command similar to: telnet 192.168.1.53 10000 (where the address and port match your setup). Anything you type will then be output on the server side, and echoed back to you. To disconnect, enter 'quit'. #!/usr/local/bin/php -q
<?php
error_reporting (E_ALL);/* Allow the script to hang around waiting for connections. */
set_time_limit (0);/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush ();$address = '192.168.1.53';
$port = 10000;if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
}if (($ret = socket_bind ($sock, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
}if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
}do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg)); do {
if (FALSE === ($buf = socket_read ($msgsock, 2048))) {
echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\n";
break 2;
}
if (!$buf = trim ($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close ($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write ($msgsock, $talkback, strlen ($talkback));
echo "$buf\n";
} while (true);
socket_close ($msgsock);
} while (true);socket_close ($sock);
?>
例子 2. Socket example: Simple TCP/IP clientThis example shows a simple, one-shot HTTP client. It simply connects to a page, submits a HEAD request, echoes the reply, and exits. <?php
error_reporting (E_ALL);echo "<h2>TCP/IP Connection</h2>\n";/* Get the port for the WWW service. */
$service_port = getservbyname ('www', 'tcp');/* Get the IP address for the target host. */
$address = gethostbyname ('www.example.com');/* Create a TCP/IP socket. */
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($socket) . "\n";
} else {
echo "OK.\n";
}echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
} else {
echo "OK.\n";
}$in = "HEAD / HTTP/1.0\r\n\r\n";
$out = '';echo "Sending HTTP HEAD request...";
socket_write ($socket, $in, strlen ($in));
echo "OK.\n";echo "Reading response:\n\n";
while ($out = socket_read ($socket, 2048)) {
echo $out;
}echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";
?>
解决方案 »
- string(381) "array ( 'title' => '这个字符怎么转成数组',)
- 采集获取,关于正则表达式的多条结果,只获取1条
- PHP可以编写WEB应用程序吗?
- php的按装和iis的配置。
- 如何算出文章发布日期(已知)和当前日期的天数差距?
- 我要怎样才能在本窗口内打开一个新的窗口???????
- php(yii框架) facebook api 具体流程
- 这个问题很难,我的代码哪有问题么,总是显示不出mysql查询的结果
- xdebug和zend debug安装问题
- php 与 sap 接口通信问题
- 我想问一下 如何链接打开文件夹
- 如果遇到这个问题??该怎么办???(有挑战性的问题)
Warning: socket_bind() unable to bind address [0]: 在其上下文中,该请求的地址无效。 in H:\aircdphp\socket1.php on line 18Warning: socket_listen() unable to listen on socket [0]: 提供了一个无效的参数。 in
H:\aircdphp\socket1.php on line 22我用socket_connect和socket_send是可以的,不过在socket_read的时候就没有响应了不知道为什么
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>