<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Comm Tutorial 3: UDP Networking">

<setdocpatch name="03cUDPNetworking" patch="03cUDPNetworking.maxpat"/>
<previous name="communicationschapter02">Serial Communication</previous>
<next name="interfacechapter01">Bpatchers</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Communications" title="UDP Networking">Passing messages over a network</indexinfo>

<h1>Communications Tutorial 3: UDP Networking</h1>

<h2>Introduction</h2>

<p>This tutorial provides information on using a network for transferring Max messages.  We examine the <o>udpsend</o> and <o>udpreceive</o> objects, which provide sending and receiving of messages through the UDP networking protocol.</p>

<p>There are many reasons to implement computer networking in Max patchers.  Often, in large projects, the work needing to be done exceeds what a single machine can handle.  In those cases, it can be useful to use an existing network to harvest CPU cycles from unused machines, or to send synchronization messages between machines running subsections of the whole performance patch. In addition, network protocols can be useful for sharing information among computers that are run by multiple performers, even in separate locations.  The <o>udpsend</o> and <o>udpreceive</o> messages wrap the standard UDP networking protocol into a pair of Max objects, providing an easy way to transmit messages between two computers on a network.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>General UDP Overview</h2>

<p>The UDP protocol is a lightweight and flexible communications protocol used for Internet application-to-application exchanges.  It doesn’t require a connection to be maintained between two machines; rather, the receiving application watches a specific <i>port</i> for data packets (called <i>datagrams</i>) and treats them as incoming messages.  The transmitting program is required to specify the destination <i>address</i> and port of the receiver.</p>

<p>UDP isn’t the most robust protocol: it does not guarantee that packets will arrive in any order, nor does it provide any means of error or packet loss notification.  However, it is a high-speed protocol, often used for networked audio and video content, and happens to be easy to coerce into working with Max messages.</p>

<h2>Sending messages of all types</h2>

<p>Take a look at our tutorial.  At the left is a <o>udpsend</o> object with several different Max message types connected.  This is the “transmitting” side of the network connection: the <o>udpsend</o> object takes Max messages, turns them into UDP datagrams, and sends them to the destination specified by the machine's TCP/IP address and port (the arguments to the object). The <o>patcher</o> named “network_receiver” is, as its name suggests, the receiving end of this patch. If you open the subpatcher, you will see a <o>udpreceive</o> object watching port <m>7000</m> (you don’t have to specify a network address – the object assumes you are watching the port on “this computer”).</p>

<p>When you click or change any of the messages connected to the <o>udpsend</o> object, you will see that they appear in the subpatcher. Both simple and complex messages can be sent through the <o>udpsend</o> object; the <o>multislider</o> sends a 12-entry list (<o>prepend</o>ed with the word <m>multislider</m>, so we can <o>route</o> that at the receiving end), but even the simplest message can be packaged and sent over the network.</p>

<p>We should quickly discuss the network address used in this example.  Since we want to test this <i>locally</i> on our own machine, we need to use a network address that consistently represents the host machine.  That address is the so-called IP loopback address: <m>127.0.0.1</m>.  Whenever that network address is used, it means “route this internally, rather than through an external network”.  If you wanted to test this patch on a pair of networked machines, the <o>udpsend</o> object would have to be changed to use the TCP/IP address of the receiving machine.</p>

<h2>A basic chat program</h2>

<p>The second segment of our tutorial patch is an example of a simple chat program.  Again, there is a subpatcher that is used to show the communications in action, although you could make some simple changes to have it work between two machines – you would only need to change the network address from “localhost” (the symbolic name for <m>127.0.0.1</m>) to the appropriate addresses of the connected computers.  This part of the patch sends text (entered in a <o>textedit</o> object) on port <m>7003</m>, and receives messages on port <m>7002</m>.</p>

<p>Unlike our first example, this shows bidirectional communication in action.  When sending information between two computers, each will need a <o>udpsend</o> and <o>udpreceive</o> object, with <o>udpsend</o> pointing to the other system’s network address.  Additionally, by convention, bidirectional communications occur through two adjacent ports: <m>7002</m> and <m>7003</m>, in this case.  If you open the <m>chat_buddy</m> <o>patcher</o>, you will see that it, too, communicates using localhost, but the port order is reversed.  Text is sent on port <m>7002</m> and received on port <m>7003</m>. You can now type text into one of the chat patches (in the white <o>textedit</o> object), when you hit the return key, it will appear in the grey area of the other patch.</p>

<h2>Summary</h2>

<p>Max message handling over the network uses the <o>udpsend</o> and <o>udpreceive</o> object, taking advantage of the simple and lightweight UDP protocol to flexibly pass any message or list type. Within a single system, the localhost network address can be used, but the same mechanism will allow you to send messages across the network to any other reachable machine.</p>

<seealsolist>
<seealso name="udpsend">Send Max messages over a network using UDP</seealso>
<seealso name="udpreceive">Receive Max messages over a network using UDP</seealso>
<seealso name="textedit">User-entered text in a patcher</seealso>
</seealsolist>

</chapter>
