BlueSocket icon indicating copy to clipboard operation
BlueSocket copied to clipboard

TCP Write wont work

Open bobongski opened this issue 6 years ago • 6 comments

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

bobongski avatar May 23 '19 11:05 bobongski

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

billabt avatar May 23 '19 16:05 billabt

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 avatar May 24 '19 11:05 bobongski

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.)

bobongski avatar May 24 '19 11:05 bobongski

Any thoughts @billabt ?

ianpartridge avatar Jun 19 '19 08:06 ianpartridge

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

billabt avatar Jun 21 '19 19:06 billabt

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"

jmurphyau avatar Feb 09 '22 06:02 jmurphyau