使用php的socket发送tcp请求
来源:互联网 发布:gopro中文软件 编辑:程序博客网 时间:2024/05/16 14:51
Of course.
in PHP manual :
Socket example: Simple TCP/IP client
This 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, SOL_TCP);
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.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\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";
?>
in PHP manual :
Socket example: Simple TCP/IP client
This 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, SOL_TCP);
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.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\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";
?>
0 0
- 使用php的socket发送tcp请求
- 使用SOCKET发送HTTP请求
- httclient使用socket发送请求
- 使用Socket发送Http请求
- 使用Socket发送Http请求
- [php]模拟socket发送GET,POST请求
- 使用JavaSocket编写发送TCP请求的工具类
- 使用JavaSocket编写发送TCP请求的工具类
- 使用JavaSocket编写发送TCP请求的工具类
- 练习使用 socket 发送简单的 HTTP GET 请求
- PHP使用socket发送邮件
- tcp socket客户端发送请求连接http服务
- 使用socket发送http请求(GET/POST)
- 使用socket发送http请求(GET/POST)
- 使用Socket发送GET/POST请求
- 使用socket发送http请求(GET/POST)
- php发送请求的方式
- tcp socket的发送与接收缓冲区
- 辛星浅析php正则匹配中的utf字符串
- MYSQL学习2数据库-表-字段
- MCU GPIO模拟spi
- centos killall安装
- iOS开发常见错误
- 使用php的socket发送tcp请求
- 推荐个bootstrap的icon图标网站
- 顺时针打印矩阵
- 进程的同步与通信,进程与线程同步的区别,进程与线程通信的区别
- 快乐之道:游戏设计的黄金法则
- java集合框架知识总结
- sublime text3空格和tab的显示
- 网络通信协议的基本知识
- 辛星浅析字符模式匹配中的u模式修正符