1.OSI分层模型和TCP/IP分层模型的对应关系
2.七层模型与协议的对应关系
网络层 ———— IP(网络之间的互联协议)
传输层 ———— TCP(传输控制协议)、UDP(用户数据报协议)
应用层 ———— Telnet(Internet远程登录服务的标准协议和主要方式)、FTP(文本传输协议)、HTTP(超文本传送协议)
3.IP地址和端口号
1)ip地址用于唯一标示网络中的一个通信实体,这个通信实体可以是一台主机,可以是一台打印机,或者是路由器的某一个端口。而在基于IP协议网络中传输的数据包,必须使用IP 地址来进行标示。ip地址就像写一封信,必须指定收件人的地址一样。每个被传输的数据包中都包括了一个源IP和目标IP。
2)ip地址唯一标示了通信实体,但是一个通信实体可以有多个通信程序同时提供网络服务。这个时候就要通过端口来区分开具体的通信程序。一个通信实体上不能有两个通信程序 使用同一个端口号。
IP地址和端口号,就像家庭住址一样,IP地址表示了具体的街道,而端口号则表示了这个人的门牌号。
4.TCP和UDP
1)TCP是一种面向连接的保证可靠传输的协议。通过TCP协议传输,得到的是一个顺序的无差错的数据流。它能够提供两台计算机之间的可靠的数据流,HTTP、FTP、Telnet等应 用都需要这种可靠的通信通道。
2)UDP是一种无连接的协议,每个数据报都是一个独立的信息,包括完整的源地址或目的地址,它在网络上以任何可能的路径传送目的地,至于能够达到目的地,达到目的地的时 间以及内容的正确性都是不能保证的。
既然有了保证可靠传输的TCP协议,为什么还要非可靠传输的UDP协议呢?原因有两个:
1)可靠的传输是要付出代价的,对数据内容的正确性的检验必然会占用计算机处理时间和网络带宽。因此TCP的传输效率不如UDP高。
2)许多应用中并不需要保证严格的传输可靠性,比如视频会议系统,并不要求视频音频数据绝对正确,只要能够连贯就可以了。所以在这些场景下,使用UDP更合适些。
5.URL访问网上资源
1)URL对象代表统一资源定位器,是指向互联网“资源”的指针。它是用协议名、主机、端口和资源组成,即满足如下格式:
protocol://host:port/resourceName
http://www.apache.org/index.html
2)通过URL对象的一些方法可以访问该URL对应的资源:
String getFile():获取该URL的资源名
String getHost():获取主机名
String getPath():获取路径部分
int getPort():获取端口号
public class Test {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://blog.csdn.net/jinjin603?a=1");
String file = url.getFile();
System.out.println(file);
String host = url.getHost();
System.out.println(host);
int port = url.getPort();
System.out.println(port);
String query = url.getQuery();
System.out.println(query);
String protocol = url.getProtocol();
System.out.println(protocol);
}
}
执行结果:
/jinjin603?a=1
blog.csdn.net
-1
a=1
http
看看URL最重要的两个方法:openConnection()、openStream()
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.baidu.com");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream("c:/data.txt");
byte[] buffer = new byte[1024];
int flag = 0;
while (-1 != (flag = is.read(buffer, 0, buffer.length))){
os.write(buffer, 0, flag);
}
os.close();
is.close();
}
获取到data.txt的内容:
<!DOCTYPE html>
<!--STATUS OK-->
<html>
<head>
<meta http-equiv=content-type content=text/html;charset=utf-8>
<meta http-equiv=X-UA-Compatible content=IE=Edge>
<meta content=always name=referrer>
<link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css>
<title>百度一下,你就知道</title>
</head>
<body link=#0000cc>
<div id=wrapper>
<div id=head>
<div class=head_wrapper>
<div class=s_form>
<div class=s_form_wrapper>
<div id=lg>
<img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129>
</div>
<form id=form name=f action=//www.baidu.com/s class=fm>
<input type=hidden name=bdorz_come value=1>
<input type=hidden name=ie value=utf-8>
<input type=hidden name=f value=8>
<input type=hidden name=rsv_bp value=1>
<input type=hidden name=rsv_idx value=1>
<input type=hidden name=tn value=baidu>
<span class="bg s_ipt_wr">
<input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus>
</span>
<span class="bg s_btn_wr">
<input type=submit id=su value=百度一下 class="bg s_btn">
</span>
</form>
</div>
</div>
<div id=u1>
<a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a>
<a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a>
<a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a>
<a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a>
<a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a>
<noscript>
<a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a>
</noscript>
<script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p>
</div>
</div>
</div>
</body>
</html>
1.代码第4行openConnection()该方法用于返回URLConnection对象,表示到URL所引用的远程连接。
2.代码第5行getInputStream方法,用于返回从此打开的连接读取的输入流。
3.后面的代码就是我们已经很熟悉的从输入流中读到数据,再通过输出流写入文件。
4.执行的结果就是我们通过浏览器访问http://www.baidu.com,百度服务器返回的内容。
5.这里是文本文件,我们将后缀修改为html,然后用浏览器访问,这样看起来更直观。
6.InetAddress
java提供了InetAddress类来代表IP地址。
public static void main(String[] args) throws IOException {
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
address = InetAddress.getByName("www.baidu.com");
System.out.println(address);
}
执行结果:
USERCHI-5FQSAAM/192.168.118.1
www.baidu.com/61.135.169.125
参考java网络编程