Best iOS Framework for Advanced network usage? - objective-c

I need a network framework that can post raw data such as 0x01. or any raw data i need posted to a server. Not just a HTTP request. I can't seem to find any network frameworks like this. Also, If there are none that currently exist. How would i go about writing the sockets? I can't seem to get that down. which is why i am looking for a existing framework. Thanks for any help. I have tried using sockets but, I was unable to convert the CFArrayRef. I tried many things, But, It crashed every time i tested. So, I am running out of options but to use a prebuilt framework.
Note, I do not want to do only a request like:
POST / HTTP/1.1
Host: localhost
Content-Length: 4
0x01
No, I would like to send RAW data so, The following would be sent to the remote server through a socket.
0x01
This would not be visible raw text however, It would be converted to a data string then sent. I know how to convert most of the stuff i need to do, Its the sending and receiving the response I need the framework for or something that'd work. Any tutorials online or examples on how to do something similar that I can modify to fit to my needs would be greatly appreciated.
How do i send raw data to a socket?
Where do i find a framework like this?
Can you provide tutorials that I can modify to fit my needs?

The best framework for this is CocoaAsyncSocket. While it allows sending arbitrary data on the socket, it also provides very useful abstraction of much of the busywork of socket management. Look particularly a the GCD version that replaces the older RunLoop based code.
I've built several low-level protocols on this stack. I recommend it highly.

You can either use raw sockets or the CFStream API. I recommend the latter.

Related

Play audio stream using WebAudio API

I have a client/server audio synthesizer where the server (java) dynamically generates an audio stream (Ogg/Vorbis) to be rendered by the client using an HTML5 audio element. Users can tweak various parameters and the server immediately alters the output accordingly. Unfortunately the audio element buffers (prefetches) very aggressively so changes made by the user won't be heard until minutes later, literally.
Trying to disable preload has no effect, and apparently this setting is only 'advisory' so there's no guarantee that it's behavior would be consistent across browsers.
I've been reading everything that I can find on WebRTC and the evolving WebAudio API and it seems like all of the pieces I need are there but I don't know if it's possible to connect them up the way I'd like to.
I looked at RTCPeerConnection, it does provide low latency but it brings in a lot of baggage that I don't want or need (STUN, ICE, offer/answer, etc) and currently it seems to only support a limited set of codecs, mostly geared towards voice. Also since the server side is in java I think I'd have to do a lot of work to teach it to 'speak' the various protocols and formats involved.
AudioContext.decodeAudioData works great for a static sample, but not for a stream since it doesn't process the incoming data until it's consumed the entire stream.
What I want is the exact functionality of the audio tag (i.e. HTMLAudioElement) without any buffering. If I could somehow create a MediaStream object that uses the server URL for its input then I could create a MediaStreamAudioSourceNode and send that output to context.destination. This is not very different than what AudioContext.decodeAudioData already does, except that method creates a static buffer, not a stream.
I would like to keep the Ogg/Vorbis compression and eventually use other codecs, but one thing that I may try next is to send raw PCM and build audio buffers on the fly, just as if they were being generated programatically by javascript code. But again, I think all of the parts already exist, and if there's any way to leverage that I would be most thrilled to know about it!
Thanks in advance,
Joe
How are you getting on ? Did you resolve this question ? I am solving a similar challenge. On the browser side I'm using web audio API which has nice ways to render streaming input audio data, and nodejs on the server side using web sockets as the middleware to send the browser streaming PCM buffers.

Twisted - succes (or failure) callback for LineReceiver sendLine

