从设备连接到本地 mac 服务器
Connect to local mac server from device
我正在尝试使用以下代码从应用程序连接到 ruby sinatra 服务器,该服务器在我的 mac 本地 运行 上:
func load(finished: @escaping ()->()) {
// Create destination URL
let destinationFileUrl = documentsUrl.appendingPathComponent("Images.zip")
//Create URL to the source file you want to download
let fileURL = URL(string: "http://waynerumble.local~waynerumble:4567/download")
//Create Session
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}
finished()
} else {
print("Error took place while downloading a file. Error description: %@", (error?.localizedDescription)! as String);
finished()
}
}
task.resume()
}
如果我从模拟器测试应用程序并将 fileURL
设置为 "http://127.0.0.1:4567/download"
它工作正常但从设备我知道这必须是不同的到目前为止我已经尝试过:
从终端的 运行 ifconig 我在 en1 得到 192.168.1.254
所以我尝试了 "http://192.168.1.255:4567/download"
这给了我:
[] nw_socket_connect connectx failed: [13] Permission denied
Error took place while downloading a file. Error description: %@ Could not connect to the server.
我也试过:
"http://waynerumble.local:4567/download"
给出:
Error took place while downloading a file. Error description: %@ Could not connect to the server.
"http://waynerumble.local.~waynerumble:4567/download"
(waynerumble 是我的计算机名称和用户名)给出:
Error took place while downloading a file. Error description: %@ A server with the specified hostname could not be found.
我还通过以太网和 iphone 共享 wifi 互联网。我不确定还能尝试什么
192.168.1.255
是您网络的广播地址,您不应使用它。
你为什么不连接到你的真实 IP 192.168.1.254
?
要将 Sinatra 应用程序绑定到每个界面,请尝试:
class MyApp < Sinatra::Base
set :bind, '0.0.0.0'
那么 http://192.168.1.254:4567/download
应该可以。
还要记住在防火墙中打开所需的端口。
我正在尝试使用以下代码从应用程序连接到 ruby sinatra 服务器,该服务器在我的 mac 本地 运行 上:
func load(finished: @escaping ()->()) {
// Create destination URL
let destinationFileUrl = documentsUrl.appendingPathComponent("Images.zip")
//Create URL to the source file you want to download
let fileURL = URL(string: "http://waynerumble.local~waynerumble:4567/download")
//Create Session
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}
finished()
} else {
print("Error took place while downloading a file. Error description: %@", (error?.localizedDescription)! as String);
finished()
}
}
task.resume()
}
如果我从模拟器测试应用程序并将 fileURL
设置为 "http://127.0.0.1:4567/download"
它工作正常但从设备我知道这必须是不同的到目前为止我已经尝试过:
从终端的 运行 ifconig 我在 en1 得到 192.168.1.254
所以我尝试了 "http://192.168.1.255:4567/download"
这给了我:
[] nw_socket_connect connectx failed: [13] Permission denied
Error took place while downloading a file. Error description: %@ Could not connect to the server.
我也试过:
"http://waynerumble.local:4567/download"
给出:
Error took place while downloading a file. Error description: %@ Could not connect to the server.
"http://waynerumble.local.~waynerumble:4567/download"
(waynerumble 是我的计算机名称和用户名)给出:
Error took place while downloading a file. Error description: %@ A server with the specified hostname could not be found.
我还通过以太网和 iphone 共享 wifi 互联网。我不确定还能尝试什么
192.168.1.255
是您网络的广播地址,您不应使用它。
你为什么不连接到你的真实 IP 192.168.1.254
?
要将 Sinatra 应用程序绑定到每个界面,请尝试:
class MyApp < Sinatra::Base
set :bind, '0.0.0.0'
那么 http://192.168.1.254:4567/download
应该可以。
还要记住在防火墙中打开所需的端口。