浏览器上的GCDAsyncSocket会立即通过“Socket由远程对等方关闭”错误连接和断开连接

时间:2014-11-13 23:14:52

标签: sockets swift network-programming ios8 gcdasyncsocket

我是网络编程的新手,并且是第一次使用GCDAsyncSocket。我正在尝试实现一个iOS应用程序,其中1个设备充当主机,其他设备(浏览器)连接到它。我正在使用NSNetService / NSNetServiceBrowser发布/浏览部分。

现在,我的客户端/浏览器正确解析了主机的地址,并尝试使用以下代码连接到该地址:

func netServiceDidResolveAddress(sender: NSNetService) {
   // Connect with Service
   if (self.connectWithService(sender, hostname: sender.hostName!)) {
       println("Connecting with service: domainName= \(sender.domain), type= \(sender.type), name= \(sender.name), onPort= \(sender.port) and hostname: \(sender.hostName!)");
   } else {
       println("Unable to connect with service: domainName= \(sender.domain), type= \(sender.type), name= \(sender.name), onPort= \(sender.port)");
   }
}

private func connectWithService(service: NSNetService, hostname: String) ->Bool {

    var isConnecting:Bool = false;



    if (self.browserSocket == nil || !(self.browserSocket?.isConnected)!) {

        // Initialize Socket

        self.browserSocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue());



        // Connect

        while (!isConnecting){

            var error: NSError?;

            println("Connecting port : \(service.port) and hostname: \(hostname)");

            if (self.browserSocket.connectToHost(hostname, onPort: UInt16(service.port), error: &error)) {

                isConnecting = true;

            } else if (error != nil) {

                println("Unable to connect to address. Error \(error) with user info \(error?.userInfo)");

            }

        }

    } else {

        println("Connecting is: \(self.browserSocket.isConnected)");

        isConnecting = self.browserSocket.isConnected;

    }

    return isConnecting;

}

// GCDAsyncSocketDelegate methods

func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {
   println("Socket: \(sock) Did connect to host: \(host) on port:\(port)");

   // Start Reading
   sock.readDataToLength(UInt(sizeof(UInt64)), withTimeout: -1.0, tag: 0);
}

func socketDidDisconnect(sock: GCDAsyncSocket!, withError err: NSError!) {
   println("Socket \(sock) did disconnect with error \(err?)");

 }

以下是发布商代码:

覆盖func viewDidLoad(){

    super.viewDidLoad()

    let barButton = UIBarButtonItem(title: "Stop Publishing", style: .Done, target: self, action: "stopPublishing");

    self.navigationItem.rightBarButtonItem = barButton;



    // Publish the service

    self.publishService();

}

// MARK: Button callbacks

private func publishService() {

    // Initialize GCDAsyncSocket

    self.socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue());



    // Start listening for incoming connections

    var error: NSError?;

    if (self.socket.acceptOnPort(0, error: &error)) {

        // Initialize Service

        self.service = NSNetService(domain: "local.", type: "_mpstest._tcp", name: UIDevice.currentDevice().name, port:0);



        // Configure Service

        self.service.delegate = self;

        self.service.includesPeerToPeer = true;

        self.service.publishWithOptions(.ListenForConnections);

    } else {

        println("Unable to create socket. Error \(error?.description) with user info \(error?.userInfo)");

    }

}

现在,客户端与主机连接并点击以下方法:

func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {

然后它会立即断开与ErrorCode = 7&#34的关注方法:远程对等关闭套接字"

func socketDidDisconnect(sock: GCDAsyncSocket!, withError err: NSError!) {

以下是来自浏览器设备的控制台日志:

Service found is : <NSNetService 0x15e2f710> local. _mpstest._tcp. iPad 30
Connecting port : 50797 and hostname: iPad-30.local.
Connecting with service: domainName= local., type= _mpstest._tcp., name= iPad 30, onPort= 50797 and hostname: iPad-30.local.
Socket <GCDAsyncSocket: 0x15d80020> did disconnect with error Optional(Error Domain=kCFStreamErrorDomainNetDB Code=8 "nodename nor servname provided, or not known" UserInfo=0x15e3c7a0 {NSLocalizedDescription=nodename nor servname provided, or not known})
Connecting port : 50797 and hostname: iPad-30.local.
Connecting with service: domainName= local., type= _mpstest._tcp., name= iPad 30, onPort= 50797 and hostname: iPad-30.local.
Socket: <GCDAsyncSocket: 0x15e59c20> Did connect to host: 169.254.78.98 on port:50797
Socket <GCDAsyncSocket: 0x15e59c20> did disconnect with error Optional(Error Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote peer" UserInfo=0x15e5b070 {NSLocalizedDescription=Socket closed by remote peer})

永远不会命中主机套接字委托方法。

我真的被困在这里了。关于我在这里做错什么的任何帮助都会很棒。

由于 维克

1 个答案:

答案 0 :(得分:1)

我也有同样的错误!经过多次搜索和拔出我的头发后,我偶然发现了一个与GCDAsyncSocket的弱声明有关的解决方案,Swift中的ARC理解了声明&#34; __ weak&#34;以GCDAsyncSocket.m为借口尽快释放套接字。

搜索关键字&#34; __ weak&#34;在您的项目中删除所有引用。 在Delegate not getting set

找到了这个解决方案