1

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?

prem111
  • 63
  • 1
  • 7

2 Answers2

0

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)port

Something like:

socket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
do {
    // I don't think socket should be optional here...
    try socket.connect(toHost: "192.168.1.1", onPort: 5000, withTimeout: 5)

} catch  {
    print("socket error")

}

func socket(socket : GCDAsyncSocket, didConnectToHost host:String, port p:UInt16)
{
    print("connected to \(host) & \(port)")
}

where you've moved your "print("connected!")" there. If the socket connected, you'll see the print appear in your console.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • What if it can't connect? how to check ? Thanks for the answer. – prem111 Jan 20 '20 at 12:47
  • if there's an error, you should see the "`socket error`" appear in your console. That's what your `catch` case is for. – Michael Dautermann Jan 20 '20 at 12:56
  • Well, the problem is that there is no error. I write with "connected" even though it is not connected. – prem111 Jan 20 '20 at 12:59
  • so the `didConnectToHost` method does fire (if you set a breakpoint there, would it hit and stop Xcode execution?). I just updated the "connected" line. What host & port are connected? – Michael Dautermann Jan 20 '20 at 13:10
  • If the server is running I have: connected to 192.168.1.1 & 5000 If the server is not running I don't have any error ... – prem111 Jan 20 '20 at 13:31
  • No. I still have a problem. Why doesn't an error appear when it can't connect? and how to check if I'm currently connected or disconnected .... – prem111 Jan 20 '20 at 15:32
0

I would assign a value to the "tag" property on each socket, and then, on the delegate, compare the socket.tag parameter to figure out which one disconnected.

socket1.tag = 1
socket2.tag = 2


func socket(socket : GCDAsyncSocket, didConnectToHost host:String, port p:UInt16)
{
    if (socket.tag==1) {
       print("Socket 1 disconnected")
    } 
}
JoeGalind
  • 3,545
  • 2
  • 29
  • 33