To send a binary message to a TCP server in Swift, you can use the Socket
class from the Foundation
framework. Here's an example of how to do it:
main.swift671 chars29 lines
In this example, we create a TCP socket and connect to a server running on localhost
at port 1234
. We then prepare some binary data using the Data
class and write it to the socket using the write(from:)
method. Finally, we close the socket.
Note that this is just a simple example and you'll probably want to add error handling and other features if you're building a real client/server application.
gistlibby LogSnag