why when trying to connect when the server does not respond "try" shows that it is connected?
I need to check the connection status. Every time, despite the timeout setting, it shows that it is connected ...
My code:
socket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
do {
try socket?.connect(toHost: "192.168.1.1", onPort: 5000, withTimeout: 5)
print ("connect")
}catch {
print("socket error")
}
And one more question,
i create two socket on the same port:
socket1.connect(toHost: "192.168.1.1", onPort: 5000, withTimeout: 5)
socket2.connect(toHost: "192.168.1.1", onPort: 5000, withTimeout: 5)
in func:
func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
...
}
How to detect which socket (socket1 or socket2 ) has been disconnected?
GCDAsyncSocket does have delegate methods (and in your code example above, you do set the delegate to
self). So implement a tiny function with the Swift equivalent of this function:- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)portSomething like:
where you've moved your "
print("connected!")" there. If the socket connected, you'll see the print appear in your console.