# Connecting to a UDP data stream with Mathematica

I have an app on my iPhone called iSeismometer which reads the iPhone's accelerometers and acts as a server which streams this data via UDP (I can set the IP address and port number). The question is how to read this data stream with Mathematica? Apparently, Dreeves has been looking into this 12 years ago, so I imagine something must have happened in the meantime.

**Update**

I got two great answers so far; one from WReach and one from Mark McClure. Both are using JLink to get at the data. This seems like a fine approach. However, I was reminded of some work I did on the WII balance board. Using a few free programs (GlovePIE and PPJoy) I got this bluetooth peripheral to appear as a joystick to Windows, and therefore also to Mathematica (via `ControllerState`

). Of course, bluetooth and UDP are quite different, but could something along the same lines be made to work too?

## Oliver-

2019-11-13

JLink is definitely the way to go. I prefer to keep my Java code and my Mathematica code separate by compiling a Java programwhich I then call from Mathematica. I set up a Notebook and companion Java program that you can grab here: http://facstaff.unca.edu/mcmcclur/UDPFiles.tar.gz

Here is the essential Mathematica code:

The updReader class is defined by the following Java code.

Note that you can use the main method of the myClient class to check that your setup is working without Mathematica, essentially taking one potential issue out of the loop.

## Oscar-

2019-11-13

Assuming the set-up discussed in a blog entry on the iSeismometer web site, a couple of options come to mind.

ImportThe first option would be to use an external program to capture the packets, and then use

`Import`

to bring in the results, e.g.Alas, the Python program mentioned in the blog post will not work well here since it runs in an endless loop that must be manually terminated. The

`Import`

approach would only work if that program were modified to stop after a fixed number of packets or a time limit or something.JLinkAn alternate approach can be implemented without leaving the comfy Mathematica environment by using JLink. Well, perhaps it is a stretch to say that we are staying within Mathematica since a fair amount of funny-looking Java code is mixed in with the Mathematica code. Nevertheless, it does illustrate the utility of the built-in Java distribution that ships with every copy of Mathematica:

Some shortcuts have been taken with respect to exception handling, packet decoding and the like in order to keep this example at a manageable length.

`ListenToISeismometer`

needs to be given the UDP port number to listen upon. Let's use the same port as in the blog post, 10552:The function will listen to all UDP events on that port until told to stop. A button is presented for this purpose, with each packet flashing by along side as received. When the button is pressed, the function returns a list of the packets received:

JLink makes this

possible, but there is no escaping the fact that the use of JLink requires a working knowledge of Java.