52

I have been working on a NSStreamDelegate, I have implemented call back, I have initialized the input and output stream ilke this...

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStringRef host = CFSTR("74.125.224.72");
    UInt32 port = 2270;

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &inputStream, &writeStream);

    if (writeStream && inputStream) {

        inputStream = (__bridge  NSInputStream *)readStream;
        [inputStream setDelegate:self];
        [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [inputStream open];

        outputStream = (__bridge  NSOutputStream *)writeStream;
        [outputStream setDelegate:self];
        [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [outputStream open];
     }

Even after opened both the stream callback(stream:(NSStream *)theStream handleEvent:) is not called with NSStreamEventOpenCompleted for both streams. Can anyone help me what am I doing wrong here. Or What is the possibilities NSStreamEventOpenCompleted won't be called, I have seen in documentation, if opening failed it will not call this, if so why opening of streams is failing. Any idea?

thanks for your help.

3

3 Answers 3

0

I use with very similar code and it works fine for me. Try the code below.

   NSString* host = @"192.168.2.105";
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    UInt32 port = 8008;

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)(host), port, &readStream, &writeStream);

    if (writeStream && readStream) {

        self.InputStream = (__bridge  NSInputStream *)readStream;
        [self.InputStream setDelegate:self];
        [self.InputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [self.InputStream open];

        self.OutputStream = (__bridge  NSOutputStream *)writeStream;
        [self.OutputStream setDelegate:self];
        [self.OutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [self.OutputStream open];
    }

If it doesn't work for you, I can to send you a small app that implement TCP Client and server for a example.

0

If it is running in a new NSThread, make sure the run loop of the thread is started after the stream setup, like CFRunLoopRun();

0

To initialize and open input and output streams using Stream class and handle stream events:

import Foundation
// Initialize input and output streams

var inputStream: InputStream?

var outputStream: OutputStream?

let host = "74.125.224.72"
let port: UInt32 = 2270

    
// Create streams for the given host and port
Stream.getStreamsToHost(withName: host, port: Int(port), inputStream: &inputStream, outputStream: &outputStream)


// Check if both streams are successfully initialized
if let inputStream = inputStream, let outputStream = outputStream {
    // Set the delegate to handle stream events
    inputStream.delegate = self
    outputStream.delegate = self


    // Schedule streams in the current run loop for default mode
    inputStream.schedule(in: .current, forMode: .default)
    outputStream.schedule(in: .current, forMode: .default)

    // Open both input and output streams
    inputStream.open()
    outputStream.open()
}

Please conform to the NSStreamDelegate protocol in your class to handle the stream events. The following code demonstrates how to handle the events:

class YourClassName: NSStreamDelegate {

// Other class methods and properties here...

// Handle stream events
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
    switch eventCode {
    case .openCompleted:
        // Stream opened successfully
        print("Stream opened successfully.")
    case .hasBytesAvailable:
        // Handle incoming data on the input stream
        // ...
    case .hasSpaceAvailable:
        // Handle available space on the output stream
        // ...
    case .errorOccurred:
        // Handle stream error
        print("Stream error occurred.")
    case .endEncountered:
        // Handle end of stream
        print("End of stream encountered.")
    default:
        break
    }
}

// Other methods and code here...

}

By implementing the above code, you'll be able to create and open input and output streams and handle stream events in Swift. Remember to replace YourClassName with the actual name of your class, and you should be all set to work with streams in your application!

Not the answer you're looking for? Browse other questions tagged or ask your own question.