简体   繁体   中英

Receiving a TCP packet in Swift

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?

Your problem is that you're using TCP improperly. You're trying to send a single packet outside an established connection, and failing to specify any of the fields that would required for it to be associated with the HTTP request.

Don't use Scapy for this. (Unless you're required to do so for some sort of academic exercise.) It's entirely the wrong tool for the job. Use a HTTP server -- if you want a simple one in Python, the builtin SimpleHTTPServer is a reasonable starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM