robbiehanson / CocoaAsyncSocket

Asynchronous socket networking library for Mac and iOS
Other
12.45k stars 3k forks source link

If two apps use the same port, I get an error. #809

Closed emoto-yasushi closed 1 year ago

emoto-yasushi commented 2 years ago

I am creating a UDP server using the following code.

Creating the UDP server works fine, but I get an error if I use the same port for two apps. If I force one app to quit, it works fine, but I would like to free the port with viewWillDisappear if possible.

Is that possible?

   var udpListner: GCDAsyncUdpSocket?

    override func viewDidLoad()
    {
           udpListner = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
    }

    override func viewWillAppear(_ animated: Bool) 
    {
        super.viewWillAppear(animated)
       do {
            try udpListner?.bind(toPort: 8888)
            try udpListner?.beginReceiving()

        } catch {
                print("The port is already in use")
                print(error)
        }
    }

    override func viewWillDisappear(_ animated: Bool) 
    {
             super.viewWillDisappear(animated)
             udpListner?.close()
    }
github-actions[bot] commented 1 year ago

This issue has been marked as stale, it will be closed automatically if there is no further activity.