URL学习笔记

不多说,先上代码,代码的注释写的已经挺详细的了

 //URL:统一资源定位符,一个URL的对象,对应着互联网上的一个资源。
//我们可以通过URL的对象调用其相应的方法,将此资源读取(即所谓的“下载”)
public class TestURL {
//看URL下有哪些常用的方法
@Test
public void Testurl() throws MalformedURLException{
//1.创建一个URL的对象
//参数部分可以传入一个网址的绝对路径,这里我传人的是我写的一篇博客的地址
URL url = new URL("http://www.cnblogs.com/gode/p/5812013.html"); //获取该URL的协议名
System.out.println(url.getProtocol());//http
//获取该URL的主机名
System.out.println(url.getHost());//www.cnblogs.com
//获取该URL的端口号
System.out.println(url.getPort());//-1
//获取该URL的文件路径
System.out.println(url.getPath());// /gode/p/5812013.html
//获取该URL的文件名
System.out.println(url.getFile());// /gode/p/5812013.html
//获取该URL在文件中的相对位置
System.out.println(url.getRef());//null
//获取该URL的查询名
System.out.println(url.getQuery());//null
}

首先先了解了URL的一些常用的方法,现在我尝试利用网址读入内容到控制台打印输出

很好奇会打印出什么东西呢

     //如何将服务端的资源读取进来
@Test
public void Testurl1() throws Exception{
//1.创建一个URL的对象
//参数部分可以传入一个网址的绝对路径,这里我传人的是我写的一篇博客的地址
URL url = new URL("http://www.cnblogs.com/gode/p/5812013.html"); //2.URL读入的方法是openStream
InputStream is = url.openStream(); //3.打印到控制台上试试
byte[] b = new byte[1024];
int length;
while((length = is.read(b)) != -1){
String str = new String(b,0,length);
System.out.print(str);//这里输出的是页面布局的HTML代码,对应的汉字还有些是乱码
}
}

下面就是打印出来的东西

