From 7e415cf074abc00c7ca363f61f004873a3b7446e Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@nps.edu> Date: Sat, 25 Aug 2018 13:41:34 -0700 Subject: [PATCH] http and multicast examples, refactored --- .../HttpWebPageSource.java | 71 ++ .../HttpWebPageSourceTerminalLog.txt | 748 ++++++++++++++++++ .../MulticastReceiver.java | 84 ++ .../MulticastSender.java | 83 ++ .../MulticastTerminalLog.txt | 63 ++ 5 files changed, 1049 insertions(+) create mode 100644 CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSource.java create mode 100644 CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSourceTerminalLog.txt create mode 100644 CourseExamples/src/UdpMulticastHttpExamples/MulticastReceiver.java create mode 100644 CourseExamples/src/UdpMulticastHttpExamples/MulticastSender.java create mode 100644 CourseExamples/src/UdpMulticastHttpExamples/MulticastTerminalLog.txt diff --git a/CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSource.java b/CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSource.java new file mode 100644 index 0000000000..82532b504a --- /dev/null +++ b/CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSource.java @@ -0,0 +1,71 @@ +package UdpMulticastHttpExamples; + +import java.io.*; +import java.net.*; + +/** + * A very simple http web-page reading example that won't work in some circumstances. + * But it will in some others. + * + * @author mcgredo + */ +public class HttpWebPageSource { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + try + { + System.out.println("HttpWebPageSource: create http connection and retrieve default page"); + System.out.println("Reference: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol"); + System.out.println("Reference: https://tools.ietf.org/html/rfc7230"); + System.out.println(); + + // We request an IP to connect to a web server running on port 80. + + Socket socket = new Socket("www.MovesInstitute.org", 80); + + // we send a command to the web server, asking for what + // we want. Note that the format for the command is very + // specific and documented. + OutputStream os = socket.getOutputStream(); + PrintStream ps = new PrintStream(os); + + String message = "GET /index.html HTTP/1.0"; + ps.print(message); + System.out.println(message); + System.out.println(); + + // Commands have to terminate with "carriage return/line feed" + // twice to end the request. Those are the special characters + // to have the control characters printed. Part of the http protocol! + ps.print("\r\n\r\n"); + ps.flush(); + + // Up until now we have been reading one line only. + // But a web server will write an unknown number of lines. + // So now we read until the transmisson ends. + + InputStream is = socket.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String line; + int count = 0; + + while((line = br.readLine()) != null) // read from BufferedReader br one line at a time + { + count++; + System.out.println(count + ": " + line); + } + System.out.println("HttpWebPageSource: response finished"); + } + catch(IOException e) + { + System.out.println("HttpWebPageSource: problem with client"); + System.out.println(e); + } + } +} diff --git a/CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSourceTerminalLog.txt b/CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSourceTerminalLog.txt new file mode 100644 index 0000000000..90f1d0c6bd --- /dev/null +++ b/CourseExamples/src/UdpMulticastHttpExamples/HttpWebPageSourceTerminalLog.txt @@ -0,0 +1,748 @@ +run: +HttpWebPageSource: create http connection and retrieve default page +Reference: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol +Reference: https://tools.ietf.org/html/rfc7230 + +GET /index.html HTTP/1.0 + +1: HTTP/1.1 200 OK +2: Date: Sat, 25 Aug 2018 19:24:34 GMT +3: Server: Apache +4: Last-Modified: Thu, 27 Jul 2017 23:49:02 GMT +5: ETag: "2fc15ee-41b4-555553588d385" +6: Accept-Ranges: bytes +7: Content-Length: 16820 +8: Connection: close +9: Content-Type: text/html; charset=UTF-8 +10: +11: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +12: <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/level_1.dwt" codeOutsideHTMLIsLocked="false" --> +13: <head> +14: <meta http-equiv="REFRESH" content="0;url=https://www.movesinstitute.org/"> +15: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +16: <!-- InstanceBeginEditable name="doctitle" --> +17: +18: +19: +20: +21: +22: +23: +24: <title>MOVES Institute at the Naval Postgraduate School</title> +25: +26: +27: +28: +29: +30: +31: +32: <!-- InstanceEndEditable --> +33: <!-- InstanceBeginEditable name="head" --> +34: +35: +36: +37: +38: +39: +40: +41: <meta name="Description" content="The Modeling, Virtual Environments and Simulation (MOVES) Institute at the Naval Postgraduate School specializes in education and research focused on national security issues. " /> +42: +43: +44: +45: +46: +47: +48: +49: <meta name="Keywords" content="MOVES, Modeling, Virtual Environments, Simulation, Naval Postgraduate School, NPS, national security, Masters degree, PhD degree, Delta3D, graduate degree, post-graduate degree, graduate school, university, modeling & simulation, modeling and simulation, America's Army, XMSF" /> +50: +51: +52: +53: +54: +55: +56: +57: <style type="text/css"> +58: +59: +60: +61: +62: +63: +64: +65: <!-- +66: +67: +68: +69: +70: +71: +72: +73: .style4 { +74: +75: +76: +77: +78: +79: +80: +81: font-size: 22pt +82: +83: +84: +85: +86: +87: +88: +89: } +90: +91: +92: +93: +94: +95: +96: +97: --> +98: +99: +100: +101: +102: +103: +104: +105: </style> +106: +107: +108: +109: +110: +111: +112: +113: <style type="text/css"> +114: +115: +116: +117: +118: +119: +120: +121: <!-- +122: +123: +124: +125: +126: +127: +128: +129: .style5 { +130: +131: +132: +133: +134: +135: +136: +137: font-size: 18pt +138: +139: +140: +141: +142: +143: +144: +145: } +146: +147: +148: +149: +150: +151: +152: +153: --> +154: +155: +156: +157: +158: +159: +160: +161: </style> +162: +163: +164: +165: +166: +167: +168: +169: <style type="text/css"> +170: +171: +172: +173: +174: +175: +176: +177: <!-- +178: +179: +180: +181: +182: +183: +184: +185: .style6 {color: #000000} +186: +187: +188: +189: +190: +191: +192: +193: --> +194: +195: +196: +197: +198: +199: +200: +201: </style> +202: +203: +204: +205: +206: +207: +208: +209: <style type="text/css"> +210: +211: +212: +213: +214: +215: +216: +217: <!-- +218: +219: +220: +221: +222: +223: +224: +225: .style7 {font-size: 18px} +226: +227: +228: +229: +230: +231: +232: +233: --> +234: +235: +236: +237: +238: +239: +240: +241: </style> +242: +243: +244: +245: +246: +247: +248: +249: <!-- InstanceEndEditable --> +250: <link href="css_html.css" rel="stylesheet" type="text/css" /> +251: <style type="text/css"> +252: <!-- +253: #logo { +254: position:absolute; +255: width:200px; +256: height:66px; +257: z-index:1; +258: left: 21px; +259: top: 16px; +260: visibility: visible; +261: } +262: body,td,th { +263: font-family: verdana, helvetica, arial, sans-serif; +264: font-size: 11pt; +265: color: #000; +266: } +267: body { +268: background-color: #FFF; +269: margin-left: 5px; +270: margin-top: 5px; +271: margin-right: 5px; +272: margin-bottom: 5px; +273: background-image: url(); +274: } +275: a { +276: font-size: 11pt; +277: color: #2E3191; +278: } +279: a:visited { +280: color: #A9C3A4; +281: } +282: #nav { +283: position:absolute; +284: width:214px; +285: height:64px; +286: z-index:1021; +287: left: 182px; +288: top: 109px; +289: visibility: visible; +290: } +291: +292: --> +293: </style> +294: <script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script> +295: <link href="SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" /> +296: <style type="text/css"> +297: <!-- +298: #apDiv1 { +299: position:absolute; +300: width:200px; +301: height:115px; +302: z-index:2; +303: } +304: #bodyCopy { +305: position:absolute; +306: width:525px; +307: height:515px; +308: z-index:100; +309: left: 342px; +310: top: 10px; +311: overflow: visible; +312: visibility: visible; +313: } +314: #slideshow_slot { +315: position:absolute; +316: width:250px; +317: height:290px; +318: z-index:50; +319: left: 900px; +320: top: 9px; +321: visibility: visible; +322: overflow: visible; +323: } +324: #alumni_slideshow { +325: position:absolute; +326: width:250px; +327: height:290px; +328: z-index:50; +329: left: 903px; +330: top: 400px; +331: visibility: visible; +332: overflow: visible; +333: } +334: #searchBox { +335: position:absolute; +336: width:200px; +337: height:115px; +338: z-index:1; +339: left: 214px; +340: top: 363px; +341: } +342: #apDiv2 { +343: position:absolute; +344: width:628px; +345: height:207px; +346: z-index:1; +347: left: 57px; +348: top: 219px; +349: } +350: --> +351: </style> +352: +353: </head> +354: +355: <body> +356: +357: <div id="logo"><a href="index.html"><img src="images/MOVES_logo.jpg" width="281" height="81" alt="MOVES logo" /></a></div> +358: <div id="nav"> +359: <ul id="MenuBar1" class="MenuBarVertical"> +360: <li><a href="index.html">Home</a> </li> +361: <li><a href="about.html" class="MenuBarItemSubmenu">About Moves</a> +362: <ul> +363: <li><a href="about.html">Overview</a></li> +364: <li><a href="about_background.html">Background</a></li> +365: <li><a href="about_visit.html">Visiting MOVES</a></li> +366: <li><a href="about_career.html">Career Opportunities</a></li> +367: <li><a href="RES.html">Research and Education Summit</a></li> +368: </ul> +369: </li> +370: <li><a class="MenuBarItemSubmenu" href="education.html">Education</a> +371: <ul> +372: <li><a href="education.html">Overview</a></li> +373: <li><a href="ed_masters.html">Masters of Science Program</a></li> +374: <li><a href="ed_phd.html">Ph.D. Programs</a></li> +375: <li><a href="certificates.html">Certificate Programs</a></li> +376: <li><a href="ed_course.html">Course Offerings</a></li> +377: <li><a href="ed_student_res.html">Student Research</a></li> +378: <li><a href="ed_student_affairs.html">Current Student Affairs</a></li> +379: <li><a href="alumni.html">Alumni Page</a></li> +380: </ul> +381: </li> +382: <li><a href="research.html" class="MenuBarItemSubmenu">Research</a> +383: <ul> +384: <li><a href="research.html">Overview & Featured Projects</a></li> +385: <li><a href="res_ARCV.html">Augmented Reality & Computer Vision</a></li> +386: <li><a href="res_HBS.html">Human Behavior Simulation</a></li> +387: <li><a href="res_HST.html">Human Systems & Training</a></li> +388: <li><a href="res_gaming.html">Computer Gaming, Visual & Web-Delivered Simulation</a></li> +389: <li><a href="res_simModel.html">Simulation Modeling for Analysis</a></li> +390: </ul> +391: </li> +392: <li><a href="faculty.html">Faculty</a></li> +393: <li><a class="MenuBarItemSubmenu" href="press.html">Press Room</a> +394: <ul> +395: <li><a href="newsfrommoves.html">News from MOVES</a></li> +396: <li><a href="press.html">MOVES in the News</a></li> +397: </ul> +398: </li> +399: <li><a href="contact.html">Contact Us</a></li> +400: </ul> +401: <p> +402: +403: +404: <form action="http://www.google.com/cse" id="cse-search-box"> +405: <div> +406: <input type="hidden" name="cx" value="005756209697011870278:o72xmhnnq04" /> +407: <input type="hidden" name="ie" value="UTF-8" /> +408: <input type="text" name="q" size="11" /> +409: <br /> +410: <input type="submit" name="sa" value="Search" /> +411: </div> +412: </form> +413: <script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&lang=en"></script> +414: +415: +416: +417: +418: </div> +419: +420: <!-- InstanceBeginEditable name="mainBody" --> +421: +422: +423: +424: +425: +426: +427: +428: <div id="bodyCopy"> +429: +430: +431: +432: +433: +434: +435: +436: <h1>Welcome to the MOVES Institute</h1> +437: +438: +439: +440: +441: +442: +443: +444: <p class="left_p"><img src="images/logo_strip.jpg" width="375" height="82" alt="partner logos" /></p> +445: +446: +447: +448: +449: +450: +451: +452: <p>Welcome to the Modeling, Virtual Environments, and Simulation (MOVES) Institute at the Naval Postgraduate School in Monterey, California. Our mission is to enhance the operational effectiveness of our joint forces and our allies by providing superior training and analysis products, education, and exemplary research in the field of modeling and simulation.</p> +453: +454: +455: +456: +457: +458: +459: +460: <blockquote> +461: +462: +463: +464: +465: +466: +467: +468: <blockquote> +469: +470: +471: +472: +473: +474: +475: +476: <blockquote> +477: +478: +479: +480: +481: +482: +483: +484: <p>Joseph Sullivan, CDR, USN<br /> +485: +486: +487: +488: +489: +490: +491: +492: Director </p> +493: +494: +495: +496: +497: +498: +499: +500: </blockquote> +501: +502: +503: +504: +505: +506: +507: +508: </blockquote> +509: +510: +511: +512: </blockquote> +513: +514: +515: +516: +517: +518: +519: +520: </div> +521: <!-- InstanceEndEditable --> +522: <div id="slideshow_slot"><h2><font size="4">News from MOVES</font></h2> +523: <p class="center"><SCRIPT TYPE="text/javascript" SRC="http://www.movesinstitute.org/NewsFromMOVES/Slideshow.js"></SCRIPT><SCRIPT TYPE="text/javascript"> +524: +525: ss = new slideshow("ss"); +526: ss.timeout = 5000; +527: +528: //********************START EDITING SLIDES HERE******************** +529: s = new slide(); +530: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/mmowgli.jpg"; +531: s.link = "http://www.movesinstitute.org/newsfrommoves.html#mmowgli"; +532: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">Don Brutzman and his team invite you to preregister for a new game based on social networking techniques to find and collectively grow breakthrough ideas to some of our most “wicked problems.<br>"; +533: ss.add_slide(s); +534: +535: s = new slide(); +536: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/visionacm.jpg"; +537: s.link = "http://www.movesinstitute.org/newsfrommoves.html#visionacm"; +538: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">Asst. Professor Mathias Kölsch and Vision-Based Hand-Gesture Research featured on the cover of ACM Online Journal<br>"; +539: ss.add_slide(s); +540: +541: s = new slide(); +542: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/ieeeaward.jpg"; +543: s.link = "http://www.movesinstitute.org/newsfrommoves.html#ieeeaward"; +544: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">Dr. Ji Hyun Yang, won the “Andrew P. Sage Best Transactions Paper Award” published in the IEEE Transactions on Systems, Man and Cybernetics.<br>"; +545: ss.add_slide(s); +546: +547: s = new slide(); +548: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/PaulRobLecture.jpg"; +549: s.link = "http://www.movesinstitute.org/newsfrommoves.html#PaulRobLecture"; +550: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">Guest Lecturers Paul Jesukiewicz (ADL) and LCDR Rob Betts visit the MOVES classroom.<br>"; +551: ss.add_slide(s); +552: +553: +554: s = new slide(); +555: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/LyonsWeaver.jpg"; +556: s.link = "http://www.movesinstitute.org/newsfrommoves.html#LyonsWeaver"; +557: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">MOVES Student LT John Weaver with WASC Accreditation Team Chair, Dr. Mary Lyons, at NPS<br>"; +558: ss.add_slide(s); +559: +560: s = new slide(); +561: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/AwardAIIDE.jpg"; +562: s.link = "http://www.movesinstitute.org/newsfrommoves.html#AwardAIIDE"; +563: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">Congratulations to Chris Darken, Daniel McCue and Michael Guerrero on their “Best Paper Award” at AIIDE - AI and Interactive Digital Entertainment Conference 2010<br>"; +564: ss.add_slide(s); +565: +566: +567: s = new slide(); +568: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/Sullys_Mcs_Slide.jpg"; +569: s.link = "http://www.movesinstitute.org/alumni.html#Sullys_Mcs"; +570: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">MOVES doctoral alumnus MAJ McClernon wins prestigious award for Best Student Paper<br>"; +571: ss.add_slide(s); +572: +573: s = new slide(); +574: s.src = "http://movesinstitute.org/NewsFromMOVES/Slides/MCMSO.jpg"; +575: s.link = "http://www.movesinstitute.org/newsfrommoves.html#MCMSO"; +576: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">U.S. Marine Corps Co-Sponsors MOVES Degree Program<br>"; +577: ss.add_slide(s); +578: +579: +580: //*********************** END EDITING SLIDES HERE ***************************************************** +581: for (var i=0; i < ss.slides.length; i++) { +582: s = ss.slides[i]; +583: s.target = ""; +584: } +585: +586: </SCRIPT> <DIV ID="news_slideshow"> <FORM ID="ss_form" NAME="ss_form" ACTION="" METHOD="GET"> <div class="artslot-img" style="padding-bottom:0px;"> <DIV ID="ss_img_div" style="position:relative;"> <A ID="ss_img_link" HREF="javascript:ss.hotlink()"><IMG ID="ss_img" NAME="ss_img" SRC="javascript:slide();" width="290" height="250" ALT="" border="0"></a><br> <style> +587: +588: .controls { +589: position:absolute; +590: top:230px; +591: left:4px; +592: padding-left:0px; +593: width:289px; +594: background-color:#333; +595: color:#fff; +596: filter:alpha(opacity=80); +597: -moz-opacity:.80; +598: opacity:.80; +599: height:19px; +600: padding:2px 0 2px 0; +601: } +602: +603: .controls a { +604: color:#fff; +605: float:right; +606: padding-right:5px; +607: } +608: +609: .caption { +610: position:absolute; +611: font-family:arial; +612: font-size:11px; +613: top:236px; +614: left:5px; +615: color:#fff; +616: } +617: +618: #ss_start { +619: display:none; +620: } +621: +622: </style> +623: <DIV ID="ss_controls" class="controls"> +624: <A ID="ss_next" HREF="javascript:ss.next()"><img src="http://www.movesinstitute.org/NewsFromMOVES/ss-next.gif" border="0"></A> +625: <A ID="ss_prev" HREF="javascript:ss.previous()"><img src="http://www.movesinstitute.org/NewsFromMOVES/ss-back.gif" border="0"></A> </DIV> <DIV ID="ss_text"> <SCRIPT type="text/javascript"> +626: +627: ss.textarea = document.ss_form.ss_textarea; +628: +629: </SCRIPT> </div> </div> </FORM> </DIV> <SCRIPT TYPE="text/javascript"> +630: +631: if (document.images) { +632: ss.image = document.images.ss_img; +633: ss.textid = "ss_text"; +634: ss.update(); +635: ss.play(); +636: } +637: </SCRIPT></div></p> +638: +639: </div> +640: +641: +642: <!-- +643: +644: <div id="alumni_slideshow"><h2><font size="4">MOVES Alumni Updates</font></h2> +645: <p class="center"><SCRIPT TYPE="text/javascript" SRC="http://www.movesinstitute.org/AlumniSlides/Slideshow.js"></SCRIPT><SCRIPT TYPE="text/javascript"> +646: +647: ss = new slideshow("ss"); +648: ss.timeout = 5000; +649: +650: //********************START EDITING SLIDES HERE******************** +651: s = new slide(); +652: s.src = "http://movesinstitute.org/AlumniSlides/Slides/Jungkunz_slide.jpg"; +653: s.link = "http://www.movesinstitute.org/newsfrommoves.html#Jungkunz"; +654: s.text = "<div class=\"caption\">Click image for the complete story</div><div class=\"artslot-caption\"><div style=\"font-size:15px;color:#000;width:300px;font-family:verdana;line-height:normal;padding-top:5px;\">U.S. Marine Corps Co-Sponsors MOVES Degree Program<br>"; +655: ss.add_slide(s); +656: +657: //*********************** END EDITING SLIDES HERE ***************************************************** +658: for (var i=0; i < ss.slides.length; i++) { +659: s = ss.slides[i]; +660: s.target = ""; +661: } +662: +663: </SCRIPT> <DIV ID="alumni_slideshow"> <FORM ID="ss_form" NAME="ss_form" ACTION="" METHOD="GET"> <div class="artslot-img" style="padding-bottom:0px;"> <DIV ID="ss_img_div" style="position:relative;"> <A ID="ss_img_link" HREF="javascript:ss.hotlink()"><IMG ID="ss_img" NAME="ss_img" SRC="javascript:slide();" width="290" height="250" ALT="" border="0"></a><br> <style> +664: +665: .controls { +666: position:absolute; +667: top:230px; +668: left:4px; +669: padding-left:0px; +670: width:289px; +671: background-color:#333; +672: color:#fff; +673: filter:alpha(opacity=80); +674: -moz-opacity:.80; +675: opacity:.80; +676: height:19px; +677: padding:2px 0 2px 0; +678: } +679: +680: .controls a { +681: color:#fff; +682: float:right; +683: padding-right:5px; +684: } +685: +686: .caption { +687: position:absolute; +688: font-family:arial; +689: font-size:11px; +690: top:236px; +691: left:5px; +692: color:#fff; +693: } +694: +695: #ss_start { +696: display:none; +697: } +698: +699: </style> +700: <DIV ID="ss_controls" class="controls"> +701: <A ID="ss_next" HREF="javascript:ss.next()"><img src="http://www.movesinstitute.org/AlumniSlides/ss-next.gif" border="0"></A> +702: <A ID="ss_prev" HREF="javascript:ss.previous()"><img src="http://www.movesinstitute.org/AlumniSlides/ss-back.gif" border="0"></A> </DIV> <DIV ID="ss_text"> <SCRIPT type="text/javascript"> +703: +704: ss.textarea = document.ss_form.ss_textarea; +705: +706: </SCRIPT> </div> </div> </FORM> </DIV> <SCRIPT TYPE="text/javascript"> +707: +708: if (document.images) { +709: ss.image = document.images.ss_img; +710: ss.textid = "ss_text"; +711: ss.update(); +712: ss.play(); +713: } +714: </SCRIPT></div></p> +715: +716: </div> +717: --> +718: +719: +720: <script type="text/javascript"> +721: <!-- +722: var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"../SpryAssets/SpryMenuBarRightHover.gif"}); +723: //--> +724: </script> +725: <script type="text/javascript"> +726: var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); +727: document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); +728: </script> +729: +730: <!--google analytics code//--> +731: +732: <!--<div id="apDiv2"><img src="../images/7words.gif" width="909" height="488" alt="7Words" /></div>--> +733: <script type="text/javascript"> +734: try { +735: var pageTracker = _gat._getTracker("UA-10865366-1"); +736: pageTracker._trackPageview(); +737: } catch(err) {}</script> +738: </body> +739: <!-- InstanceEnd --></html> +HttpWebPageSource: response finished +BUILD SUCCESSFUL (total time: 1 second) diff --git a/CourseExamples/src/UdpMulticastHttpExamples/MulticastReceiver.java b/CourseExamples/src/UdpMulticastHttpExamples/MulticastReceiver.java new file mode 100644 index 0000000000..e9587ef561 --- /dev/null +++ b/CourseExamples/src/UdpMulticastHttpExamples/MulticastReceiver.java @@ -0,0 +1,84 @@ +package UdpMulticastHttpExamples; + +import java.io.*; +import java.net.*; + +/** + * + * @author mcgredo + */ +public class MulticastReceiver { + + public static final String MULTICAST_ADDRESS = "239.1.2.15"; + public static final int DESTINATION_PORT = 1718; + /** How many routers can be crossed */ + public static final int TTL = 10; + + public static void main(String[] args) + { + try + { + System.out.println("MulticastReceiver started..."); + + // This is a java/IPv6 problem. You should also add it to the + // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true + // set in the "run" section of preferences. Also, typically + // netbeans must be restarted after these settings. + // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache + System.setProperty("java.net.preferIPv4Stack", "true"); + + MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT); + multicastSocket.setTimeToLive(TTL); + InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); + System.out.println("Multicast packets received log:"); + + int count = 0; // initialize + + while(true) + { + byte[] packetArray = new byte[1500]; // note use of typical MTU value + DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length); + + multicastSocket.receive(packet); + count++; + + ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); + DataInputStream dis = new DataInputStream(bais); + String firstCharacters = new String(); + char nextChar = ' '; + while (nextChar != ';') // read and build string until terminator ; found + { + nextChar = dis.readChar(); + firstCharacters += nextChar; + } + if (firstCharacters.equals("quit;")) + { + System.out.println("Received \"quit;\" sentinel"); + break; // exit out of reading loop + } + int index = dis.readInt(); + float firstFloat = dis.readFloat(); + float secondFloat = dis.readFloat(); + + if (count < 10) System.out.print(" "); // prettier output formatting + System.out.print(count + ". \"" + firstCharacters + "\" "); // wrap string in quote marks + System.out.println("index=" + index + ", firstFloat=" + firstFloat + " secondFloat=" + secondFloat); + } + System.out.println("MulticastReceiver loop complete."); + } + catch(IOException e) + { + System.out.println("Problem with MulticastReceiver, see exception trace:"); + System.out.println(e); + } + finally // clean up after exit condition + { + // socket/database/completion cleanup code can go here, if needed. + System.out.println("MulticastReceiver finally block, complete."); + } + } +} diff --git a/CourseExamples/src/UdpMulticastHttpExamples/MulticastSender.java b/CourseExamples/src/UdpMulticastHttpExamples/MulticastSender.java new file mode 100644 index 0000000000..79b5a6a710 --- /dev/null +++ b/CourseExamples/src/UdpMulticastHttpExamples/MulticastSender.java @@ -0,0 +1,83 @@ +package UdpMulticastHttpExamples; + +import java.io.*; +import java.net.*; + +/** + * Looks a lot like a UDP sender. + * + * @author mcgredo + */ +public class MulticastSender { + + public static final String MULTICAST_ADDRESS = "239.1.2.15"; // within reserved multicast address range + public static final int DESTINATION_PORT = 1718; + /** How many routers can be crossed */ + public static final int TTL = 10; + + @SuppressWarnings("SleepWhileInLoop") + public static void main(String[] args) + { + try + { + System.out.println("MulticastSender started..."); + // This is a java/IPv6 problem. You should also add it to the + // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true + // set in the "run" section of preferences. Also, typically + // netbeans must be restarted after these settings. + // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache + System.setProperty("java.net.preferIPv4Stack", "true"); + + // multicast group we are sending to--not a single host + MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT); + multicastSocket.setTimeToLive(TTL); // time to live reduces scope of transmission + InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + + int loopSize = 20; + for (int index = 0; index < loopSize; index++) + { + + // Put together a message with binary content. "ByteArrayOutputStream" + // is a java.io utility that lets us put together an array of binary + // data, which we put into the UDP packet. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + + // Put together an updated packet to send + if (index < loopSize - 1) + { + // Put together an updated packet to send + dos.writeChars("MulticastSender packet " + Integer.toString(index) + ";"); // string chars for readability + dos.writeInt (index); // arbitrary data, needs Java or byte-alignment to read + dos.writeFloat(17.0f); // arbitrary data, needs Java or byte-alignment to read + dos.writeFloat(23.0f); // arbitrary data, needs Java or byte-alignment to read + } + else dos.writeChars("quit;"); // note string must include ; semicolon as termination sentinel + + byte[] buffer = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); + + multicastSocket.send(packet); + System.out.println("Sent multicast packet " + index + " of " + loopSize); + + // How fast does this go? Does UDP try to slow it down, or does + // this cause network problems? (hint: yes for an unlimited send + // rate, unlike TCP). How do you know on the receiving side + // that you haven't received a duplicate UDP packet, out of + // order packet, or dropped packet? Necessary considerations. + Thread.sleep(1000); // Send 100, one per second + } + System.out.println("MulticastSender complete."); + } + catch(IOException | InterruptedException e) + { + System.out.println("Problem with MulticastSender, see exception trace:"); + System.out.println(e); + } + } + +} diff --git a/CourseExamples/src/UdpMulticastHttpExamples/MulticastTerminalLog.txt b/CourseExamples/src/UdpMulticastHttpExamples/MulticastTerminalLog.txt new file mode 100644 index 0000000000..62d5a5c369 --- /dev/null +++ b/CourseExamples/src/UdpMulticastHttpExamples/MulticastTerminalLog.txt @@ -0,0 +1,63 @@ +Invocation instructions: +1. run/debug MulticastReceiver.java +2. run/debug MulticastSender.java + +Program responses: + +=================================================== +run: +MulticastSender started... +Multicast address/port: 239.1.2.15/1718 +Sent multicast packet 0 of 20 +Sent multicast packet 1 of 20 +Sent multicast packet 2 of 20 +Sent multicast packet 3 of 20 +Sent multicast packet 4 of 20 +Sent multicast packet 5 of 20 +Sent multicast packet 6 of 20 +Sent multicast packet 7 of 20 +Sent multicast packet 8 of 20 +Sent multicast packet 9 of 20 +Sent multicast packet 10 of 20 +Sent multicast packet 11 of 20 +Sent multicast packet 12 of 20 +Sent multicast packet 13 of 20 +Sent multicast packet 14 of 20 +Sent multicast packet 15 of 20 +Sent multicast packet 16 of 20 +Sent multicast packet 17 of 20 +Sent multicast packet 18 of 20 +Sent multicast packet 19 of 20 +MulticastSender complete. +BUILD SUCCESSFUL (total time: 20 seconds) + +=================================================== +run: +MulticastReceiver started... +Multicast address/port: 239.1.2.15/1718 +Multicast packets received log: + 1. "MulticastSender packet 0;" index=0, firstFloat=17.0 secondFloat=23.0 + 2. "MulticastSender packet 1;" index=1, firstFloat=17.0 secondFloat=23.0 + 3. "MulticastSender packet 2;" index=2, firstFloat=17.0 secondFloat=23.0 + 4. "MulticastSender packet 3;" index=3, firstFloat=17.0 secondFloat=23.0 + 5. "MulticastSender packet 4;" index=4, firstFloat=17.0 secondFloat=23.0 + 6. "MulticastSender packet 5;" index=5, firstFloat=17.0 secondFloat=23.0 + 7. "MulticastSender packet 6;" index=6, firstFloat=17.0 secondFloat=23.0 + 8. "MulticastSender packet 7;" index=7, firstFloat=17.0 secondFloat=23.0 + 9. "MulticastSender packet 8;" index=8, firstFloat=17.0 secondFloat=23.0 +10. "MulticastSender packet 9;" index=9, firstFloat=17.0 secondFloat=23.0 +11. "MulticastSender packet 10;" index=10, firstFloat=17.0 secondFloat=23.0 +12. "MulticastSender packet 11;" index=11, firstFloat=17.0 secondFloat=23.0 +13. "MulticastSender packet 12;" index=12, firstFloat=17.0 secondFloat=23.0 +14. "MulticastSender packet 13;" index=13, firstFloat=17.0 secondFloat=23.0 +15. "MulticastSender packet 14;" index=14, firstFloat=17.0 secondFloat=23.0 +16. "MulticastSender packet 15;" index=15, firstFloat=17.0 secondFloat=23.0 +17. "MulticastSender packet 16;" index=16, firstFloat=17.0 secondFloat=23.0 +18. "MulticastSender packet 17;" index=17, firstFloat=17.0 secondFloat=23.0 +19. "MulticastSender packet 18;" index=18, firstFloat=17.0 secondFloat=23.0 +Received "quit;" sentinel +MulticastReceiver loop complete. +MulticastReceiver finally block, complete. +BUILD SUCCESSFUL (total time: 31 seconds) + +=================================================== -- GitLab