接收TCP數據包我想發出一個TCP數據包,使用sacpy,從服務器(Python)的客戶端(SWIFT)。 從服務器發送所述消息:斯威夫特
packet = IP()/TCP()/Raw()
print(CLIENT_IP, CLIENT_PORT)
packet[IP].dst = CLIENT_IP
packet[TCP].dport = CLIENT_PORT
#packet[Raw].load = str(to_text(places_list))
#print(str(to_text(places_list)))
packet[Raw].load = "HELLO WORLD"
send(packet)
在迅速的代碼是基於:HTTP Request in Swift with POST method
接收的代碼在迅速:
var request = URLRequest(url: URL(string: "http://10.172.0.136:80")!)
request.httpMethod = "POST"
let postString = "x=30&y=10&name=hello"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
print("response = \(String(describing: response))")
}
task.resume()
該分組被從服務器發送,但是客戶端不會收到它。 我嘗試發送分組到該端口,從該消息是從客戶端發送到服務器。也嘗試發送到端口80(HTTP)。 在這兩種情況下,錯誤是:(10.172.0.136是服務器的IP)
錯誤=可選(錯誤域= NSURLErrorDomain代碼= -1005 「網絡連接已丟失」 的UserInfo = {NSUnderlyingError = 0x608001043330 {錯誤域= kCFErrorDomainCFNetwork代碼= -1005 「(空)」 的UserInfo = {_ kCFStreamErrorCodeKey = -4,kCFStreamErrorDomainKey = 4}},NSErrorFailingURLStringKey = http://10.172.0.136:80/,NSErrorFailingURLKey = http://10.172.0.136:80/,kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -4,NSLocalizedDescription =該網絡連接))
我假設問題出在客戶端的接收端(U唱Wireshark我看到消息正確嗅探)。 有沒有簡單的方法來修復我的代碼?如果它確實是不可修復的,我可以用什麼其他方式來快速接收數據包?
謝謝。最後,我使用了Bottle框架而不是SimpleHTTPServer,並且效果很好。 – Inbal