I'm still trying to master Twisted while in the midst of finishing an application that uses it.
My question is:
My application uses LineReceiver.sendLine to send messages from a Twisted TCP server.
I would like to know if the sendLine succeeded.
I gather that I need to somehow add a success (and error?) callback to sendLine but I don't know how to do this.
Thanks for any pointers / examples
You need to define "succeeded" in order to come up with an answer to this.
All sendLine does immediately (probably) is add some bytes to a send buffer. In some sense, as long as it doesn't raise an exception (eg, MemoryError because your line is too long or TypeError because your line was the number 3 instead of an actual line) it has succeeded.
That's not a very useful kind of success, though. Unfortunately, the useful kind of success is more like "the bytes were added to the send buffer, the send buffer was flushed to the socket, the peer received the bytes, and the receiving application acted on the data in a persistent way".
Nothing in LineReceiver can tell you that all those things happened. The standard solution is to add some kind of acknowledgement to your protocol: when the receiving application has acted on the data, it sends back some bytes that tell the original sender the message has been handled.
You won't get LineReceiver.sendLine to help you much here because all it really knows how to do is send some bytes in a particular format. You need a more complex protocol to handle acknowledgements.
Fortunately, Twisted comes with a few. twisted.protocols.amp is one: it offers remote method calls (complete with responses) as a basic feature. I find that AMP is suitable for a wide range of applications so it's often safe to recommend for new development. It largely supersedes the older twisted.spread (aka "PB") which also provides both remote method calls and remote object references (and is therefore more complex - in my experience, more complex than most applications need). There are also some options that are a bit more standard: for example, Twisted Web includes an HTTP implementation (HTTP, as you may know, is good at request/response style interaction).

Sending data packets over udp

I am creating an app that acts as a remote control for a lighting console and I need to send commands to the console over UDP. The protocol that I am using has its own custom header. How do I create the data packet with header and message to send over UDP? Thanks!
If you are trying to test the protocol, without writing any code, I suggest you use WireShark.
The probably most powerful solution you can use is scapy, which is a python module that allows very advanced packet crafting and manipulation. See its documentation or search the interwebs for examples to find out how to generate arbitrary packets and transmit them.
If you can't use python for some reason, there are multiple command line tools for packet generation, one other example being nping (documentation), the brother of nmap, the popular network scanner. nping has options to generate UDP packets with arbitrary payloads, with can be specified as a hex string, for example.
There may be other options as well. It would be good to know more details like the operating system you're on or where you get your input data from, and in which format.

Socket programming and telnet with VB.net

I'm writing a GUI-based app in VB.net that talks to a LambdaMOO server via telnet, sends commands to display the object hierarchy, then parses the output and creates a visual representation of the object hierarchy.
So my question is: is there some kind of "telnet client" class for .NET to simplify the sending and receiving of data, or do I have to write my own using the socket API?
Does Mono have something like this?
Barring an easy solution, does anyone have a good tutorial they can point to for telnet client programming in VB.net?
Ok, I had a similar issue and ignoring all security complications and the like, wanted to TELNET from a VB initiated connection to a remote device and do stuff. I concur that the whole negotiation process is a hellish thing to do but once you've worked it out it's actually pretty simple to implement. I decided not to stop because I kept reading things that said it couldn't be done when it clearly can be done if you can write and read 1's and 0's into/from a network stream.
The code in the link below will initiate the connection and get you through to actually exchanging clear text information over TELNET. Given the example of sending a username and password combo shows how to read and write to the connection.
Big tips - initially have a nice big textbox or something to trap everything that comes into the buffer (variable returndata). This will help you diagnose problems. Also check on my blog there how to do this without the textbox blinking like a flashing thing. Once you've done all that and you know your script is reliable, trun off any screen updates and it will whizz through rather than take an age.
Apologies for the really dirty code and the crappy website layout.
http://myhead-online.blogspot.com/2009/05/vb-net2008-express-telnet-to-sun.html
The telnet protocol is basically just the usual TCP protocol, with a bunch of optional stuff that you probably won't need to implement. So you'd open a socket and start sending and receiving data with the socket stream interface.
Give it a try with the regular socket API, you'll probably find that it's quite straightforward.
just a suggestion. you may try to program your vb application to execute an existing telnet application in batch mode.
here is the link for your reference. refer to 7.3 Using Plink in batch files and scripts. Hope it helps.
You can grab one of any number of libraries to use. Here's one library:
LINK
For others try googling something along the lines of: library telnet mud .NET
Lastly, there are any number of opensource MUD/MOO/MUSH projects open at any time who are willing the share ideas and looking for people to help with projects.
I had my trials with telnet. You've to use tools like wireshark in conjuntion to figure out what commands needs to be initiated. I did find communicating with my unix box quite a challenge. For one thing you must know your telnet instructions. You might find it difficult to determined the state of the application - whether it is logged in or not innately. You'd have to formulate your own logic for it.
Another thing you'd have to do is parse the bytes returned by telnet into commands or instruction data i.e. you have to know if the bytes received is an instruction or some other thing it is trying to send you. Here is a ref that would come in handy.
First I suggest you start using the wireshark tool and get the communications send to and fro manually as well as via application.
From the LambdaMOO end of things, if you have wizard access or are friends with someone who does, you can have the MOO give you the data over another protocol that you might be able to work with more readily, such as HTTP. All you need is an object on the MOO with a do_login_command() set to handle requests, and then use the listen() builtin to get that object to listen on a given port. As long as a protocol doesn't require anything complex SSL, it's fairly easy to code up on the MOO end. So that might be worthwhile if VB.net has easier handling for HTTP etc.

