Swift: receive UDP packet using Network framework - swift

I'm studying swift 5.6 Network framework. For this I have a Java-based server, that waits a udp packet of size 64 at localhost port 10000 and sends it back to localhost port 20000. Here is my implementation for Swift :
import Foundation
import Network
class UdpConnection {
private var connection: NWConnection?
private var isConnectionReady = false
init?(host: String, port: UInt16) {
self.connection = NWConnection(
host: NWEndpoint.Host(host),
port: NWEndpoint.Port(integerLiteral: port),
using: .udp
)
let connectionEstablishWaiter = DispatchSemaphore(value: 0)
self.connection?.stateUpdateHandler = { [weak self] (newState) in
switch (newState) {
case .ready:
self?.isConnectionReady = true
default :
self?.isConnectionReady = false
}
connectionEstablishWaiter.signal()
}
self.connection?.start(queue: .global())
switch connectionEstablishWaiter.wait(timeout: .now() + 1) {
case .timedOut:
return nil
default:
()
}
}
func sendUDP(content: Data) {
let sema = DispatchSemaphore(value: 0)
self.connection?.send(content: content, completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
if (NWError == nil) {
print("Data was sent to UDP")
} else {
print("ERROR! Error when data (Type: Data) sending. NWError: \n \(NWError!)")
}
sema.signal()
})))
sema.wait()
}
func receiveUDP() {
self.connection?.receiveMessage { (data, context, isComplete, error) in
if (isComplete) {
if let data = data {
print("Receive is complete : \(data.count)")
} else {
print("Data == nil")
}
}
}
}
}
And here is my test app :
import Foundation
if let udpRequestConnection = UdpConnection(host: "127.0.0.1", port: 10_000) {
print("connection established OK")
if let udpResponseConnection = UdpConnection(host: "127.0.0.1", port: 20_000) {
let data = Data(count: 64)
udpResponseConnection.receiveUDP()
udpRequestConnection.sendUDP(content: data)
print("sent")
}
} else {
print("connection establishing FAILURE")
}
I see no packet received and moreover I see a strange picture in Wireshark :
What am I doing wrong? Why is there an ICMP packet ? What am I missing to get this UDP ?

Ok, it seems that I got the misunderstanding -> NWConnection ctor is responsible for setting outbound connection params. So to listen to UDP stream I need the following implementation :
import Foundation
import Network
import Combine
class UDPListener: ObservableObject {
var listener: NWListener?
var connection: NWConnection?
var queue = DispatchQueue.global(qos: .userInitiated)
/// New data will be place in this variable to be received by observers
#Published private(set) public var messageReceived: Data?
/// When there is an active listening NWConnection this will be `true`
#Published private(set) public var isReady: Bool = false
/// Default value `true`, this will become false if the UDPListener ceases listening for any reason
#Published public var listening: Bool = true
/// A convenience init using Int instead of NWEndpoint.Port
convenience init(on port: Int) {
self.init(on: NWEndpoint.Port(integerLiteral: NWEndpoint.Port.IntegerLiteralType(port)))
}
/// Use this init or the one that takes an Int to start the listener
init(on port: NWEndpoint.Port) {
let params = NWParameters.udp
params.allowFastOpen = true
self.listener = try? NWListener(using: params, on: port)
self.listener?.stateUpdateHandler = { update in
switch update {
case .ready:
self.isReady = true
case .failed, .cancelled:
// Announce we are no longer able to listen
self.listening = false
self.isReady = false
default:
()
}
}
self.listener?.newConnectionHandler = { connection in
self.createConnection(connection: connection)
}
self.listener?.start(queue: self.queue)
}
private func createConnection(connection: NWConnection) {
self.connection = connection
self.connection?.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
self.receive()
case .cancelled, .failed:
// Cancel the listener, something went wrong
self.listener?.cancel()
// Announce we are no longer able to listen
self.listening = false
default:
()
}
}
self.connection?.start(queue: .global())
}
func receive() {
self.connection?.receiveMessage { data, context, isComplete, error in
if error != nil {
return
}
guard isComplete, let data = data else {
return
}
self.messageReceived = data
print("Rx data size = \(data.count)")
if self.listening {
self.receive()
}
}
}
func cancel() {
self.listening = false
self.connection?.cancel()
}
}
With this I am able to get the UDP response I expect.

