2

iOS 13, Swift 5, Xcode 11

Running a python udp server.

#!/usr/bin/python

import socket

port = 31766

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', port))
s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1500)
print("listening on port ",port)

while True:
    compressed_data, server = s.recvfrom(1500)
    print(compressed_data)

Can connect with UDP client.

#!/usr/bin/python
import socket

UDP_IP = "192.168.1.1"
UDP_PORT = 31766 
MESSAGE = "Hello, World!"

print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE

sock = socket.socket(socket.AF_INET, # Internet
         socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

But when I try and run a BONJOUR service

dns-sd -R wow _wow._udp local 31766

I can see it with my iOS. I can see it with OSX. I can connect with no errors and send data with this code ....

func connectToUDP(name: String) {
  self.talking = NWConnection(to: .service(name: name, type: "_wow._udp", domain: "local", interface: nil), using: .udp)

  self.talking?.stateUpdateHandler = { (newState) in
  switch (newState) {
  case .ready:
    print("ready to send")
    self.sendUDP("Boo")
  default:
    print("damn")
  }
}
self.talking?.start(queue: .main)
}

My server works ok, cause I see data coming thru when the python sending script is run. But when I try and send data thru the swift code I see nothing.

Some more notes on this. If I change the NWConnection so that it looks like this, it works.

self.talking = NWConnection(host: "192.168.1.110", port: 31766, using: .udp)

But the point is I lose the bonjour service effectively. Note I use this command to check that the bonjour service is running.

dns-sd -B _wow._udp

returns Browsing for _wow._udp DATE: ---Fri 07 Feb 2020--- 12:08:41.387 ...STARTING... Timestamp A/R Flags if Domain Service Type Instance Name 12:08:41.388 Add 3 1 local. _wow._udp. wow 12:08:41.388 Add 3 4 local. _wow._udp. wow 12:08:41.388 Add 2 5 local. _wow._udp. wow

And I use this command to check it is returning the correctly parameters

dig -p 5353 @224.0.0.251 wow._wow._udp.local. SRV +short

returns 0 0 31766 Home-3.local.

More debug. I downloaded, compiled and used this as a server on my OS X. It works.

OSX Network Framework

Obviously you need to change tcp in this source to UDP and comment out the back connection.

Also tried this to get the host + port thru

Bonjour: Search for a service by name

And than went with the second call for NWConnection, with the returned information...

self.talking = NWConnection(host: "Home-3.local.", port: 31766, using: .udp)

This works too, But to go back to my original question. I want to try and connect to a PYTHON server, not on iOS one or an OSX one. Incidentally here is the code for the browser under iOS which finds the service!

func findUDP() {
print("findUDP")
let parameters = NWParameters()
parameters.includePeerToPeer = true

browser = NWBrowser(for: .bonjour(type: "_wow._udp", domain: "local."), using: parameters)
browser?.browseResultsChangedHandler = { foo, changes in
  for change in changes {
    switch change {
    case .added(let browseResult):
      playing = browseResult.endpoint
      print(browseResult.endpoint)
    default:
      print("anything else seen")
    }
  }
}
self.browser?.start(queue: .main)
user3069232
  • 8,587
  • 7
  • 46
  • 87

0 Answers0