I want to send a TCP packet, using sacpy, from the server (Python) to the client (Swift). Sending the Message from the server:
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)
The code in swift is based on: HTTP Request in Swift with POST method
Receiving the code in swift:
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()
The packet is sent from the server, but the client doesn't receives it. I tried sending the packet to the port, from which the message was sent from the client to the server. Also tried sending to port 80 (HTTP). In both cases, The error was: (10.172.0.136 is the server's IP)
error=Optional(Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x608001043330 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://10.172.0.136:80/, NSErrorFailingURLKey=http://10.172.0.136:80/, kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.})
I assume, that the problem is in the receiving at the client (Using Wireshark I saw that the message was sniffed correctly). Is there an easy way to fix my code? If it’s really unfixable, what other way can I use to receive the packet in swift?