在 iPhone 上监听 CocoaASyncSocket 接收连接,但新套接字不调用委托
我正在尝试通过指定 IP 来实现两个设备之间的基本消息传递。当一台设备告诉其侦听套接字按如下方式进行侦听时:
UInt16 port = 59647;
NSError *err = nil;
[socket acceptOnPort:port error:&err];
didAcceptNewSocket
委托被正确调用,并且应该向连接 IP:Port 返回一个新套接字。然而,据我所知,它正在执行此操作,然后新套接字应该调用 didConnectToHost
,但据我所知,事实并非如此。
didConnectToHost
已正确实现,因为发起与其套接字连接的设备在建立连接后正确调用它。
除了一些 NSLogs
并将旧的监听套接字设置为新的监听套接字(因为之后不需要它,并将其分配给一个之前的不同变量没有改变任何东西,也不应该改变)。
什么可以使侦听端的新套接字不调用该委托?
I'm trying to implement basic message pasing between two devices by specifying IPs. When one device tells its listening socket to listen as follows:
UInt16 port = 59647;
NSError *err = nil;
[socket acceptOnPort:port error:&err];
The didAcceptNewSocket
delegate is called correctly, and should return a new socket to the connecting IP:Port. As far as I can tell it's doing this, however, the new socket should be then calling didConnectToHost
, which as far as I can tell it is not.
didConnectToHost
is implemented correctly, as the device which initiates the connection with its socket properly calls it after the connection has been established.
I'm not doing anything in didAcceptNewSocket
aside from a few NSLogs
and setting the old listening socket to the new one (as its not needed after, and having it assigned to a different variable before didn't change anything, nor should it).
What could make my new socket on the listening side not call this delegate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您正在处理客户端和服务器,因此重要的是要认识到只有客户端才会调用
socket:didConnectToHost:port:
委托方法,因为它是连接到主机的客户端(而不是反之亦然)。在服务器上,使用侦听套接字作为第一个参数来调用
socket:didAcceptNewSocket:
委托方法。Since you are dealing with a client and a server, it is important to realize that only the client will invoke the
socket:didConnectToHost:port:
delegate method as it is the client who connects to a host (not the other way around).On the server, the
socket:didAcceptNewSocket:
delegate method is invoked with the listening socket as the first argument.