{"id":74,"date":"2006-04-22T10:46:22","date_gmt":"2006-04-22T15:46:22","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/code\/processing\/74"},"modified":"2008-01-21T21:33:11","modified_gmt":"2008-01-22T02:33:11","slug":"test-server-in-processing","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/74\/","title":{"rendered":"Test Server in Processing"},"content":{"rendered":"<p>This is a test server. I use it any time I have a client that needs to connect to a web server or chat server, and I want to see what the client is sending, byte for byte.  You can modify it to send and receive whatever you need.<\/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>\/* \n server_test\n \n Creates a server that listens for clients and prints what they say. \n It also sends the last client anything that's  typed on the keyboard.\n\n  created 22 April 2006\n  by Tom Igoe\n *\/\n \n \n\/\/ include the net library:\nimport processing.net.*;\n\nint port = 8080;        \/\/ the port the server listens on\nServer myServer;        \/\/ the server object\nClient  thisClient;     \/\/ incoming client object \n\nvoid setup()\n{\n  myServer = new Server(this, port); \/\/ Start the server\n}\n\nvoid draw()\n{\n  \/\/ get the next client that sends a message:\n  Client speakingClient = myServer.available();\n\n  \/\/ if the message is not null, display what it sent:\n  if (speakingClient !=null) {\n    int whatClientSaid = speakingClient.read();\n    \/\/ print who sent the message, and what they sent:\n    println(speakingClient.ip() + \"\\t\" + whatClientSaid);\n  } \n}\n\n\/\/ ServerEvent message is generated when a new client \n\/\/ connects to the server.\nvoid serverEvent(Server myServer, Client someClient) {\n  println(\"We have a new client: \" + someClient.ip());\n  thisClient = someClient;\n}\n\nvoid keyReleased() {\n  \/\/ only send if there's a client to send to:\n  if (thisClient != null) {\n    \/\/ if return is pressed, send newline and carriage feed\"\n    if (key == '\\n') {\n      thisClient.write(\"\\r\\n\");\n    } \n    \/\/ send any other key as is:\n    else {\n      thisClient.write(key);\n    }\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I use it any time I have a client that needs to connect to a web server or chat server, and I want to see what the client is sending, byte for byte.  You can modify it to send and receive whatever you need.\/* server_test Language: Processing Creates a server that listens for clients and prints what they say.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/74","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=74"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}