WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Network framework example

Forums > Swift

Good afternoon

Has anyone stumbled upon a good Network framework example to send/receive data over TCP?

Just found this example which sends only:

https://gist.github.com/ura14h/6f254f0f972f2cc430521acd6081e482

Like a simple:

klingler@Mac-mini ~ % telnet 10.0.5.1 5555
Trying 10.0.5.1...
Connected to 10.0.5.1.
Escape character is '^]'.
*IDN?    <- sent
RIGOL TECHNOLOGIES,DS1104Z Plus,DS1ZC223303310,00.04.04.SP4    -> received
^]
telnet> q
Connection closed.

thanks in advance

richard

   

Okay..found a solution which can receive the data....

Basically two classes called Connection() and Client().....but Connection() is used by Client() only....

Now the problem is that Connection() class gets the receiving data. So how do I get back the data into SwiftUI where I trigger the sending of a command like:

let client = Client()
let command = "*IDN?\n".data(using: .utf8)
Button("Connect") {
      print("Button Connect")
      client.start()
  }
  Button("Send") {
      print("Button Send")
      client.connection.send(data: command!)
  }

   

Hi, I'm new to the forum and am still figuring how the various post function works. Are you using the code block function to post your code or is that a screen shot because I see that it is color-coded? If it is a screenshot can you tell me how i can insert an image in my post? The insert image function provides a place-holder for me to insert a url but I'm not sure what to do with that.

Thanks in advance

   

Thanks, appreciate the help.

Do you know how to post an image?

   

Percival tells us some fun info:

Hi, I'm new to the forum and am still figuring how the various post function works.

and asks:

Are you using the code block function to post your code or is that a screen shot because I see that it is color-coded? If it is a screenshot can you tell me how i can insert an image in my post?

First rule of Forums!

Use the search bar! At the top of each forum section is a search bar. Give it a try! Enter the text "post picture" or something similarly inquisitive! Voila! Results!

Second rule of Forums.

Don't hijack a forum question. This question is about network frameworks. You turned it into a question about posting graphics in a forum. Stop!

Instead, create your own question. But before you do, see First rule of Forums!

See-> How to post code

   

From Apple Developer Tech Support person Quinn "The Eskimo":

https://forums.swift.org/t/socket-api/19971/6

   

Well the snippet is exactly from there (o;

The problem is how to get the received data back from the Connection() Class which is called by the Client() Class...

I assume the Observable property can only be in the Client() Class, but it has no way of knowing when data was received (if any).

Maybe rewrite those two classes into one is the only way?

   

Eskimo uses his Client class to instantiate his Connection class. You can consider the Client class to be the view model. If you already have a view model class, you can let your view model instantiate the Client as an @Published var, or you can eliminate Client as a separate class by moving its methods and properties to your view model. In any of these 3 alternatives, only the view model needs to be an ObservableObject.

Eskimo's method for handling received data is the closure that is the final parameter of self.nwConnection.receive() within Connection.setupReceive(). Eskimo merely prints the data. You need to store it in a property. If you make it a property of Connection, your view model will have a reference to it because that's where the Connection instance is stored.

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.