Last active
June 9, 2023 21:50
-
-
Save alimovlex/2e78b9454b0a403047ecc9f5963865b0 to your computer and use it in GitHub Desktop.
The TCP connection demo for iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (C) 2023 Recompile.me. | |
| * All rights reserved. | |
| */ | |
| class TCP_Communicator: NSObject, StreamDelegate { | |
| var readStream: Unmanaged<CFReadStream>? | |
| var writeStream: Unmanaged<CFWriteStream>? | |
| var inputStream: InputStream? | |
| var outputStream: OutputStream? | |
| private var url: URL; | |
| private var port: UInt32; | |
| init(url: URL, port: UInt32) { | |
| self.url = url; | |
| self.port = port; | |
| } | |
| func connect() { | |
| CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (url.absoluteString as CFString), port, &readStream, &writeStream); | |
| print("Opening streams.") | |
| outputStream = writeStream?.takeRetainedValue() | |
| inputStream = readStream?.takeRetainedValue() | |
| outputStream?.delegate = self; | |
| inputStream?.delegate = self; | |
| outputStream?.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default); | |
| inputStream?.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default); | |
| outputStream?.open(); | |
| inputStream?.open(); | |
| } | |
| func disconnect(){ | |
| print("Closing streams."); | |
| inputStream?.close(); | |
| outputStream?.close(); | |
| inputStream?.remove(from: RunLoop.current, forMode: RunLoop.Mode.default); | |
| outputStream?.remove(from: RunLoop.current, forMode: RunLoop.Mode.default); | |
| inputStream?.delegate = nil; | |
| outputStream?.delegate = nil; | |
| inputStream = nil; | |
| outputStream = nil; | |
| } | |
| func stream(_ aStream: Stream, handle eventCode: Stream.Event) { | |
| print("stream event \(eventCode)") | |
| switch eventCode { | |
| case .openCompleted: | |
| print("Stream opened") | |
| case .hasBytesAvailable: | |
| if aStream == inputStream { | |
| var dataBuffer = Array<UInt8>(repeating: 0, count: 1024) | |
| var len: Int | |
| while (inputStream?.hasBytesAvailable)! { | |
| len = (inputStream?.read(&dataBuffer, maxLength: 1024))! | |
| if len > 0 { | |
| let output = String(bytes: dataBuffer, encoding: .ascii) | |
| if nil != output { | |
| print("server said: \(output ?? "")") | |
| } | |
| } | |
| } | |
| } | |
| case .hasSpaceAvailable: | |
| print("Stream has space available now") | |
| case .errorOccurred: | |
| print("\(aStream.streamError?.localizedDescription ?? "")") | |
| case .endEncountered: | |
| aStream.close() | |
| aStream.remove(from: RunLoop.current, forMode: RunLoop.Mode.default) | |
| print("close stream") | |
| default: | |
| print("Unknown event") | |
| } | |
| } | |
| func send(message: String){ | |
| let response = "msg:\(message)" | |
| let buff = [UInt8](message.utf8) | |
| if let _ = response.data(using: .ascii) { | |
| outputStream?.write(buff, maxLength: buff.count) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment