Currently I am working on a program in which i am connecting two IOS devices using NSNetService class. I am able to connect both the devices but i don't know how to send data using getInoutStream function. Can any buddy help me.
Asked
Active
Viewed 415 times
1 Answers
1
Why not use multipeer? It's a much simpler solution to what you're trying to do. Also NSNetService has a few known bugs. However, you send data between the two by setting up a Bonjour connection on NSNetService. The getInputStream method requires arguments of the type UnsafeMutablePointer:
public func getInputStream(inputStream: UnsafeMutablePointer<NSInputStream?>, outputStream: UnsafeMutablePointer<NSOutputStream?>) -> Bool
var inputStream : NSInputStream?
var outputStream : NSOutputStream?
let success = service.getInputStream(&inputStream, outputStream: &outputStream)
Then just write data using a memory stream and it'll get passed to the listener port.
This is the finished code:
This function initiates the connection on 127.0.0.1
func initNetworkCommunication(){
var host : CFString = "127.0.0.1"
var port : UInt32 = 7001
var readstream : Unmanaged<CFReadStream>?
var writestream : Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readstream, &writestream)
inputstream = readstream!.takeRetainedValue()
outputstream = writestream!.takeRetainedValue()
inputstream.delegate = self
outputstream.delegate = self
inputstream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
outputstream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
inputstream.open()
And this is for the stream IO:
func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) { //This is the stream IO function. It allows RW of the stream
switch (eventCode){
case NSStreamEvent.ErrorOccurred:
NSLog("ErrorOccurred")
break
case NSStreamEvent.EndEncountered:
NSLog("EndEncountered")
break
case NSStreamEvent.None:
NSLog("None")
break
case NSStreamEvent.HasBytesAvailable:
NSLog("HasBytesAvaible")
var buffer = [UInt8](count: 4096, repeatedValue: 0)
if ( aStream == inputstream){
while (inputstream.hasBytesAvailable){
var len = inputstream.read(&buffer, maxLength: buffer.count)
if(len > 0){
var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding)
if (output != ""){
NSLog("server said: %@", output!)
}
}
}
}
break
case NSStreamEvent.allZeros:
NSLog("allZeros")
break
case NSStreamEvent.OpenCompleted:
NSLog("OpenCompleted")
break
case NSStreamEvent.HasSpaceAvailable:
NSLog("HasSpaceAvailable")
break
default:
// default code here
break
}

TechnicalTophat
- 1,655
- 1
- 15
- 37
-
Thank's for the reply. I have done all these things but I want to send message between both the devices, so what should I do?? – Avim Apr 29 '16 at 08:38
-
@AvinashMulewa I told you. Use a memory stream and just write to 'success'. – TechnicalTophat Apr 29 '16 at 08:42
-
I am beginner in swift can you please send me the code. – Avim Apr 29 '16 at 08:48
-
@AvinashMulewa I will this once, but Stack Overflow is a community of finding answers to issues, not completed code. I will update my answer and get back to you. – TechnicalTophat Apr 29 '16 at 08:51
-
@AvinashMulewa My pleasure. Please remember that Stack Overflow is for answers, not code (however, we are happy to help whenever we can). Please mark this as answered for future reference. Happy coding :-) – TechnicalTophat Apr 29 '16 at 09:15
-
@AvinashMulewa FYI: It's also generally not good practice to use `default` case in Swift for debugging purposes, otherwise you lose any errors or issues the compiler gives you at runtime from the switch statement. – TechnicalTophat Apr 29 '16 at 09:16
-
this is for connected iPhone app to any device that listen to port and host (computer for example) ? and transfer files ? – Roei Nadam Apr 08 '19 at 11:49