Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set timeout for connect? #76

Closed
SelvaBalasubramanian opened this issue Jul 24, 2017 · 6 comments
Closed

How to set timeout for connect? #76

SelvaBalasubramanian opened this issue Jul 24, 2017 · 6 comments

Comments

@SelvaBalasubramanian
Copy link

SelvaBalasubramanian commented Jul 24, 2017

I need to set timeout for socket.connect(ip ,port) how can I do it?

@billabt
Copy link
Collaborator

billabt commented Jul 24, 2017

The easiest way is to use the isReadableOrWritable function. The basic steps are as follows:

  1. Create the socket.
  2. Set the socket to non-blocking mode using setBlocking(mode: false).
  3. Issue the connect.
  4. Call isReadableOrWritable(waitForever: false, timeout: yourTimeout) function on the socket using your desired timeout.
  5. If the returned tuple indicates that it's writable, you're connected, if not, the timeout has occurred.
  6. Reset the blocking mode back to blocking if desired...

@billabt billabt closed this as completed Jul 24, 2017
@SelvaBalasubramanian
Copy link
Author

I tried but the same problem occurs.The problem what I am facing is when the socket.connect(ip, port) starts executing it waits for some time to connect.

@SelvaBalasubramanian
Copy link
Author

SelvaBalasubramanian commented Jul 25, 2017

here is the code!!
I need to implement a network scan...for availability of a particular ip and port...
`

func searchController(){
    let ipString = "192.168.1."
    let port = 8286
    var k = 1
    
    let socket = try? Socket.create()
    while(k <= 255){

        let ip = ipString + "\(k)"
        try? socket?.setBlocking(mode: false)
        print("function entered")
            try?  socket?.connect(to: ip , port: Int32(port))
           print(k)
        let result = try? socket?.isReadableOrWritable(waitForever: false, timeout: UInt(0
        ))
        
        if result??.writable != nil {
            if result.unsafelyUnwrapped?.writable == true{
                print(ip  + " " + "\(port)" )
            }
            }else{
                socket?.close()
            }else{
                 socket?.close()
             }
     k +=  1

}
    
}`

@SelvaBalasubramanian
Copy link
Author

i tried but it still waits in socket?.connect(to: ip , port: Int32(port))

@mna
Copy link
Contributor

mna commented Jul 26, 2017

I can confirm the same behaviour for me on Darwin, see https://github.com/mna/testconnecttimeout to reproduce. Blocks on the call to socket.connect even though the socket is in non-blocking mode.

@billabt
Copy link
Collaborator

billabt commented Aug 7, 2017

Fixed in 0.12.60.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants