UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

2      

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!)
  }

2      

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

2      

Thanks, appreciate the help.

Do you know how to post an image?

2      

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

2      

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

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

2      

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?

2      

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.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.