I'm making a remote control app and I'm trying to send bytes to a TCP server but I'm getting this error I'm not sure if I'm structuring the array of bytes correctly or not.
The operation couldn't be completed. (Socket.Socket.Error error 1)
This is my code:
func sendCommand(name: String) {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
let code = commands[name]!
print(code)
let length = 12
// array of bytes necessary to send a command
var commandBytes:[Int8] = [4, 1, 0, 0, 0, 0, -32, 11]
// connect to the Sky box
do {
socket = try Socket.create()
try socket!.connect(to: ipAddress!, port: Int32(UInt32(port!)), timeout: 1000)
while(true) {
var result = Data()
// read the data sent by sky
let bytesRead = try socket!.read(into: &result)
if (bytesRead < 24){
// send TV the same message
print("Sending back same message...")
try socket!.write(from: result)
}else{
print("Sending command...")
try socket!.write(from: commandBytes, bufSize: 8)
commandBytes = [4, 0, 0, 0, 0, 0, -32, 11]
try socket!.write(from: commandBytes, bufSize: 8)
socket!.close()
break
}
}
} catch {
let alert = UIAlertController(title: "Something went wrong", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel))
viewController.present(alert, animated: true)
}
}