4

I am new to Swift and have some questions about UDP connections.

Could someone provide a link or some short lines of code showing how I can connect a Swift client to a Java server?

user123
  • 55
  • 5
user3838885
  • 41
  • 1
  • 1
  • 5
  • 2
    Probably want to use this - https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html#//apple_ref/doc/uid/CH73-SW12 – admdrew Sep 16 '14 at 18:43
  • `Swift example code?` Can you post *your* code that you're currently having issues with? Otherwise, this is probably off-topic (either because you're asking someone to write code for you, or are looking for an off-site resource). – admdrew Sep 16 '14 at 19:01
  • I am sorry but at the moment I have a TCP Connection between a Java Server and a Swift Client. After coding some lines I relizaded that I need a UDP Connection. – user3838885 Sep 16 '14 at 19:20

2 Answers2

3

You can just use the relevant C functions, from the 'Darwin' module. The part which is a bit tricky is the casting from sockaddr_xyz structs to the generic sockaddr (maybe someone has a better solution than mine ...). Otherwise it is pretty straight forward.

Updated for Swift 0.2 aka Xcode 6.3.1 (strlen() must be converted to Int).

Sample:

let textToSend = "Hello World!"

func htons(value: CUnsignedShort) -> CUnsignedShort {
  return (value << 8) + (value >> 8);
}

let INADDR_ANY = in_addr(s_addr: 0)

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(1337),
  sin_addr:   INADDR_ANY,
  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, Int(strlen(cstr)), 0,
           addrptr, socklen_t(addr.sin_len))
  }
}
hnh
  • 13,957
  • 6
  • 30
  • 40
  • Oh, and I have a few convenience extensions for in_addr and such over here: https://github.com/AlwaysRightInstitute/SwiftSockets/blob/master/ARISockets/SocketAddress.swift – hnh Sep 17 '14 at 12:30
  • 1
    not sure if something has changed since this was posted but i needed to make `cstr` a Void pointer like so `let string = UnsafePointer(cstr)` and also cast the `strlen(cstr)` to an Int: `Int(strlen(cstr))` and used in the function: `sendto(fd, string, Int(strlen(cstr)), 0, addrptr, socklen_t(addr.sin_len))` – Fonix May 18 '15 at 02:55
  • Thanks, did the Int() change for the current Swift. But for me it works w/o the Void cast, presumable UnsafePointer transparently converts to UnsafePointer (Xcode 6.3.1). – hnh May 18 '15 at 14:08
1

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

Ash
  • 45
  • 9