0

If the Wifi connection is active and I'm on the network 192.168.1.xxx and try to establish the connection:

var readStream: Unmanaged<CFReadStream>?
    var writeStream: Unmanaged<CFWriteStream>?
    ///77.246.237.150

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
                                       "192.168.2.100" as CFString,
                                       5555,
                                       &readStream,
                                       &writeStream)
    inputStream = readStream!.takeRetainedValue()
    outputStream = writeStream!.takeRetainedValue()

The delay is 2 minutes before I know the status of the connection. Check for nil readStream writeStream inputStream outputStream does not help Help please solve this problem Use Simple Ping is not suitable since ping can not be done ping to a specific port. Since this port has a TCP server up

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • What do you mean by "before I know the status of the connection?" How are you determining that? When do you open the stream, and what do you write to it? Just creating a stream pair doesn't cause it to perform any network activity. – Rob Napier Jan 18 '18 at 21:15
  • The stream is always created regardless of whether the server is available or not. Sending a byte to the stream awaits delivery of this message. After 2 minutes if the server is not available then I will find out about it. If the server is available then I immediately get an answer from it. – Alexandr Zernov Jan 18 '18 at 21:41
  • This is just the nature of TCP; A "host unreachable" ICMP will cause a rapid failure, but if the host is up but the target socket isn't available then TCP will keep retrying until it times out. `CFStream` is a pretty low-level API. It doesn't seem to offer any way to customise the timeout. – Paulw11 Jan 18 '18 at 21:46
  • Yes, I did not manage to set the timeout. If you use the DNS name and not ipv4, then the failure occurs immediately. – Alexandr Zernov Jan 18 '18 at 21:51
  • 1
    Also, if you try to send any message, then the status can be recognized only after a connection error. it's about 2 minutes – Alexandr Zernov Jan 18 '18 at 21:54
  • You should get an answer back faster if the remote server is up and the port is closed. But if the remote server is up and the port is filtered, then you won't get any response from your write. And if the host is down, it depends on the local networking equipment what will happen. You may get a quick response or not. As Paulw11 note, this is mostly "just the nature of TCP." Typically you first want to ping before trying to connect to the port (but sometimes ICMP is filtered, so it doesn't always help either). – Rob Napier Jan 18 '18 at 23:04
  • You should compare the behavior to what `curl 192.168.2.100:5555` does from your computer. – Rob Napier Jan 18 '18 at 23:04
  • I can not execute the cURL request because the tcp server is used in its pure form. I do not get a quick response if even use cellular data – Alexandr Zernov Jan 19 '18 at 05:46

0 Answers0