Objective-C networking - best practices?

I'm building an Objective-C app that has both a server and a client. The client can send updates to the server, and the server needs to be able to send updates to each connected client. I've been thinking about how best to implement this system, but am asking for your suggestions.
Currently, I'm thinking that when new updates are available, the server will use threads to send the update to each client in turn. If a client times out, they are disconnected.
I have very little networking experience, so am asking your insight.
Do you think that this system would work well?
If so, do you have any suggestions about how to do the threading? Any NS classes you can point me at? There's got to be some kind of queue I can use, I'm thinking.
Any other thoughts?
EDIT: I do not expect the client count to get much above 50 or so, at the max.
As long as both client and server are OS X apps and can both be written in Objective-C using the Cocoa frameworks, I would highly recommend you take a look at the Distributed Objects (DO) technology in Cocoa. I won't try to give a tutorial in Distributed Objects here, just explain why it might be useful...
DO handles asynchronous network details for you (all your client updates could happen on a single thread). In addition the semantics of communication with a remote object (client to server or visa versa; DO is bidirectional once the connection is established) are very similar to in-process communication. In other words, once you have a reference to the remote object (really an NSDistantObject which acts as a proxy to the object on the other end of the connection), your client code can send messages to the remote object as if it were local:
[remoteServer update:client];
from the client or
[[remoteClientList objectAtIndex:i] update:server];
from the server. I'll leave the details of setting up the connection and for getting the remoteServer or remoteClient reference to you after reading the Distributed Objects programming guide.
The downside of using DO is that you are tied to Cocoa; it will be very difficult to write a non-Cocoa client or server that communicates using Distirbuted Objects. If there's a chance you may want to have non-Cocoa client or server implementations, you should not use DO. In this case, I would recommend something simple with a lot of cross-platform and language support. A REST-style API over HTTP is a good option. Have a look at the Cocoa URL Loading System documentation for info on how to implement HTTP requests and responses. Have a look at Apple's CocoaHTTPServer example code or a code.google.com project of the same name for info on implementing an HTTP server in your Cocoa code.
As a very last option, you can take a look at the Cocoa Stream Programming Guide if you want to implement your own network protocol. NSStream's subclasses will let you listen on a network socket and handle asynchronous reads/writes to/from that socket. A lot of people use AsyncSocket for this purpose. It wraps the (lower-level) CFStream and CFSocket and makes writing network code somewhat easier.
When the server sends updates to the clients, it would probably be easier to just have one thread handle them all, and just use async sockets. Of course this would depend on how many clients you had to deal with too.
There's several networking examples in the apple developer side.
One I would recommend that you check out is the URLCache, which can be downloaded.
Quoting from the Apple's documentation for this example:
URLCache is a sample iPhone application that demonstrates how to download a resource off the web, store it in the application's data directory, and use the local copy of the resource. URLCache also demonstrates how to implement a couple of caching policies:
An interesting option is the BLIP protocol from Jens Alfke. It's like a stripped down version of BEEP: a message oriented networking system. It basically provides the low-level abstractions for a bidirectional message pipe so you can concentrate on layering your communication protocol on top of it.
It has some worthy followers such as Marcus Zarra (author of the CoreData bible) and Gus Mueller of Flying Meat software.
I don't know how you plan to design you system, but usually a server cannot connect to a client; the client must initiate the communication. With a low limit of 50 clients, you may not be looking at a web-server/client-like implementation...
That said, there are basically two ways to handle client server communication:
1. The client polls the server periodically to get updates
2. The client keeps a connection open to the server and the the server responds with a well known (as in both sides understand it) protocol.