Related

In Swift, with a class, how to update the connection status?

In order to detect if there is a network connection, I have created a class:
import Foundation
import Network
final class NetworkMonitor: ObservableObject {
static let shared = NetworkMonitor()
private let queue = DispatchQueue(label: "Network") // DispatchQueue.global()
private let monitor: NWPathMonitor
public private(set) var isConnected: Bool = false
public private(set) var connectionType: ConnectionType = .unknown
enum ConnectionType {
case wifi
case cellular
case ethernet
case unknown
}
private init() {
monitor = NWPathMonitor()
}
public func startMonitoring() {
monitor.start(queue: queue)
monitor.pathUpdateHandler = { [weak self] path in
self?.isConnected = path.status == .satisfied
self?.getConnectionType(path)
DispatchQueue.main.async {
self?.objectWillChange.send()
}
}
print("##34355 Etat de la connexion: \(self.isConnected)")
}
public func stopMonitoring() {
monitor.cancel()
}
private func getConnectionType(_ path: NWPath) {
if path.usesInterfaceType(.wifi) {
connectionType = .wifi
} else if path.usesInterfaceType(.cellular) {
connectionType = .cellular
} else if path.usesInterfaceType(.wiredEthernet) {
connectionType = .ethernet
} else {
connectionType = .unknown
}
}
}
I use it like this everywhere in my code in some func after viewDidAppear:
if NetworkMonitor.shared.isConnected == true {
// Do something
}
And I put this in the appDelegate:
NetworkMonitor.shared.startMonitoring()
The connection is detected correctly the first time, but if it changes, the isConnected doesn't work well. he displays the opposite:
When i active the wifi for the simulator, the isConnected is on false, and when i shut it down, he is on true.
I already tried to work with .unsatisfied. but with no success
How could I fix it?

Too many open files using NWListener on macOS

I'm trying to create an app that listens for incoming data on a UDP port using NWListener. This works fine until 248 messages are received - at which point the app crashes with the error message nw_listener_inbox_accept_udp socket() failed [24: Too many open files].
This (I think) relates to the file descriptor limit and so I tried resetting the NWListener within a safe count of 100, but the problem still persists. This seems clumsy and I'm not sure it's actually possible within a receive.
How can I truly reset it such that it releases any open files?
Full code below, which is instantiated within a SwiftUI content view using:
.onAppear() {
udpListener.start(port: self.udpPort)
}
Full class:
import Foundation
import Network
class UdpListener: NSObject, ObservableObject {
private var listener: NWListener?
private var port: NWEndpoint.Port?
#Published var incoming: String = ""
#Published var messageCount: Int = 0
func start(port: NWEndpoint.Port) {
self.port = port
do {
let params = NWParameters.udp
params.allowLocalEndpointReuse = true
self.listener = try NWListener(using: params, on: port)
self.listener?.stateUpdateHandler = {(newState) in
switch newState {
case .ready:
print("ready")
default:
break
}
}
self.listener?.newConnectionHandler = {(newConnection) in
newConnection.stateUpdateHandler = {newState in
switch newState {
case .ready:
self.receive(on: newConnection)
default:
break
}
}
newConnection.start(queue: DispatchQueue(label: "new client"))
}
} catch {
print("unable to create listener")
}
self.listener?.start(queue: .main)
}
func receive(on connection: NWConnection) {
connection.receiveMessage { (data, context, isComplete, error) in
if let error = error {
print(error)
return
}
guard let data = data, !data.isEmpty else {
print("unable to receive data")
return
}
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss.SSSS"
DispatchQueue.main.async {
self.messageCount = self.messageCount + 1
self.incoming = formatter.string(from: date) + " " + String(decoding: data, as: UTF8.self) + "\n" + self.incoming
}
if self.messageCount == 100 {
print("Resetting")
self.listener?.stateUpdateHandler = nil
self.listener?.newConnectionHandler = nil
self.listener?.cancel()
self.listener = nil
self.start(port: self.port!)
}
}
}
}

