Arbitrary binary data with TcpSender?

Jan 9, 2008 at 9:38pm

Arbitrary binary data with TcpSender?

I need to build an object that can send arbitrary binary data via TCP. I was hoping to use mxj, in particular com.cycling74.net.TcpSender for this purpose. But on closer examination, TcpSender only seems to grok standard Max data types.-(

I need to send largish (2-4kB) data structures of binary data to custom hardware, and the data simply does not parse into Atoms. Ideally I would like to simply pass a byte[] to a TcpSender, which does everything else I need quite handily. Is this possible? (Preparing the stream of bytes is not the problem, at least not yet!)

Thanks — P

#35295
Jan 9, 2008 at 9:52pm

Why don’t you encode your byte array into a hex string and send that?
Its pretty straightforward.
T

Peter Castine wrote:
> I need to build an object that can send arbitrary binary data via TCP. I was hoping to use mxj, in particular com.cycling74.net.TcpSender for this purpose. But on closer examination, TcpSender only seems to grok standard Max data types.-(
>
> I need to send largish (2-4kB) data structures of binary data to custom hardware, and the data simply does not parse into Atoms. Ideally I would like to simply pass a byte[] to a TcpSender, which does everything else I need quite handily. Is this possible? (Preparing the stream of bytes is not the problem, at least not yet!)
>
> Thanks — P
>
>
> –
> Peter Castine
> –
> Peter is in dire need of a new Facebook tagline. Not to mention a new .sig
>
>

#120176
Jan 9, 2008 at 10:06pm

If you want to send arbitrary binary data over TCP, then you’re
basically working at the level that the Java API provides (the stuff
in java.net.* specifically). I don’t see why you’d want to link
against the com.cycling74 classes. (TcpSender seems to be a wrapper
around Socket anyway, with some sanitisation for Atoms and
InetAddresses.)

– N.

Nick Rothwell / Cassiel.com Limited
http://www.cassiel.com
http://www.myspace.com/cassieldotcom
http://www.last.fm/music/cassiel
http://www.reverbnation.com/cassiel
http://www.linkedin.com/in/cassiel
http://www.loadbang.net

#120177
Jan 9, 2008 at 10:59pm

Thanks to both Topher and Nick. In the mean time I’ve been looking at java.net.Socket, but that’s also meant importing add’l packages to catch exceptions and other fun stuff. It looks like I need java.net.*, java.lang.*, and java.io.* as well as the Cycling74 packages.

I’m sure it’ll all seem easy once I’ve figured it out, but a lot of this is new ground for me.

Given the size of the data (I misspoke previously, it’s more like 7-8kB), I was a little unsure of the binary string approach. I was thinking (probably incorrectly) that if I were to do something like TcpSender.send(String s, Atom[] a), the contents of the String would have to go into the Max symbol table (the signature _looks_ like a Max message starting with a symbol). But if the Max symbol table doesn’t need to know about what data is being used inside an mxj object, than that isn’t a problem.

I’ll try to get the coding finished tomorrow and can show some code if I’m still stuck.

Thanks again.

– P

#120178
Jan 10, 2008 at 10:11am

> In the mean time I’ve been looking at java.net.Socket, but that’s
> also meant importing add’l packages to catch exceptions and other
> fun stuff. It looks like I need java.net.*, java.lang.*, and
> java.io.* as well as the Cycling74 packages.

java.lang.* you get for free; but anyway, if you’re using Eclipse, at
least, management of import statements is one key shortcut away. Yes,
you have to deal with exceptions (or just let the MXJ instance’s
entry points propagate the exceptions); it’s just one of the ways in
which Java sometimes requires a bit (or a lot) of boilerplate.

> I was thinking (probably incorrectly) that if I were to do
> something like TcpSender.send(String s, Atom[] a), the contents of
> the String would have to go into the Max symbol table

I would guess, by analogy with the C/C++ API, that Atom.newAtom
(String) would hash the string into a symbol. But otherwise, if
you’re just in the Java world, Max won’t see it: just you, me and the
Sheriff will know about it.

– N.

nick rothwell — composition, systems, performance — http://
http://www.cassiel.com

#120179
Jan 16, 2008 at 5:43pm

On a note of closure: Java Sockets work OK. There is more work involved than with Cycling’s TcpSender, but it’s manageable.

Leastaways the code I’ve written seems to be working now.

Thanks also to Brad for some tips offlist.

– P.

#120180
Nov 24, 2013 at 4:23am

Hey Peter, I’m trying the same thing with limited luck; still there’s no standard way in max to send raw bytes via tcp using java.

Are you willing to post your 5-year-old java code?
Will save me reinventing the wheel :)

#273635

You must be logged in to reply to this topic.