如何连接swift中的socket来读取数据并写入

时间:2015-02-15 08:48:06

标签: sockets swift

如何在swift中创建一个可以监听和写入的套接字? GCDAsyncSocket是正确的方式还是其他方式?我使用CFStreamCreatePairWithSocketToHost连接到套接字但无法从中读取数据。

3 个答案:

答案 0 :(得分:2)

您可以使用桥接到Swift的SocketRocket ObjC框架

https://github.com/square/SocketRocket

然后你只需创建一个类的实例并在Swift中使用它,即:

var socket = SRWebSocket(...)
socket.open()
socket.send()
socket.close()

SocketRocket sources

当用户请求我附上桥接标题提示的屏幕截图时: enter image description here

还记得将你的应用与libcucore.dylib相关联: enter image description here

答案 1 :(得分:1)

对于那些对Swift 3实施感兴趣的人。 它来自raywenderlich's example,并经过一些编辑。

func connect() {
    Stream.getStreamsToHost(withName: self.address!, port: self.port!, inputStream: &self.inputStream, outputStream: &self.outputStream);

    guard let _ = inputStream, let _ = outputStream else {
        // your own execption or otherwise
        return;
    }

    self.inputStream?.delegate = self;
    self.outputStream?.delegate = self;

    self.inputStream?.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode);
    self.outputStream?.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode);

    self.inputStream?.open();
    self.outputStream?.open();
}

func disconnect() {
    if let stream = self.inputStream {
        stream.close();
        stream.remove(from: RunLoop.current, forMode: RunLoopMode.commonModes);
    }
    if let stream = self.outputStream {
        stream.close();
        stream.remove(from: RunLoop.current, forMode: RunLoopMode.commonModes);
    }
    self.inputStream = nil;
    self.outputStream = nil;
}

func write(data:Data) {
    let _ = data.withUnsafeBytes { (unsafePointer:UnsafePointer<UInt8>) in
        let bytesWritten = self.outputStream?.write(unsafePointer, maxLength: data.count);
    };
}

func readAvailableBytes(stream: InputStream) {
    var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: maxReadLength);
    while stream.hasBytesAvailable {
        let numberOfBytesRead = stream.read(buffer, maxLength: maxReadLength)
        if numberOfBytesRead < 0 {
            if let _ = stream.streamError {
                break
            }
        }
        //Construct the Message object
        if(numberOfBytesRead > 0){
            let output = String(cString:buffer)
            NSLog("server said: %@", output)
        } else {
            NSLog("empty string from stream")
        }

    }
    buffer.deallocate(capacity: maxReadLength);
}

确保在处理类中实现 StreamDelegate 和委托方法:

func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
    NSLog("Receieve stream event: %d", eventCode.rawValue);
    switch (eventCode){
    case Stream.Event.errorOccurred:
        NSLog("ErrorOccurred")
        break;
    case Stream.Event.endEncountered:
        NSLog("EndEncountered")
        break;
    case Stream.Event.hasBytesAvailable:
        NSLog("HasBytesAvaible");
        break;
    case Stream.Event.openCompleted:
        NSLog("OpenCompleted");
        break;
    case Stream.Event.hasSpaceAvailable:
        NSLog("HasSpaceAvailable");
        break;
    default:
        NSLog("default reached. unknown stream event")
        break;
    }
} 

注意:raywnderlich的实现有一些变化,特别是在委托方法中。

答案 2 :(得分:0)

我找到了swift的答案。我的应用程序代码是swift 2,服务器代码是java。我有快速插座的问题,但我找到了答案。这是我的问题链接how to use socket in swift (connect, send and get message)并使用https://github.com/swiftsocket/SwiftSocket 我创建了自己的套接字类:

import Foundation

类SocketManager {

let socket : TCPClient

class var sharedInstance: SocketManager {
    struct Singleton {
        static let instance = SocketManager()
    }
    return Singleton.instance
}

init() {
    // Create the socket
    self.socket = TCPClient(addr: "www.apple.com", port: 9000)
    // Connect the socket
    let (success, msg )=self.socket.connect(timeout: 1)
    print("\(msg) : \(success)")

}

internal func sendMessage(data: String){
    let qualityOfServiceClass = QOS_CLASS_BACKGROUND
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
    dispatch_async(backgroundQueue, {
        let request = self.sendRequest(data, client: self.socket)
        print("received message: \(request)")
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            print("This is run on the main queue, after the previous code in outer block")
        })

    })
}

private func sendRequest(data: String, client: TCPClient?) -> (String?) {
    // It use ufter connection
    if client != nil {
        // Send data  (WE MUST ADD SENDING MESSAGE '\n' )
        let (isSuccess, errorMessage) = client!.send(str: "\(data)\n")
        if isSuccess {
            // Read response data
            let data = client!.read(1024*10)
            if let d = data {
                // Create String from response data
                if let str = NSString(bytes: d, length: d.count, encoding: NSUTF8StringEncoding) as? String {
                    return (data: str)
                } else {
                    return (data: nil)
                }
            } else {
                return (data: nil)
            }
        } else {
            print(errorMessage)
            return (data: nil)
        }
    } else {
        return (data: nil)
    }
}

}

像这样使用:

SocketManager.sharedInstance.sendMessage("Salom Hammaga")