Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
流套接字服务器端swift_Swift_Sockets_Stream - Fatal编程技术网

流套接字服务器端swift

流套接字服务器端swift,swift,sockets,stream,Swift,Sockets,Stream,我创建了一个小脚本,它是一个套接字客户端,等待服务器消息。我的目标是实现相反的功能,一个服务器是开放的,可以监听客户端消息,当然它也可以发送消息。我已经包含了该代表,以便“侦听”新消息。有什么想法吗?谢谢 import UIKit class ViewController: UIViewController, StreamDelegate { struct connectionStream { static var inp :InputStream?

我创建了一个小脚本,它是一个套接字客户端,等待服务器消息。我的目标是实现相反的功能,一个服务器是开放的,可以监听客户端消息,当然它也可以发送消息。我已经包含了该代表,以便“侦听”新消息。有什么想法吗?谢谢

import UIKit

class ViewController: UIViewController, StreamDelegate {

    struct connectionStream {
        static var inp :InputStream?
        static var out :OutputStream?
    }

    var inputStream: InputStream!
    var outputStream: OutputStream!

    override func viewDidLoad() {
        super.viewDidLoad()

        let addr = "127.0.0.1"
        let port = 12345

        Stream.getStreamsToHost(withName: addr, port: port, inputStream: &connectionStream.inp, outputStream: &connectionStream.out)

        inputStream = connectionStream.inp!
        outputStream = connectionStream.out!

        self.inputStream.delegate = self
        self.outputStream.delegate = self

        self.inputStream.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default)
        self.outputStream.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default)

        self.inputStream.open()
        self.outputStream.open()



    }

    @IBAction func messageToHololens(_ sender: Any) {
        self.sendToHololens("Hello Hololens")
    }


    func sendToHololens(_ messageValue : String) {

        let length_message = messageValue.count

        self.outputStream!.write(messageValue, maxLength: length_message)

        print("message envoyé")

    }

    func disconnect() {
        self.inputStream.close()
        self.outputStream.close()
    }

    func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
        if aStream === inputStream {
            switch eventCode {
            case Stream.Event.openCompleted:
                print("client accepté")

            case Stream.Event.hasBytesAvailable:
                var buffer = [UInt8](repeating: 0, count: 4096)

                while (self.inputStream.hasBytesAvailable){
                    let len = self.inputStream.read(&buffer, maxLength: buffer.count)
                    if(len > 0){
                        let output = NSString(bytes: &buffer, length: len, encoding: String.Encoding.utf8.rawValue)
                        if (output != ""){
                            print("server said:", output!)
                        }
                    }
                }
            default:
                break
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
使用:

import UIKit
import CocoaAsyncSocket

class ViewController: UIViewController, GCDAsyncSocketDelegate{
    let host = "127.0.0.1"
    let port:UInt16 = 7777
    var mSocket: GCDAsyncSocket!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.mSocket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
        do {
            try self.mSocket.accept(onPort: self.port)
        } catch let error {
            print(error)
        }
    }

    public func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
        print("Message sent")
    }
    public func socket(_ sock: GCDAsyncSocket, didAcceptNewSocket newSocket: GCDAsyncSocket) {
        print("client accepted!!!!")

    }

    @IBAction func sendMessage(_ sender: Any) {

        let strData = "Hi"
        let closedString: String = strData + "\n"
        let data: Data = closedString.data(using: String.Encoding.utf8)!
        self.mSocket!.write(data, withTimeout: -1.0, tag: 1)

        self.mSocket!.readData(withTimeout: 1.0, tag: 0)

    }

}