{"id":460,"date":"2007-12-03T00:00:00","date_gmt":"2007-12-02T16:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=460"},"modified":"2011-07-15T09:52:51","modified_gmt":"2011-07-15T01:52:51","slug":"j2me-datagram-receiver","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=460","title":{"rendered":"J2ME Datagram Receiver"},"content":{"rendered":"<p><FONT face=\"Courier New\"><FONT color=#7f0055><STRONG>import&nbsp;<\/STRONG><\/FONT><FONT color=#000000>java.io.IOException;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>javax.microedition.io.Connector;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>javax.microedition.io.Datagram;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>javax.microedition.io.DatagramConnection;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>public&nbsp;class&nbsp;<\/B><\/FONT><FONT color=#000000>DatagramReceiver&nbsp;<\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;static&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>main<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>String<\/FONT><FONT color=#000000>[]&nbsp;<\/FONT><FONT color=#000000>args<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>if&nbsp;<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>args.length&nbsp;!=&nbsp;<\/FONT><FONT color=#990000>1<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Usage:&nbsp;DatagramReceiver&nbsp;port&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.exit<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#990000>1<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>try&nbsp;<\/B><\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>DatagramConnection&nbsp;receiver&nbsp;=&nbsp;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>DatagramConnection<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>Connector.open<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;datagram:\/\/:&#8221;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;args<\/FONT><FONT color=#000000>[<\/FONT><FONT color=#990000>0<\/FONT><FONT color=#000000>])<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>byte<\/B><\/FONT><FONT color=#000000>[]&nbsp;<\/FONT><FONT color=#000000>buffer&nbsp;=&nbsp;<\/FONT><FONT color=#7f0055><B>new&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>byte<\/B><\/FONT><FONT color=#000000>[<\/FONT><FONT color=#990000>256<\/FONT><FONT color=#000000>]<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>Datagram&nbsp;dgram&nbsp;=&nbsp;receiver.newDatagram<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>buffer,&nbsp;buffer.length<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>for&nbsp;<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>$$<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>dgram.setLength<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>buffer.length<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>receiver.receive<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>dgram<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>int&nbsp;<\/B><\/FONT><FONT color=#000000>length&nbsp;=&nbsp;dgram.getLength<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Datagram&nbsp;received.&nbsp;Length&nbsp;is&nbsp;&#8221;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;length<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f7f5f>\/\/&nbsp;Show&nbsp;the&nbsp;content&nbsp;of&nbsp;the&nbsp;datagram.<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>for&nbsp;<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#7f0055><B>int&nbsp;<\/B><\/FONT><FONT color=#000000>i&nbsp;=&nbsp;<\/FONT><FONT color=#990000>0<\/FONT><FONT color=#000000>;&nbsp;i&nbsp;&lt;&nbsp;length;&nbsp;i++<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.print<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>buffer<\/FONT><FONT color=#000000>[<\/FONT><FONT color=#000000>i<\/FONT><FONT color=#000000>]&nbsp;<\/FONT><FONT color=#000000>+&nbsp;<\/FONT><FONT color=#2a00ff>&#8221;&nbsp;&#8220;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f7f5f>\/\/&nbsp;Send&nbsp;it&nbsp;back&#8230;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>receiver.send<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>dgram<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}&nbsp;<\/FONT><FONT color=#7f0055><B>catch&nbsp;<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>IOException&nbsp;ex<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;IOException:&nbsp;&#8221;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;ex<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#000000>}<\/FONT><\/FONT><BR><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import&nbsp;java.io.IOException;import&nbsp;javax.microedition.io.Connector;import&nbsp;javax.microedition.io.Datagram;import&nbsp;javax.microedition.io.DatagramConnection;public&nbsp;class&nbsp;DatagramReceiver&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(args.length&nbsp;!=&nbsp;1)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Usage:&nbsp;DatagramReceiver&nbsp;port&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit(1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DatagramConnection&nbsp;receiver&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(DatagramConnection)Connector.open(&#8220;datagram:\/\/:&#8221;&nbsp;+&nbsp;args[0]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte[]&nbsp;buffer&nbsp;=&nbsp;new&nbsp;byte[256];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Datagram&nbsp;dgram&nbsp;=&nbsp;receiver.newDatagram(buffer,&nbsp;buffer.length);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;($$)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dgram.setLength(buffer.length);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;receiver.receive(dgram);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;length&nbsp;=&nbsp;dgram.getLength();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Datagram&nbsp;received.&nbsp;Length&nbsp;is&nbsp;&#8221;&nbsp;+&nbsp;length);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Show&nbsp;the&nbsp;content&nbsp;of&nbsp;the&nbsp;datagram.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nb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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;}}<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[124],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-linux","tag-j2me"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=460"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/460\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}