{"id":65,"date":"2007-08-20T09:19:22","date_gmt":"2007-08-20T14:19:22","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/processing\/65"},"modified":"2008-01-21T21:30:24","modified_gmt":"2008-01-22T02:30:24","slug":"net-connection-between-processing-and-maxmsp","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/65\/","title":{"rendered":"Net connection between Processing and Max\/MSP"},"content":{"rendered":"<p>The following Max\/MSP patch uses the netsend object to send data over a TCP socket.  It sends three values.  The Processing sketch further down is a server that receives the string that the Max patch sends and uses it to move a ball on the screen.<\/p>\n<p><!-- technorati tags start --><\/p>\n<p style=\"text-align:right;font-size:10px;\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tag\/networked objects\" rel=\"tag\">networked objects<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/networks\" rel=\"tag\">networks<\/a><\/p>\n<p><!-- technorati tags end --><br \/>\n<!--more--><\/p>\n<pre>max v2;\n#N vpatcher 35 147 635 547;\n#P window setfont \"Sans Serif\" 9.;\n#P window linecount 1;\n#P newex 187 227 68 196617 prepend send;\n#P newex 187 202 52 196617 pak 0 0 0;\n#P number 281 170 35 9 0 255 3 3 0 0 0 221 221 221 222 222 222 0 0 0;\n#P number 187 170 35 9 0 255 3 3 0 0 0 221 221 221 222 222 222 0 0 0;\n#P number 233 170 35 9 0 255 3 3 0 0 0 221 221 221 222 222 222 0 0 0;\n#P message 60 169 55 196617 disconnect;\n#P message 60 144 114 196617 connect localhost 3000;\n#P number 47 294 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;\n#P newex 47 270 43 196617 netsend;\n#P comment 186 151 155 196617 three arbitrary integer values:;\n#P window linecount 4;\n#P comment 60 88 100 196617 click here to connect after server application is running:;\n#P window linecount 2;\n#P comment 60 194 75 196617 click here to disconnect;\n#P window linecount 1;\n#P comment 242 202 154 196617 packs three values into one list;\n#P comment 260 227 202 196617 adds the command \"send\" before the list;\n#P comment 86 294 258 196617 Whether netsend is connected to the server or not;\n#P window linecount 4;\n#P comment 215 55 82 196617 NetSend tester Created 29 Nov 2006 by Tom Igoe;\n#P fasten 10 0 7 0 65 189 52 189;\n#P fasten 9 0 7 0 65 163 52 163;\n#P fasten 15 0 7 0 192 258 52 258;\n#P connect 7 0 8 0;\n#P fasten 12 0 14 0 192 193 192 193;\n#P connect 14 0 15 0;\n#P fasten 11 0 14 1 238 188 213 188;\n#P fasten 13 0 14 2 286 192 234 192;\n#P pop;\n<\/pre>\n<p>Here&#8217;s the Processing server sketch:<\/p>\n<pre>\/*\n  Max Server \n \n Accepts three values from a remote client. Uses those values\n to draw and move a ball.\n \n This server expects a string containing ASCII-encoded integers\n terminated by a semicolon. This is the format that Max\/MSP's netsend\n object sends data.\n \n by Tom Igoe\n Created 18 Mar 2005\n Modified 20 Aug 2007\n *\/\n\n\/\/ import the net library:\nimport processing.net.*;\n\nint port = 3000;    \/\/ incoming TCP port\nServer myServer;    \/\/ server object\n\nvoid setup()\n{\n  size(256, 256);\n  background(0);\n  myServer = new Server(this, port); \/\/ Starts a server on port 3000\n  smooth();\n}\n\n\nvoid draw()\n\n{\n  \/\/ see if there's a client that's connected and sent data:\n  Client thisClient = myServer.available();\n  if (thisClient != null) {\n    \/\/ if there are bytes in the client's incoming data buffer:\n    if (thisClient.available() &gt; 0) {\n      \/\/ read the incoming string:\n      String thisString = thisClient.readString();\n      \/\/ split it at the semicolon to strip off the semicolon:\n      String[] pieces = split(thisString, \";\");\n      \/\/ save only what's before the semi:\n      thisString = pieces[0];\n      \/\/ split the resulting string on the space characters:\n      int messages[] = int(split(thisString, \" \"));\n      \/\/ if you've got three pieces, use them to draw the ball:\n      if (messages.length &gt; 2) {\n        drawBall(messages[0], messages[1], messages[2]);      \n      }  \n    }\n  }\n}\n\n\/\/ draw the ball:\nvoid drawBall(int x, int y, int greyscale) {\n  background(255 - ); \n  fill(greyscale);\n  ellipse(x, y, 20, 20);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following Max\/MSP patch uses the netsend object to send data over a TCP socket.  It sends three values.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,7],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-MaxMSP","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}