gpt4 book ai didi

php - socket_connect()函数中的问题

转载 作者:行者123 更新时间:2023-12-03 11:53:27 25 4
gpt4 key购买 nike

// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// An example list of IP addresses owned by the computer
$sourceips['kevin'] = '127.0.0.1';
$sourceips['madcoder'] = '127.0.0.2';

// Bind the source address
socket_bind($sock, $sourceips['madcoder']);

// Connect to destination address
socket_connect($sock, $sourceips['madcoder'], 80);

// Write
$request = 'GET / HTTP/1.1' . "\r\n" .
'Host: example.com' . "\r\n\r\n";
socket_write($sock, $request);

// Close
socket_close($sock);

我收到一个错误

Warning: socket_connect() [function.socket-connect]: unable to connect [0]: A socket operation was attempted to an unreachable host. in C:\wamp\www\sockert\sockert.php on line 13



谢谢,

最佳答案

嗯,看起来您是直接从the PHP reference page for socket_bind()拿了这个示例。另外,我假设您没有更改任何代码,并且没有将计算机设置为127.0.0.2。这是你的问题。

请记住,示例代码只是示例代码。这是一个基于示例代码的工作示例,该示例代码查看一个随机的Google IP。我还添加了socket_read()函数,以便您可以看到返回的一些数据(当然,它的1024个字节)。

    <?php
// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// An example list of IP addresses owned by the computer
$sourceips['google'] = '74.125.127.103';

// Bind the source address
socket_bind($sock, $sourceips['google']);

// Connect to destination address
socket_connect($sock, $sourceips['google'], 80);

// Write
$request = 'GET / HTTP/1.1' . "\r\n" .
'Host: google.com' . "\r\n\r\n";
socket_write($sock, $request);

// You'll get some HTTP header information here,
// and maybe a bit of HTML for fun!
print socket_read($sock, 1024);

// Close
socket_close($sock);
?>

关于php - socket_connect()函数中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3644944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com