范例
例子 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";
?>
解决方案 »
- mysql 求和问题
- file_get_contents第二波!正则匹配很欢乐
- 怎么样统计一段字符串中特殊字符的数量呢?
- Warning: mysql_free_result(): supplied argument is not a valid MySQL result reso
- 為什么在ie運行正常,在fixfox運行出錯呢?evt is undefined
- php的用户登录的问题
- 插入数据的时候遇见了Unknown column 'title' in 'field list'
- 不用xmlhttp实现的了吗?
- 全部都配置好了,打开 *.php 还是出现下载界面
- 关于apache解析php的问题
- 我想问一下 如何链接打开文件夹
- 如果遇到这个问题??该怎么办???(有挑战性的问题)
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);
}
?>