gpt4 book ai didi

Dart 服务器拒绝连接本地网络

转载 作者:行者123 更新时间:2023-12-05 00:47:11 25 4
gpt4 key购买 nike

我在 Mac Mavericks 和 Ubuntu Linux 上运行 Dart。我才刚刚开始;而且,所有示例都无法通过我的本地网络运行。当我在 Ubuntu 上安装服务器并尝试使用 mac 访问时,没有任何反应。当我尝试从 Ubunutu 访问时,我收到“java.net Connection Refused error”同样适用于两台机器上的 Curl(连接被拒绝)。所有示例都作为本地主机的客户端/服务器运行良好。客户端应用程序或浏览器都无法连接。所有其他服务器/客户端在家庭网络中正常工作。防火墙没问题,我仔细检查了端口号。所需端口上没有冲突的服务器。我尝试了来自 Dart 站点和其他站点的大约 5 个不同的脚本,每个脚本都有一个 CORS 函数,但没有任何连接,包括下面的示例,SimpleServer-Github

示例代码

/* A simple web server that responds to **ALL** GET requests by returning
* the contents of data.json file, and responds to ALL **POST** requests
* by overwriting the contents of the data.json file
*
* Browse to it using http://localhost:8080
*
* Provides CORS headers, so can be accessed from any other page
*/
import 'dart:io';

final HOST = "127.0.0.1"; // eg: localhost
final PORT = 8080;
final DATA_FILE = "data.json";

void main() {
HttpServer.bind(HOST, PORT).then((server) {
server.listen((HttpRequest request) {
switch (request.method) {
case "GET":
handleGet(request);
break;
case "POST":
handlePost(request);
break;
case "OPTIONS":
handleOptions(request);
break;
default: defaultHandler(request);
}
},
onError: printError);

print("Listening for GET and POST on http://$HOST:$PORT");
},
onError: printError);
}

/**
* Handle GET requests by reading the contents of data.json
* and returning it to the client
*/
void handleGet(HttpRequest req) {
HttpResponse res = req.response;
print("${req.method}: ${req.uri.path}");
addCorsHeaders(res);

var file = new File(DATA_FILE);
if (file.existsSync()) {
res.headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
file.readAsBytes().asStream().pipe(res); // automatically close output stream
}
else {
var err = "Could not find file: $DATA_FILE";
res.write(err);
res.close();
}

}

/**
* Handle POST requests by overwriting the contents of data.json
* Return the same set of data back to the client.
*/
void handlePost(HttpRequest req) {
HttpResponse res = req.response;
print("${req.method}: ${req.uri.path}");

addCorsHeaders(res);

req.listen((List<int> buffer) {
var file = new File(DATA_FILE);
var ioSink = file.openWrite(); // save the data to the file
ioSink.add(buffer);
ioSink.close();

// return the same results back to the client
res.add(buffer);
res.close();
},
onError: printError);
}

/**
* Add Cross-site headers to enable accessing this server from pages
* not served by this server
*
* See: http://www.html5rocks.com/en/tutorials/cors/
* and http://enable-cors.org/server.html
*/
void addCorsHeaders(HttpResponse res) {
res.headers.add("Access-Control-Allow-Origin", "*");
res.headers.add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res.headers.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}

void handleOptions(HttpRequest req) {
HttpResponse res = req.response;
addCorsHeaders(res);
print("${req.method}: ${req.uri.path}");
res.statusCode = HttpStatus.NO_CONTENT;
res.close();
}

void defaultHandler(HttpRequest req) {
HttpResponse res = req.response;
addCorsHeaders(res);
res.statusCode = HttpStatus.NOT_FOUND;
res.write("Not found: ${req.method}, ${req.uri.path}");
res.close();
}

void printError(error) => print(error);

数据.json

{
"language":"DART",
"targets":["dartium","javascript","Android?"],
"website":{
"homepage":"www.dartlang.org",
"api":"api.dartlang.org"
}

最佳答案

我认为这是因为 Dart 服务器被配置为只监听本地主机上的客户端。您需要让它监听任何 IPv4/v6 客户端,以便通过网络接收来自服务器的任何响应。

将行 final HOST = "127.0.0.1"; 更改为 final HOST = InternetAddress.ANY_IP_V4;final HOST = InternetAddress.ANY_IP_V6;

它应该可以正常工作。

关于Dart 服务器拒绝连接本地网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23691105/

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