Skip to content

Commit

Permalink
Merge pull request #18 from gunterhager/swift5
Browse files Browse the repository at this point in the history
Updated example
  • Loading branch information
gunterhager authored Apr 2, 2019
2 parents 6fd7bb8 + ca580a7 commit e2153e8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Send a message via broadcast:
try broadcastConnection.sendBroadcast("This is a test!")
```

### Try it out

You can test the broadcast and the handler for receiving messages by running the included `receive_and_reply.py` script (tested with Python 2.7.10) on your Mac. If you send a broadcast with the example app, you should see the message that was sent in the terminal and see the script's answer in the example app.

## Installation

Expand Down
6 changes: 4 additions & 2 deletions UDPBroadcast/UDPBroadcastConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class UDPBroadcastConnection {
var address: sockaddr_in

/// Type of a closure that handles incoming UDP packets.
public typealias ReceiveHandler = (_ ipAddress: String, _ port: Int, _ response: [UInt8]) -> Void
public typealias ReceiveHandler = (_ ipAddress: String, _ port: Int, _ response: Data) -> Void
/// Closure that handles incoming UDP packets.
var handler: ReceiveHandler?

Expand Down Expand Up @@ -163,8 +163,10 @@ open class UDPBroadcastConnection {

debugPrint("UDP connection received \(bytesRead) bytes from \(endpoint.host):\(endpoint.port)")

let responseBytes = Data(response[0..<bytesRead])

// Handle response
self.handler?(endpoint.host, endpoint.port, response)
self.handler?(endpoint.host, endpoint.port, responseBytes)
} catch {
if let error = error as? ConnectionError {
self.errorHandler?(error)
Expand Down
2 changes: 1 addition & 1 deletion UDPBroadcastExample/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
struct Config {

struct Strings {
static let broadcastMessage = "get services"
static let broadcastMessage = "Hello!"
}

struct Ports {
Expand Down
13 changes: 11 additions & 2 deletions UDPBroadcastExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ class ViewController: UIViewController {
do {
broadcastConnection = try UDPBroadcastConnection(
port: Config.Ports.broadcast,
handler: { [weak self] (ipAddress: String, port: Int, response: [UInt8]) -> Void in
handler: { [weak self] (ipAddress: String, port: Int, response: Data) -> Void in
guard let self = self else { return }
self.log("Received from \(ipAddress):\(port):\n\n\(response)\n")
let hexString = self.hexBytes(data: response)
let utf8String = String(data: response, encoding: .utf8) ?? ""
print("UDP connection received from \(ipAddress):\(port):\n\(hexString)\n\(utf8String)\n")
self.log("Received from \(ipAddress):\(port):\n\(hexString)\n\(utf8String)\n")
},
errorHandler: { [weak self] (error) in
guard let self = self else { return }
Expand All @@ -36,6 +39,12 @@ class ViewController: UIViewController {
}
}

private func hexBytes(data: Data) -> String {
return data
.map { String($0, radix: 16, uppercase: true) }
.joined(separator: ", ")
}


@IBAction func reload(_ sender: AnyObject) {
log("")
Expand Down
17 changes: 17 additions & 0 deletions receive_and_reply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python

import select, socket
import subprocess
import os

port = 5559
bufferSize = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('0.0.0.0', port))

while True:
data, addr = s.recvfrom(bufferSize)
print "received incoming call with: ", data, ", from: ", addr
s.sendto("Hello, World!", addr)

0 comments on commit e2153e8

Please sign in to comment.