Kitura / BlueSocket

Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux.
Apache License 2.0
1.41k stars 197 forks source link

TCP Write wont work #172

Open bobongski opened 5 years ago

bobongski commented 5 years ago

I tried TCP using simulator and try sending string to my NIO server but in my server it wont go inside my function channelRead.

billabt commented 5 years ago

I need a bit more info to be able to troubleshoot this. Any chance you can post some code that illustrates the problem? Thanks.

bobongski commented 5 years ago
func loadServer(){
        self.mySocket = try! Socket.create(family: .inet, type: .stream, proto: .tcp)

         //self.mySocket = try! Socket.create(family: .inet6)
        self.mySocket?.readBufferSize = 32768

        do {
            try mySocket!.connect(to: "localhost", port: 50694)

            var message = Data()
            let _ = try mySocket!.read(into: &message)

            if let string = String(data: message, encoding: .utf8) {
                let xml = try! XML.parse(string)
               // print("xml \(xml["ResultSet"])")
                //xml.ResultSet
                if let text = xml["Result", "CardNumber"].text {
                    print(text)
                }

                lblTest.text = string
            }else{
                lblTest.text = "connected"
           }

          //   mySocket.write(from:, to: address)
          //   lblTest.text = "connected"
//            try mySocket!.write(from: "testing")

        } catch let error {
            lblTest.text = "An error occurred: \(error.localizedDescription)"
        }
    }

    @IBAction func actionWrite(_ sender: Any) {
        print("actionWrite")
        do {
            try mySocket!.write(from: "testing")
        } catch let error {
            print("actionWrite \(error.localizedDescription)")
        }
    }
bobongski commented 5 years ago

Loading server works fine but after I try to write it it has a Socket Error.

The operation couldn’t be completed. (Socket.Socket.Error error 1.)

ianpartridge commented 5 years ago

Any thoughts @billabt ?

billabt commented 5 years ago

Can you try it outside the simulator? I think permissions might be getting in the way when you use the simulator...

jmurphyau commented 2 years ago

This article helped me.. For anyone else getting errors when trying to open a connect on a Mac app, make sure you allow "Outgoing Connections (Client)" in "Signing & Capabilities"