Reputation: 2863
I want to develop UDP client and send data in Swift.
I have reference the following link:
Swift: Receive UDP with GCDAsyncUdpSocket
Retrieving a string from a UDP server message
But I could not find a good way to implement UDP
in Swift
.
Can someone teach me How to implement UDP client and send data in Swift on iPhone?
Upvotes: 7
Views: 34591
Reputation: 11646
very nice sample! My two cents.. fixed for swift 4.2. / Xcode 10, (I love using 20 lines instead of a bunch of other files.. and Pods.. ). Added some more returned results.
import Foundation
func udpSend(textToSend: String, address: in_addr, port: CUnsignedShort) {
func htons(value: CUnsignedShort) -> CUnsignedShort {
return (value << 8) + (value >> 8);
}
let fd = socket(AF_INET, SOCK_DGRAM, 0) // DGRAM makes it UDP
let addr = sockaddr_in(
sin_len: __uint8_t(MemoryLayout<sockaddr_in>.size),
sin_family: sa_family_t(AF_INET),
sin_port: htons(value: port),
sin_addr: address,
sin_zero: ( 0, 0, 0, 0, 0, 0, 0, 0 )
)
let sent = textToSend.withCString { cstr -> Int in
var localCopy = addr
let sent = withUnsafePointer(to: &localCopy) { pointer -> Int in
let memory = UnsafeRawPointer(pointer).bindMemory(to: sockaddr.self, capacity: 1)
let sent = sendto(fd, cstr, strlen(cstr), 0, memory, socklen_t(addr.sin_len))
return sent
}
return sent
}
close(fd)
}
Upvotes: 1
Reputation: 22397
for me, I used this, and its usage:
broadcastConnection = UDPBroadcastConnection(port: 35602) { [unowned self] (response: (ipAddress: String, port: Int, response: [UInt8])) -> Void in
print("Received from \(response.ipAddress):\(response.port):\n\n\(response.response)")
}
Upvotes: 4
Reputation: 14795
This looks like a dupe to Swift UDP Connection.
Refactoring the example a little bit:
let INADDR_ANY = in_addr(s_addr: 0)
udpSend("Hello World!", address: INADDR_ANY, port: 1337)
func udpSend(textToSend: String, address: in_addr, port: CUnsignedShort) {
func htons(value: CUnsignedShort) -> CUnsignedShort {
return (value << 8) + (value >> 8);
}
let fd = socket(AF_INET, SOCK_DGRAM, 0) // DGRAM makes it UDP
var addr = sockaddr_in(
sin_len: __uint8_t(sizeof(sockaddr_in)),
sin_family: sa_family_t(AF_INET),
sin_port: htons(port),
sin_addr: address,
sin_zero: ( 0, 0, 0, 0, 0, 0, 0, 0 )
)
textToSend.withCString { cstr -> Void in
withUnsafePointer(&addr) { ptr -> Void in
let addrptr = UnsafePointer<sockaddr>(ptr)
sendto(fd, cstr, strlen(cstr), 0, addrptr, socklen_t(addr.sin_len))
}
}
close(fd)
}
If you have the target IP as a string, use inet_pton() to convert it to an in_addr. Like so:
var addr = in_addr()
inet_pton(AF_INET, "192.168.0.1", &buf)
Feel free to steal code from over here: SwiftSockets
Oh, and if you plan to do any serious network programming, grab this book: Unix Network Programming
Upvotes: 1
Reputation: 45
After pretty extensive research coming up with only basic pointers towards GCD's AsyncSocket, and being a pure Swift trained iOS programmer, I looked for something with native Swift socket support.
Thankfully, I found a much simpler alternative to using Async, called SwiftSocket.
The github (https://github.com/xyyc/SwiftSocket) has examples for most sockets, and is ready to use by copying just a few files into a project.
For swift-only devs, I feel its underutilized and will quickly replace Async for non-objective-c apps. Then again I'm pretty new to this stuff so I may be way off :D
Upvotes: 0