 <!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<title>浣跨敤UDP杩涜鏁版嵁鍙戦?佺殑瀹炰緥涓? - Hello.World! - 鍗氬鍥?</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=Ugwf7ssw_T-Y4bsdVDPHPhPR9NEESRNHOF-E8IRqEJk1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/CodingLife/bundle-CodingLife.css?v=dE43LeVMdRWr6dflRs5MpsAmgg8oyLrU-UvBlmYDLvg1"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/gode/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/gode/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/gode/wlwmanifest.xml"/>
<script src="//common.cnblogs.com/script/jquery.js" type="text/javascript"></script>
<script type="text/javascript">var currentBlogApp = 'gode', cb_enable_mathjax=false;var isLogined=false;</script>
<script src="/bundles/blog-common.js?v=Q7JqGMCRFJwGRojUTJLkQ6nIu4kUBVwm5mErn4um_MM1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a> <!--done-->
<div id="home">
<div id="header">
<div id="blogTitle">
<a id="lnkBlogLogo" href="http://www.cnblogs.com/gode/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="杩斿洖涓婚〉" /></a> <!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/gode/">Hello.World!</a></h1>
<h2></h2> </div><!--end: blogTitle 鍗氬鐨勬爣棰樺拰鍓爣棰? -->
<div id="navigator"> <ul id="navList">
<li><a id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">鍗氬鍥?</a></li>
<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/gode/">棣栭〉</a></li>
<li><a id="MyLinks1_NewPostLink" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">鏂伴殢绗?</a></li>
<li><a id="MyLinks1_ContactLink" class="menu" rel="nofollow" href="http://msg.cnblogs.com/send/Hello.World%21">鑱旂郴</a></li>
<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/gode/rss">璁㈤槄</a>
<!--<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/gode/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="璁㈤槄" /></a>--></li>
<li><a id="MyLinks1_Admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">绠$悊</a></li>
</ul>
<div class="blogStats"> <span id="stats_post_count">闅忕瑪 - 2&nbsp; </span>
<span id="stats_article_count">鏂囩珷 - 0&nbsp; </span>
<span id="stats-comment_count">璇勮 - 0</span> </div><!--end: blogStats -->
</div><!--end: navigator 鍗氬瀵艰埅鏍? -->
</div><!--end: header 澶撮儴 --> <div id="main">
<div id="mainContent">
<div class="forFlow"> <div id="post_detail">
<!--done-->
<div id="topics">
<div class = "post">
<h1 class = "postTitle">
<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/gode/p/5812013.html">浣跨敤UDP杩涜鏁版嵁鍙戦?佺殑瀹炰緥涓?</a>
</h1>
<div class="clear"></div>
<div class="postBody">
<div id="cnblogs_post_body"><p>棣栧厛濡傛灉TCP瀛﹁繃浠ュ悗锛屽啀鐪婾DP杩涜鏁版嵁浼犺緭涔熸槸澶у悓灏忓紓鐨勶紝鍙槸鐢ㄥ埌鐨勭被涓嶅悓</p>
<p>UDP杩涜浼犺緭闇?瑕丏ataSocket鍜孌atapacket绫伙紝Datapacket鍙暟鎹姤锛屾瘡涓?涓暟鎹姤涓嶈兘澶т簬64k锛岄兘璁板綍鐫?鏁版嵁淇℃伅锛屽彂閫佺鐨処P銆佺鍙e彿锛?</p>
<p>浠ュ強瑕佸彂閫佸埌鐨勬帴鏀剁鐨処P銆佺鍙e彿銆?</p>
<p>UDP杩涜浼犺緭鏄皢鏁版嵁鍏ㄩ儴鎵撳寘锛岀劧鍚庝竴璧峰彂杩囧幓锛屽鏄撻?犳垚鏁版嵁涓㈠け锛屼絾閫熷害杈冨揩锛屾瘮濡傝涓嬭浇涓?涓數褰憋紝缂哄け鍑犱睛骞舵病鏈変粈涔堝奖鍝嶏紝浣嗛渶瑕佸揩閫熶笅杞斤紝杩欎釜鏃跺?欏彲浠ラ?夋嫨UDP杩涜浼犺緭锛屾墍璋撳悇鏈夋墍闀匡紝鍏蜂綋閫夋嫨閭g鏂瑰紡杩樻槸瑕佺湅鍏蜂綋闇?瑕併??</p>
<p>鎺ヤ笅鏉ラ檮鍔犱竴涓猆DP杩涜鏂囧瓧浼犺緭锛岀劧鍚庢帴鏀剁鎵撳嵃鍒版帶鍒跺彴涓?</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">package</span><span style="color: #000000;"> com.UDP.java;
</span><span style="color: #008080;"> 2</span>
<span style="color: #008080;"> 3</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.IOException;
</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.DatagramPacket;
</span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.DatagramSocket;
</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.InetAddress;
</span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.junit.Test;
</span><span style="color: #008080;"> 8</span>
<span style="color: #008080;"> 9</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> TestUDP {
</span><span style="color: #008080;">10</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 鍙戦?佺</span>
<span style="color: #008080;">11</span> <span style="color: #000000;"> @Test
</span><span style="color: #008080;">12</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> send() {
</span><span style="color: #008080;">13</span> DatagramSocket ds = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #008080;">15</span> ds = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DatagramSocket();
</span><span style="color: #008080;">16</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 鍒涘缓涓?涓暟鎹姤锛屾瘡涓?涓暟鎹姤涓嶈兘澶т簬64k锛岄兘璁板綍鐫?鏁版嵁淇℃伅锛屽彂閫佺鐨処P銆佺鍙e彿锛屼互鍙婅鍙戦?佸埌
</span><span style="color: #008080;">17</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 鐨勬帴鏀剁鐨処P銆佺鍙e彿銆?</span>
<span style="color: #008080;">18</span> <span style="color: #0000ff;">byte</span>[] b = "鎴戞槸鍙戦?佺"<span style="color: #000000;">.getBytes();
</span><span style="color: #008080;">19</span> DatagramPacket pack = <span style="color: #0000ff;">new</span> DatagramPacket(b, 0<span style="color: #000000;">, b.length,
</span><span style="color: #008080;">20</span> InetAddress.getByName("192.168.1.101"), 9090<span style="color: #000000;">);
</span><span style="color: #008080;">21</span> <span style="color: #000000;"> ds.send(pack);
</span><span style="color: #008080;">22</span> } <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
</span><span style="color: #008080;">23</span> <span style="color: #000000;"> e.printStackTrace();
</span><span style="color: #008080;">24</span> } <span style="color: #0000ff;">finally</span><span style="color: #000000;"> {
</span><span style="color: #008080;">25</span> <span style="color: #0000ff;">if</span> (ds != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #008080;">26</span> <span style="color: #000000;"> ds.close();
</span><span style="color: #008080;">27</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">28</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">29</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">30</span>
<span style="color: #008080;">31</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 鎺ユ敹绔?</span>
<span style="color: #008080;">32</span> <span style="color: #000000;"> @Test
</span><span style="color: #008080;">33</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> receive() {
</span><span style="color: #008080;">34</span> DatagramSocket ds = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">35</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #008080;">36</span> ds = <span style="color: #0000ff;">new</span> DatagramSocket(9090<span style="color: #000000;">);
</span><span style="color: #008080;">37</span> <span style="color: #0000ff;">byte</span>[] b = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1024<span style="color: #000000;">];
</span><span style="color: #008080;">38</span> DatagramPacket pack = <span style="color: #0000ff;">new</span> DatagramPacket(b, 0<span style="color: #000000;">, b.length);
</span><span style="color: #008080;">39</span> <span style="color: #000000;"> ds.receive(pack);
</span><span style="color: #008080;">40</span> String str = <span style="color: #0000ff;">new</span> String(pack.getData(), 0<span style="color: #000000;">, pack.getLength());
</span><span style="color: #008080;">41</span> <span style="color: #000000;"> System.out.println(str);
</span><span style="color: #008080;">42</span> }<span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
</span><span style="color: #008080;">43</span> <span style="color: #000000;"> e.printStackTrace();
</span><span style="color: #008080;">44</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">45</span> <span style="color: #0000ff;">finally</span><span style="color: #000000;">{
</span><span style="color: #008080;">46</span> <span style="color: #0000ff;">if</span>(ds != <span style="color: #0000ff;">null</span><span style="color: #000000;">){
</span><span style="color: #008080;">47</span> <span style="color: #000000;"> ds.close();
</span><span style="color: #008080;">48</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">49</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">50</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">51</span> }</pre>
</div>
<p>&nbsp;</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div> </div>
<div class = "postDesc">posted @ <span id="post-date">2016-08-26 23:29</span> <a href='http://www.cnblogs.com/gode/'>Hello.World!</a> 闃呰(<span id="post_view_count">...</span>) 璇勮(<span id="post_comment_count">...</span>) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=5812013" rel="nofollow">缂栬緫</a> <a href="#" onclick="AddToWz(5812013);return false;">鏀惰棌</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=301422,cb_entryId=5812013,cb_blogApp=currentBlogApp,cb_blogUserGuid='a076c060-2898-e511-9fc1-ac853d9f53cc',cb_entryCreatedDate='2016/8/26 23:29:00';loadViewCount(cb_entryId);</script> </div><!--end: topics 鏂囩珷銆佽瘎璁哄鍣?-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id='comment_form' class='commentform'>
<a name='commentform'></a>
<div id='divCommentShow'></div>
<div id='comment_nav'><span id='span_refresh_tips'></span><a href='javascript:void(0);' onclick='return RefreshCommentList();' id='lnk_RefreshComments' runat='server' clientidmode='Static'>鍒锋柊璇勮</a><a href='#' onclick='return RefreshPage();'>鍒锋柊椤甸潰</a><a href='#top'>杩斿洖椤堕儴</a></div>
<div id='comment_form_container'></div>
<div class='ad_text_commentbox' id='ad_text_under_commentbox'></div>
<div id='ad_t2'></div>
<div id='opt_under_post'></div>
<div id='ad_c1' class='c_ad_block'></div>
<div id='under_post_news'></div>
<div id='ad_c2' class='c_ad_block'></div>
<div id='under_post_kb'></div>
<div id='HistoryToday' class='c_ad_block'></div>
<script type='text/javascript'>
$(function () {
setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
deliverAdT2();
deliverAdC1();
deliverAdC2();
loadNewsAndKb();
loadBlogSignature();
LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
loadOptUnderPost();
GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
});
</script>
</div> </div><!--end: forFlow -->
</div><!--end: mainContent 涓讳綋鍐呭瀹瑰櫒--> <div id="sideBar">
<div id="sideBarMain"> <!--done-->
<div class="newsItem">
<h3 class="catListTitle">鍏憡</h3>
<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div> <div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script> <div id="leftcontentcontainer">
<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
</div> </div><!--end: sideBarMain -->
</div><!--end: sideBar 渚ц竟鏍忓鍣? -->
<div class="clear"></div>
</div><!--end: main -->
<div class="clear"></div>
<div id="footer"> <!--done-->
Copyright &copy;2016 Hello.World!
</div><!--end: footer -->
</div><!--end: home 鑷畾涔夌殑鏈?澶у鍣? -->
</body>
</html>

并不是想象中的会将我写的博客打印出来,而是打印出了页面的HTML布局代码

而教程中,使用Tomcat服务器,URL地址指定的是一个txt文档,启动Tomcat,可以将该txt文档中的内容打印出来

分析:这个URL所指的地址被封装了,不能直接找到服务器的文档(ps:如果能的话那岂不是乱套了,服务器里存储的东西想看就看)

如果此文件非文本文件呢?

我要将此文件写入本地,使用url.openConnection()方法,返回一个URLConnection的对象

具体代码如下

 //如果既有数据的读入,又有数据的输出,就要考虑使用URLConnection
URLConnection urlConnection = url.openConnection();
InputStream is1 = urlConnection.getInputStream();
//之前打印已经发现是HTML类型的文件了,所以在这里定义成一个.html类型的文件,看看效果如何
FileOutputStream fos = new FileOutputStream(new File("aaa.html"));
byte[] b1 = new byte[1024];
int length1;
while((length1 = is1.read(b1)) != -1){
fos.write(b1, 0, length1);
}
fos.close();
is1.close();

激动人心的时刻到了,打印出来的HTML文件真的是HTML文件,具体内容如下(没有乱码了)

 <!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<title>使用UDP进行数据发送的实例一 - Hello.World! - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=Ugwf7ssw_T-Y4bsdVDPHPhPR9NEESRNHOF-E8IRqEJk1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/CodingLife/bundle-CodingLife.css?v=dE43LeVMdRWr6dflRs5MpsAmgg8oyLrU-UvBlmYDLvg1"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/gode/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/gode/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/gode/wlwmanifest.xml"/>
<script src="//common.cnblogs.com/script/jquery.js" type="text/javascript"></script>
<script type="text/javascript">var currentBlogApp = 'gode', cb_enable_mathjax=false;var isLogined=false;</script>
<script src="/bundles/blog-common.js?v=Q7JqGMCRFJwGRojUTJLkQ6nIu4kUBVwm5mErn4um_MM1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a> <!--done-->
<div id="home">
<div id="header">
<div id="blogTitle">
<a id="lnkBlogLogo" href="http://www.cnblogs.com/gode/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="返回主页" /></a> <!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/gode/">Hello.World!</a></h1>
<h2></h2> </div><!--end: blogTitle 博客的标题和副标题 -->
<div id="navigator"> <ul id="navList">
<li><a id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">博客园</a></li>
<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/gode/">首页</a></li>
<li><a id="MyLinks1_NewPostLink" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
<li><a id="MyLinks1_ContactLink" class="menu" rel="nofollow" href="http://msg.cnblogs.com/send/Hello.World%21">联系</a></li>
<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/gode/rss">订阅</a>
<!--<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/gode/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li>
<li><a id="MyLinks1_Admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">管理</a></li>
</ul>
<div class="blogStats"> <span id="stats_post_count">随笔 - 2&nbsp; </span>
<span id="stats_article_count">文章 - 0&nbsp; </span>
<span id="stats-comment_count">评论 - 0</span> </div><!--end: blogStats -->
</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 --> <div id="main">
<div id="mainContent">
<div class="forFlow"> <div id="post_detail">
<!--done-->
<div id="topics">
<div class = "post">
<h1 class = "postTitle">
<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/gode/p/5812013.html">使用UDP进行数据发送的实例一</a>
</h1>
<div class="clear"></div>
<div class="postBody">
<div id="cnblogs_post_body"><p>首先如果TCP学过以后,再看UDP进行数据传输也是大同小异的,只是用到的类不同</p>
<p>UDP进行传输需要DataSocket和Datapacket类,Datapacket叫数据报,每一个数据报不能大于64k,都记录着数据信息,发送端的IP、端口号,</p>
<p>以及要发送到的接收端的IP、端口号。</p>
<p>UDP进行传输是将数据全部打包,然后一起发过去,容易造成数据丢失,但速度较快,比如要下载一个电影,缺失几侦并没有什么影响,但需要快速下载,这个时候可以选择UDP进行传输,所谓各有所长,具体选择那种方式还是要看具体需要。</p>
<p>接下来附加一个UDP进行文字传输,然后接收端打印到控制台上</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">package</span><span style="color: #000000;"> com.UDP.java;
</span><span style="color: #008080;"> 2</span>
<span style="color: #008080;"> 3</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.IOException;
</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.DatagramPacket;
</span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.DatagramSocket;
</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.InetAddress;
</span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> org.junit.Test;
</span><span style="color: #008080;"> 8</span>
<span style="color: #008080;"> 9</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> TestUDP {
</span><span style="color: #008080;">10</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 发送端</span>
<span style="color: #008080;">11</span> <span style="color: #000000;"> @Test
</span><span style="color: #008080;">12</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> send() {
</span><span style="color: #008080;">13</span> DatagramSocket ds = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #008080;">15</span> ds = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DatagramSocket();
</span><span style="color: #008080;">16</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 创建一个数据报,每一个数据报不能大于64k,都记录着数据信息,发送端的IP、端口号,以及要发送到
</span><span style="color: #008080;">17</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 的接收端的IP、端口号。</span>
<span style="color: #008080;">18</span> <span style="color: #0000ff;">byte</span>[] b = "我是发送端"<span style="color: #000000;">.getBytes();
</span><span style="color: #008080;">19</span> DatagramPacket pack = <span style="color: #0000ff;">new</span> DatagramPacket(b, 0<span style="color: #000000;">, b.length,
</span><span style="color: #008080;">20</span> InetAddress.getByName("192.168.1.101"), 9090<span style="color: #000000;">);
</span><span style="color: #008080;">21</span> <span style="color: #000000;"> ds.send(pack);
</span><span style="color: #008080;">22</span> } <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
</span><span style="color: #008080;">23</span> <span style="color: #000000;"> e.printStackTrace();
</span><span style="color: #008080;">24</span> } <span style="color: #0000ff;">finally</span><span style="color: #000000;"> {
</span><span style="color: #008080;">25</span> <span style="color: #0000ff;">if</span> (ds != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #008080;">26</span> <span style="color: #000000;"> ds.close();
</span><span style="color: #008080;">27</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">28</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">29</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">30</span>
<span style="color: #008080;">31</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 接收端</span>
<span style="color: #008080;">32</span> <span style="color: #000000;"> @Test
</span><span style="color: #008080;">33</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> receive() {
</span><span style="color: #008080;">34</span> DatagramSocket ds = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">35</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #008080;">36</span> ds = <span style="color: #0000ff;">new</span> DatagramSocket(9090<span style="color: #000000;">);
</span><span style="color: #008080;">37</span> <span style="color: #0000ff;">byte</span>[] b = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1024<span style="color: #000000;">];
</span><span style="color: #008080;">38</span> DatagramPacket pack = <span style="color: #0000ff;">new</span> DatagramPacket(b, 0<span style="color: #000000;">, b.length);
</span><span style="color: #008080;">39</span> <span style="color: #000000;"> ds.receive(pack);
</span><span style="color: #008080;">40</span> String str = <span style="color: #0000ff;">new</span> String(pack.getData(), 0<span style="color: #000000;">, pack.getLength());
</span><span style="color: #008080;">41</span> <span style="color: #000000;"> System.out.println(str);
</span><span style="color: #008080;">42</span> }<span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
</span><span style="color: #008080;">43</span> <span style="color: #000000;"> e.printStackTrace();
</span><span style="color: #008080;">44</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">45</span> <span style="color: #0000ff;">finally</span><span style="color: #000000;">{
</span><span style="color: #008080;">46</span> <span style="color: #0000ff;">if</span>(ds != <span style="color: #0000ff;">null</span><span style="color: #000000;">){
</span><span style="color: #008080;">47</span> <span style="color: #000000;"> ds.close();
</span><span style="color: #008080;">48</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">49</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">50</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">51</span> }</pre>
</div>
<p>&nbsp;</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div> </div>
<div class = "postDesc">posted @ <span id="post-date">2016-08-26 23:29</span> <a href='http://www.cnblogs.com/gode/'>Hello.World!</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=5812013" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(5812013);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=301422,cb_entryId=5812013,cb_blogApp=currentBlogApp,cb_blogUserGuid='a076c060-2898-e511-9fc1-ac853d9f53cc',cb_entryCreatedDate='2016/8/26 23:29:00';loadViewCount(cb_entryId);</script> </div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id='comment_form' class='commentform'>
<a name='commentform'></a>
<div id='divCommentShow'></div>
<div id='comment_nav'><span id='span_refresh_tips'></span><a href='javascript:void(0);' onclick='return RefreshCommentList();' id='lnk_RefreshComments' runat='server' clientidmode='Static'>刷新评论</a><a href='#' onclick='return RefreshPage();'>刷新页面</a><a href='#top'>返回顶部</a></div>
<div id='comment_form_container'></div>
<div class='ad_text_commentbox' id='ad_text_under_commentbox'></div>
<div id='ad_t2'></div>
<div id='opt_under_post'></div>
<div id='ad_c1' class='c_ad_block'></div>
<div id='under_post_news'></div>
<div id='ad_c2' class='c_ad_block'></div>
<div id='under_post_kb'></div>
<div id='HistoryToday' class='c_ad_block'></div>
<script type='text/javascript'>
$(function () {
setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
deliverAdT2();
deliverAdC1();
deliverAdC2();
loadNewsAndKb();
loadBlogSignature();
LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
loadOptUnderPost();
GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
});
</script>
</div> </div><!--end: forFlow -->
</div><!--end: mainContent 主体内容容器--> <div id="sideBar">
<div id="sideBarMain"> <!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div> <div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script> <div id="leftcontentcontainer">
<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
</div> </div><!--end: sideBarMain -->
</div><!--end: sideBar 侧边栏容器 -->
<div class="clear"></div>
</div><!--end: main -->
<div class="clear"></div>
<div id="footer"> <!--done-->
Copyright &copy;2016 Hello.World!
</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->
</body>
</html>

这里处理异常的方式是直接throws出去了,不符合规范,正常应该全都try-catch-finally,原谅我懒了~~~

上一篇:Transact-SQL


下一篇:POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)