ORSSerialPort.send doesn't send anything

I am new (very new!!) to swift and straggling to make my UI to send a string over the serial port. I've managed to open the port and read/parse the incoming traffic but when it comes to send a string, nothing is sent.
What I need to do is typing in the sendTextField and when press the SendButton to send the string to serial port. Also, when I print the data which is what I want to send over serial port, it prints the number of bytes I try to send (i.e. 5 bytes). Shouldn't this be the string "Hello" that I try to send to serial port?
I am using Xcode Version 11.2 (11B52) and Swift 5.
Any help will be really appreciated. Thank you in advance!
This is how I call the "send" function:
#IBAction func SendButton(_ sender: Any) {
let TxData = sendTextField.stringValue
SFSerialIn.SendSerialData(TxData)
}
My main program is below:
import ORSSerial
import IOKit
import IOKit.serial
let SFSerialRegexp =
"(?<SFmode>[A-Z]+),\\s*" + "(?<prox>[0-1]),\\s*"
class SFSerialIn: NSObject, ORSSerialPortDelegate {
let path = "/dev/cu.usbserial-AI0484S9"
let baudRate: NSNumber = 115200
var serialPort: ORSSerialPort?
var delegate: SFSerialDelegate?
var stringBuffer = ""
var regex: NSRegularExpression!
var receivedBufferStart = false
override init() {
regex = try! NSRegularExpression(pattern: SFSerialRegexp)
}
deinit {
disconnect()
}
func SendSerialData(_ TxData: String){
let data = Data(TxData.utf8)
serialPort?.send(data)
print(TxData)
print(data)
}
func connect() {
if let serialPort = ORSSerialPort(path: path) {
serialPort.baudRate = baudRate
serialPort.delegate = self
serialPort.open()
} else {
print("Failed to open serial port")
}
}
func disconnect() {
serialPort?.close()
print("closing port...")
}
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
guard let string = String(data: data, encoding: .utf8)
else {
return
}
stringBuffer += string
parseBuffer()
}
func parseBuffer() {
let lines = stringBuffer.split { $0.isNewline }
guard lines.count > 1 else {
return
}
let nextLines = lines[1...].joined()
if !receivedBufferStart {
stringBuffer = nextLines
receivedBufferStart = true
return
}
let line = String(lines[0])
if let matchResult = regex.firstMatch(in: line, range: NSRange(..<line.endIndex, in: line)) {
let sensorFrame = SFFrame(matchResult: matchResult, string: line)
delegate?.receive(sensorFrame: sensorFrame)
stringBuffer = nextLines
return
}
print("Failed to parse line :(")
stringBuffer = nextLines
}
func serialPort(_ serialPort: ORSSerialPort, didEncounterError error: Error) {
print("Serial port encountered error", error)
}
func serialPortWasOpened(_ serialPort: ORSSerialPort) {
print("Serial port opened")
}
func serialPortWasClosed(_ serialPort: ORSSerialPort) {
print("Serial port closed")
}
func serialPortWasRemovedFromSystem(_ serialPort: ORSSerialPort) {
print("Serial port was removed from system")
}
}
protocol SFSerialDelegate {
func receive(sensorFrame: SFFrame)
}
extension StringProtocol {
var data: Data { .init(utf8) }
}
It doesn't look to me like you're ever storing the opened serial port in your serialPort instance property. So, when you do serialPort?.send(data), serialPort is nil, and the ? (optional chaining) operator means that send() isn't called.
Try storing the serial port in your property after opening it:
func connect() {
if let serialPort = ORSSerialPort(path: path) {
serialPort.baudRate = baudRate
serialPort.delegate = self
serialPort.open()
self.serialPort = serialPort
} else {
print("Failed to open serial port")
}
}

How to detect if the internet connection is over WiFi or Ethernet?

