BlueSocket icon indicating copy to clipboard operation
BlueSocket copied to clipboard

Longer than expected socket connection time for client

Open SmackTO opened this issue 5 years ago • 1 comments

Hi,

Socket connection times are taking longer than I expected. Ten runs of this simple code results in an average connection time of 0.65 seconds. The the client and "server.local" are both on my wifi network.

import Foundation
import Socket

let MyPySkyXServer = "server.local"
let MyPySkyXPort: Int32 = 1234
do {
    let client = try Socket.create()

    let startTime = Date()
    try client.connect(to: MyPySkyXServer, port: MyPySkyXPort)
    print(Date().timeIntervalSince(startTime))

    client.close()
}
catch {
    print("Error.")
}

While this similar Python script results in average connection times of 0.024 seconds over 10 connects.

import socket
import time

HOST = 'server.local'  # The server's hostname or IP address
PORT = 1234  # The port used by the server

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

startTime = time.perf_counter()
s.connect((HOST, PORT))
elapsed = time.perf_counter() - startTime
print(elapsed)

s.close()

Am I doing something wrong or is this expected?

Steve

SmackTO avatar May 31 '20 14:05 SmackTO

Some more info: Xcode 11.5 and Python 3.7.7 on MacOS 10.15.4.

SmackTO avatar May 31 '20 14:05 SmackTO