我想在我的计算机和运行在replit上的服务器之间建立一个聊天服务器,但我无法连接到replit服务器(当我在设备上运行服务器时它有效,因此代码似乎有效)
以下是在replit上运行的服务器端代码:use std::io::{self, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::str;
use std::thread;
fn handle_client(mut stream: TcpStream) -> std::io::Result<()> {
let mut data = [0; 4096];
loop {
let len = stream.read(&mut data)?;
let text = str::from_utf8(&data[..len]).unwrap();
println!("Received: {}", text);
println!("What do you want to send: ");
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
stream.write_all(input.as_bytes())?;
}
}
fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("0.0.0.0:6969")?;
for stream in listener.incoming() {
let stream = stream?;
println!("New connection: {}", stream.peer_addr()?);
thread::spawn(move || handle_client(stream));
}
Ok(())
}
下面是客户端代码:
use std::io::{self, Read, Write};
use std::net::TcpStream;
use std::str;
fn main() -> std::io::Result<()> {
let mut stream = TcpStream::connect("IncompleteHelpfulServer.smartflow.repl.co:6969")?;
loop {
println!("What do you want to send: ");
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
stream.write_all(input.as_bytes())?;
let mut data = [0; 4096];
let len = stream.read(&mut data)?;
let text = str::from_utf8(&data[..len]).unwrap();
println!("Recieved: {}", text);
}
}
当服务器运行在replit上,我试图连接到它,我得到这个错误:Error: Custom { kind: Uncategorized, error: "Failed to lookup address information: Temporary failure in name resolution" }
我试图从IncompleteHelpfulServer.smartflow.repl更改地址。公司:6969:
IncompleteHelpfulServer.smartflow.repl.co
https://IncompleteHelpfulServer.smartflow.repl.co: 6969
http://IncompleteHelpfulServer.smartflow.repl.co: 6969
https://IncompleteHelpfulServer.smartflow.repl.co
http://IncompleteHelpfulServer.smartflow.repl。co
但两者都不起作用
我搜索如果它甚至是可能的,它也是如果我尝试连接到地址与我的浏览器的replit服务器得到一些关于我的浏览器的信息,所以我认为有一个问题在客户端代码
有人能帮我修一下吗?
感谢阅读
尝试使用https的hyper和native-tls在评论中向@Locke致敬