Is there a way to open network settings programmatically? Closest thing I know is opening the main settings page:
let settingsURL = NSURL(string: UIApplicationOpenSettingsURLString)!
UIApplication.sharedApplication().openURL(settingsURL)
I want to be able to detect if the internet connection is over WiFi or Ethernet.
The way to detect this is to look at the name of the network interfaces. For Mac and Apple TV, en0 and en1 refer to the wired and wireless interfaces respectively.
Add this to your bridging header (or create one if needed):
#include <ifaddrs.h>
#include <net/if_dl.h>
Then use this Swift code to get the information you need:
struct Networking {
enum NetworkInterfaceType: String, CustomStringConvertible {
case Ethernet = "en0"
case Wifi = "en1"
case Unknown = ""
var description: String {
switch self {
case .Ethernet:
return "Ethernet"
case .Wifi:
return "Wifi"
case .Unknown:
return "Unknown"
}
}
}
static var networkInterfaceType: NetworkInterfaceType {
if let name = Networking().getInterfaces().first?.name, let type = NetworkInterfaceType(rawValue: name) {
return type
}
return .Unknown
}
static var isConnectedByEthernet: Bool {
let networking = Networking()
for addr in networking.getInterfaces() {
if addr.name == NetworkInterfaceType.Ethernet.rawValue {
return true
}
}
return false
}
static var isConnectedByWiFi: Bool {
let networking = Networking()
for addr in networking.getInterfaces() {
if addr.name == NetworkInterfaceType.Wifi.rawValue {
return true
}
}
return false
}
// Credit to Martin R http://stackoverflow.com/a/34016247/600753 for this lovely code
// New Swift 3 implementation needed upated to replace unsafepointer calls with .withMemoryRebound
func getInterfaces() -> [(name : String, addr: String, mac : String)] {
var addresses = [(name : String, addr: String, mac : String)]()
var nameToMac = [ String: String ]()
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return [] }
guard let firstAddr = ifaddr else { return [] }
// For each interface ...
for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
let flags = Int32(ptr.pointee.ifa_flags)
if var addr = ptr.pointee.ifa_addr {
let name = String(cString: ptr.pointee.ifa_name)
// Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
switch Int32(addr.pointee.sa_family) {
case AF_LINK:
nameToMac[name] = withUnsafePointer(to: &addr) { unsafeAddr in
unsafeAddr.withMemoryRebound(to: sockaddr_dl.self, capacity: 1) { dl in
dl.withMemoryRebound(to: Int8.self, capacity: 1) { dll in
let lladdr = UnsafeRawBufferPointer(start: dll + 8 + Int(dl.pointee.sdl_nlen), count: Int(dl.pointee.sdl_alen))
if lladdr.count == 6 {
return lladdr.map { String(format:"%02hhx", $0)}.joined(separator: ":")
} else {
return nil
}
}
}
}
case AF_INET, AF_INET6:
// Convert interface address to a human readable string:
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
if (getnameinfo(addr, socklen_t(addr.pointee.sa_len),
&hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST) == 0) {
let address = String(cString: hostname)
addresses.append( (name: name, addr: address, mac : "") )
}
default:
break
}
}
}
}
freeifaddrs(ifaddr)
// Now add the mac address to the tuples:
for (i, addr) in addresses.enumerated() {
if let mac = nameToMac[addr.name] {
addresses[i] = (name: addr.name, addr: addr.addr, mac : mac)
}
}
return addresses
}
}
Usage is:
debugPrint(Networking.networkInterfaceType)
Or:
switch Networking.networkInterfaceType {
case .Ethernet:
// do something
break
case .Wifi:
// do something else
break
default:
break
}
For iOS 12.0+, tvOS 12.0+, macOS 10.14+ and watchOS 5.0+ apps you can use NWPathMonitor to solve the problem that you described in your question. Add this code to your application(_:didFinishLaunchingWithOptions:) implementation (Swift 5.1.3/Xcode 11.3.1):
let pathMonitor = NWPathMonitor()
pathMonitor.pathUpdateHandler = { path in
if path.status == .satisfied {
if path.usesInterfaceType(.wifi) {
print("wifi")
} else if path.usesInterfaceType(.cellular) {
print("cellular")
} else if path.usesInterfaceType(.wiredEthernet) {
print("wiredEthernet")
} else if path.usesInterfaceType(.loopback) {
print("loopback")
} else if path.usesInterfaceType(.other) {
print("other")
}
} else {
print("not connected")
}
}
pathMonitor.start(queue: .global(qos: .background))
And don't forget to add import Network to the top of the file.
You can use Reachability API.
let reachability: Reachability = Reachability.reachabilityForInternetConnection()
(reachability.currentReachabilityStatus().value == ReachableViaWiFi.value) // For WiFi
(reachability.currentReachabilityStatus().value == ReachableViaWWAN.value) // For WWAN
(reachability.currentReachabilityStatus().value == NotReachable.value) // For No Internet

