gpt4 book ai didi

php - 与本地主机的套接字连接超时

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:45 24 4
gpt4 key购买 nike

所以我正在尝试获得一个托管网站(我们称之为 online.com),以便能够从我的 Apache 本地 (WAMP) 获取一些文本文件。

但是,当我运行代码时,online.com 会加载一段时间,然后出现错误 100(超时)。我不知道为什么。

我正在使用套接字来执行此操作。我已经在我的 PC 的防火墙上将 online.com 的 IP 列入白名单。我已经将监听端口添加到 Apache 服务器。我已经完成了端口转发(尽管我不能 100% 确定我是否正确完成了)。

以下是上述内容的更具象征意义的表示。

端口转发

online.com's IP = X, port = 80
local host's IP = (used ipconfig) IPV4 Address, port = Y

Apache

Listening on port Y

白名单

Added IP X

当我在 online.com 上运行 php 脚本时

if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

// Connect socket to remote server


**if (!socket_connect($sock, 'IP X', PORTY))**
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [$errorcode] $errormsg \n");
}

echo "Connection established \n";
$message = "GET / PRCC /HTTP/1.1 \r\n\r\n";

// Send the message to the server

if (!socket_send($sock, $message, strlen($message) , 0))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}

echo "Message send successfully \n";
$buf = "";

// Now receive reply from server

if (socket_recv($sock, $buf, 2045, MSG_WAITALL) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}

// print the received message

echo $buf . "this is the buffer";
socket_close($sock);

if(!socket_connect($sock , 'IP X' , PORT Y)) 是问题的原因。

如有任何可能出错的建议,我们将不胜感激。

最佳答案

为什么要使用套接字?只需使用 file_get_contents() 尝试向您的家庭 IP 发出常规 HTTP 请求。

例子,

<?php

$text_file1 = file_get_contents("http://YOUR_IP:PORT/PRCC");
// so on..

关于php - 与本地主机的套接字连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38401604/

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