0

I would like to write an iOS App using XCODE Swift 3 to test UDP communicate with a general TCP/UDP terminal testing program (which is an app installed in another cell phone). Considering all devices are in the same Wifi network, the UDP terminal testing program is listening to a port number 12333, and the iOS App will have a button showing on screen. Once the button is pressed, a UDP broadcast message is sent. This message should then be received and displayed on the UDP terminal testing program.

I found a link which describe UDP broadcast issue, but it was written in objective-C. Then I tried to modify the code using Swift 3, I failed in modifying the following two lines of codes.

// Problem: Swift 3 doesn't have the function "htons()"
"broadcastAddr.sin_port = htons(8585)" 

// Problem: cannot convert value of type "sockaddr_in" to expected argument type "sockaddr"
ret = sendto(socketSD, request, strlen(request), 0, (struct sockaddr*)&broadcastAddr, sizeof(broadcastAddr));

Is there anybody can help on the issue? Or am I in the right direction in testing the UDP communication?

piet.t
  • 11,718
  • 21
  • 43
  • 52
user llm
  • 1
  • 4
  • The is the link i am referring to: https://gist.github.com/chancyWu/8349411 – user llm Jul 25 '17 at 06:36
  • You would be better off using some other framework rather than trying to convert C code - https://stackoverflow.com/questions/28295486/how-to-implement-udp-client-and-send-data-in-swift-on-iphone – Paulw11 Jul 25 '17 at 06:46
  • Do you mean the SwiftSocket project? I copied the SwiftSocket source files to my project. When i start using it by typing "import SwiftSocket", the xcode said "No such module". How can I use this SwiftSocket for testing the UDP transmission? – user llm Jul 25 '17 at 07:34
  • Is it available as a pod? – Paulw11 Jul 25 '17 at 08:47

0 Answers0