How to read data from NSInputStream explicitly in swift?

I am using a socket connect in my application.
Here's my SocketConnection.swift
init(host: String, port:UInt32){
self.host = host
self.port = port
self.status = false
output = ""
super.init()
}
func stream(aStream: NSStream, handleEvent aStreamEvent: NSStreamEvent) {
switch aStreamEvent {
case NSStreamEvent.OpenCompleted:
break
case NSStreamEvent.HasBytesAvailable:
break
case NSStreamEvent.HasSpaceAvailable:
break
case NSStreamEvent.EndEncountered:
// aStream.close()
aStream.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
break
case NSStreamEvent.None:
break
case NSStreamEvent.ErrorOccurred:
break
default:
println("# something weird happend")
break
}
}
func connect() {
println("# connecting to \(host):\(port)")
var cfReadStream : Unmanaged<CFReadStream>?
var cfWriteStream : Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &cfReadStream, &cfWriteStream)
inputStream = cfReadStream!.takeRetainedValue()
outputStream = cfWriteStream!.takeRetainedValue()
inputStream!.delegate = self
outputStream!.delegate = self
inputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
inputStream!.open()
outputStream!.open()
}
func read(){
var buffer = [UInt8](count: bufferSize, repeatedValue: 0)
output = ""
while (self.inputStream!.hasBytesAvailable){
var bytesRead: Int = inputStream!.read(&buffer, maxLength: buffer.count)
if bytesRead >= 0 {
output += NSString(bytes: UnsafePointer(buffer), length: bytesRead, encoding: encoding)! as String
} else {
println("# error")
}
println("> \(output)")
}
}
func send(message:String){
let data:NSData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
let bytesWritten = self.outputStream!.write(UnsafePointer(data.bytes), maxLength: data.length)
println("< send to \(host)")
}
In my ViewController.swift,
I am connecting to the server like this
var socketConnection = SocketConnection(host: _ip, port: _port)
socketConnection.connect()
socketConnection.send(urlString)
socketConnection.read()
Now I can send my url string via socket but when I am reading explicitly I am not getting the data from the server if I call the same read function from the NSStreamEvent.HasBytesAvailable case it printing the server response.. but how can I trigger the event queue?
I want to call this socketConnection.read() explicitly.. How can I do that?
After 2 sec of connection, its closes the connection channel, I want to keep alive my connection until I close.
Help me out from this problem.
Thanks
Change your read() as below and then call read() func in case NSStreamEvent.HasBytesAvailable:
private func read(stream: InputStream) {
let maxReadLength = 1024
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: maxReadLength)
while stream.hasBytesAvailable {
let numberOfBytesRead = inputStream?.read(buffer, maxLength: maxReadLength)
if numberOfBytesRead! < 0 {
if let _ = inputStream?.streamError {
break
}
}
if let message = processedMessageString(buffer: buffer, length: numberOfBytesRead!) {
delegate?.receivedMessage(message: message)
}
}
}
You do not need to call read functionality explicitly. Change your read func as below and call in case case Stream.Event.hasBytesAvailable:.
func read(stream: InputStream) {
let maxReadLength = 1024
var buffer = [uint8](repeating: 0, count: maxReadLength)
while stream.hasBytesAvailable {
let numberOfBytesRead : Int = stream.read(&buffer, maxLength: maxReadLength)
if numberOfBytesRead < 0 {
if let _ = stream.streamError {
break
}
}
if let message = processedMessageString(buffer: buffer, length: numberOfBytesRead) {
delegate?.receivedMessage(message: message)
}
}}
If you want to call read() explicitly then you need to pass the stream as parameters and call the read function in ViewController class—
socketConnection.read(stream)