<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 52: Jitter Networking">
<setdocpatch name="Jitter Networking" patch="52jNetworking.maxpat"/>

<previous name="jitterchapter51">Jitter Java</previous>
<next name="jitterchapter99_appendixa">Jitter Appendix A</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 52: Jitter Networking</h1>
<p>The <o>jit.net.send</o> object allows us to send uncompressed matrices over the Internet to a <o>jit.net.recv</o> object in a Max patch on a different computer.  </p>

<bullet>Open the tutorial patch. Note that there are two included patchers, <m>52jNetworkSend</m> and <m>52jNetworkReceive</m>. Open them both. (If you like, you can open copies on different computers.)</bullet><br/>
<p>The <o>jit.net.send</o> and <o>jit.net.recv</o> objects communicate using the TCP protocol.  A discussion of the different types of network transmission protocols is beyond the scope of this Tutorial; it's important to note, however, that TCP is a <i>connection-oriented</i> protocol. Before anything can be sent or received the pair of objects must make a connection to one another.  Both objects report any change in their connection status out the dump outlet with a <m>connected</m> message, followed by an argument of <m>1</m> when connected or <m>0</m> when disconnected.  </p>

<illustration><img src="images/jitterchapter52a.png"/>A patch to send Jitter matrices over a computer network using TCP.</illustration>
<p>To form a connection, the <o>jit.net.recv</o> and <o>jit.net.send</o> objects must be set to link to the same <i>port</i>.  We can set the <m>port</m> attribute to any positive integer value, but some of the lower numbers are reserved for commonly used network services such as FTP, web browsing, etc. Watch the Max console for error messages when you change ports&#x2014;no message means the connection is valid. A port in the range above 1024 and below 10000 will probably be safe.  If no port is specified, the default for both objects is 7474.</p>

<p>In addition to <m>port</m> number, the <o>jit.net.send</o> object must know the <i>IP address</i> of the computer running the Max patch containing the <o>jit.net.recv</o> object we'd like to send to.  A computer may have more than one IP address &#x2013; the wireless interface and Ethernet port will have different addresses, for example &#x2013; and, by default, the <o>jit.net.recv</o> object will listen to all of them for a connection request from a <o>jit.net.send</o> suitor.  It is possible to specify which interface to use by sending the object an <m>ip</m> message with the specific IP address of the desired interface as an argument.</p>

<p>If we open the sending and receiving patches on the same computer, the objects may immediately form a connection.  The <o>toggle</o> boxes connected to the <o>route</o> objects in each patch will be checked if this is the case.  By default the <o>jit.net.send</o> object's <m>ip</m> attribute is set to <m>127.0.0.1</m>, which is the <i>local loopback</i> address.  The <o>jit.net.recv</o> object is listening to all addresses by default, including the local loopback, so a connection should be made if the two patches are on the same computer.  If you're running the patches on different computers, you can use the <link name="mxj" type="refpage">mxj net.local</link> object to figure out what IP address the <o>jit.net.send</o> object should be trying to connect with.  Note that one can specify an IP address with the <m>ip</m> attribute, or alternatively one can specify a <i>hostname</i> with the <m>host</m> attribute and <o>jit.net.send</o> will attempt to use the Domain Name Service (DNS) to resolve the hostname to an IP address.</p>

<illustration><img src="images/jitterchapter52b.png"/>A patch to receive and display Jitter matrices sent over a computer network.</illustration>
<div>
<techdetail>If you can't get your objects to connect to one another, and you're sure you've got the IP address and port correct, make sure a software firewall on any computer isn't preventing communication on the <m>port</m> we've selected (<m>7474</m> in these tutorial patches).  On a computer running Windows XP, the controls for the software firewall can be found in the <i>Control Panel:Windows Firewall:Advanced</i> tab, where double clicking any of the network connections will bring up an <i>Advanced Settings</i> window that allows you to define new "services".  On an OS X machine the controls for the software firewall can be found in <i>System Preferences:Security &amp; privacy</i>, where you can set Max.app as a trusted application.  If you are connecting two computers together through one or more routers, it's possible that they have filters in place that are rejecting the packets.</techdetail>
</div>
<bullet>Turn on the <o>toggle</o> box connected to the <o>qmetro</o> in the <i>NetworkSend</i> section of the patch.</bullet><br/>

<p>In the sending patch, a <o>jit.movie</o> object is sending matrices to a <o>jit.net.send</o> object.  Note that the <o>qmetro</o> driving the movie playback triggers the movie frame and then sends the <o>jit.net.send</o> object a <m>getlatency</m> message.  This causes a one-way latency estimate to be output from the dump outlet.  We can use this estimate of the transmission latency to synchronize events on the two computers, for instance by delaying the processing or display of a matrix on a local computer by the amount of the estimate, at which point the receiving computer should be ready to process the matrix it has received.</p>

<div>
<techdetail>Latency is influenced by two factors: the direct transmission time between the two computers, and the amount of data being sent.  The more data that is sent, the longer the time it takes to move all the data across the network.  Obviously the speed of the computer's network interface and all routing hardware is key in this respect &#x2013; the transmission can only be as fast as the weakest link in the chain.  One technique we might consider using to reduce latency is to transmit matrices using either of the <i>grgb</i> or <i>uyvy</i> compressed color spaces that are discussed in <link type="tutorial" module="jit" name="jitterchapter49">Tutorial 49</link>.   </techdetail>
</div>
<p>In addition to sending matrices, any normal Max message input into the right inlet of <o>jit.net.send</o> will come out the middle outlet of the connected <o>jit.net.recv</o> object.  The input order of messages and matrices will be preserved in the receiving patch.  In this example, the <o>patcher</o> named <m>dim changes</m> contains a slower <o>qmetro</o> that changes the dimensionality of the movie every half second.  These new dimensions are input into the right inlet of the <o>jit.net.send</o> object.  When the two integers come out the second outlet of the <o>jit.net.recv</o> object they are used as the arguments of the <m>size</m> message to resize the <o>jit.pwindow</o> object.  </p>

<p>The matrices in this tutorial patch are of type <m>char</m>, but <o>jit.net.send</o> and <o>jit.net.recv</o> can handle matrices of any type.  For an example that uses <m>float32</m> matrices, please refer to <openpatchlink>audio-over-network</openpatchlink> in the <i>jit-examples/audio</i> folder.</p>


<h2>Summary</h2>
<p>Using the <o>jit.net.send</o> and <o>jit.net.recv</o> objects, you can send uncompressed Jitter matrices to another computer running Max.  You specify a <m>port</m> for the two machines to communicate on, as well as an <m>ip</m> address for the sending machine to use when connecting to the receiving machine.</p>

	<seealsolist>
		<seealso name="jit.net.recv">Receive matrices from ajit.net.send object via TCP/IP</seealso>
		<seealso name="jit.net.send">Send matrices to ajit.net.recv object via TCP/IP</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
	</seealsolist>
	</chapter>
