目录
一、OSI七层网络模型
是一个把网络通信在逻辑上的定义,也可以理解成为定义了通用的网 络通信规范。而我们的数据在网络中传输的过程,实际上就是如下图的封装和解封装的过程,发送方通过各种封装处理,把数据 转换成比特流的形式,比特流在信号传输的硬件媒介中传输,接收方再把比特流进行解封装处理。
1.物理层
规定了如何为网络通信实现最底层的物理连接,以及物理设备的机械、电气、功能和过程特性。如何使用电缆和接头的类型、用来传送信号的电压。需要注意的是,网络通信过程中所需的物理媒介(网线,线缆等),其实并不属于物理层,因为物理层实际上是一种规定,规定这些物理媒介设备在连接网络时的各种规格,参数以及工作方式。但是同时,双绞线,线缆等物理媒介又是物理层的实现。
2.数据链路层
规定了如何进行物理地址寻址、如何在物理线路上进行数据(帧frame)的可靠传递以及流量控制。数据链路层协议有SLIP协议、CSLIP协议、PPP协议等。交换机,对帧解码并根据帧中包含的信息把数据发送到正确的接收方,所以 交换机是工作在数据链路层的。
3.网络层
规定了通过哪些网络节点、什么样的网络路径来将数据(数据包)从发送方发送到接收方。在网络层中,确定了从节点A发数据到节点B的网络路径,经过哪些节点。网络层既可以建立LAN通信系统,更主要的是可以在WAN网络系统中建立通信,这是因为它有自己的路由地址结构,通过路由协议(又称可路由协议)进行网络通信的路由工作。
4.传输层
负责总体的数据传输和数据控制,提供端到端的交换数据的机制。传输层对数据(段)进行分割和重组,并且进行流量控制和根据接收方的接收数据能力确定适当的传输速率。例如以太网无法处理大于1500字节的数据包,传输层将数据分割成数据片段,并对小数据片段进行序列编号。接收方的传输层将根据序列编号对数据进行重组。传输层协议有TCP协议、UDP协议等。
5.会话层
在网络中的两个节点之间建立、维持和终止通信。
6.表示层
在应用程序和网络之间对数据进行格式化,使之能够被另一方理解。即发送方的表示层将应用程序数据的抽象语法转换成网 络适用于OSI网络传输的传送语法,接收方则相反。除此之外,表示层还可对数据进行加密与解密。
7.应用层
最顶层的OSI层,为应用程序提供网络服务。如为电子邮件、文件传输功能提供协议支持。应用层协议有HTTP协议、FTP协议,SMTP协议等。
二、网络编程概述
一、网络编程中有两个主要的问题:
1.如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
2.找到主机后如何可靠高效地进行数据传输
二、网络编程中的两个要素:
1.对应问题一:IP和端口号
2.对应问题二:提供网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
三、通信要素一:IP和端口号
- IP:唯一的标识 Internet 上的计算机(通信实体)
- 在Java中使用InetAddress类代表IP
- IP分类:IPv4 和 IPv6 ; 万维网 和 局域网
- 域名: www.baidu.com www.mi.com www.sina.com www.jd.com www.vip.com
- 本地回路地址:127.0.0.1 对应着:localhost
- 如何实例化InetAddress:两个方法:getByName(String host) 、 getLocalHost()、 两个常用方法:getHostName()/getHostAddress()
- 端口号:正在计算机上运行的进程。 要求:不同的进程有不同的端口号。 范围:被规定为一个 16 位的整数 0~65535。
1)公认端口(WellKnownPorts):从0到1023,它们紧密绑定(binding)于一些服务。通常这些端口的通讯明确表明了某种服务的协议。例如:80端口实际上总是HTTP通讯,FTP占用端口21,Telnet占用端口23。
(2)注册端口(RegisteredPorts):从1024到49151。它们松散地绑定于一些服务。也就是说有许多服务绑定于这些端口,这些端口同样用于许多其它目的。例如:许多系统处理动态端口从1024左右开始,Tomcat占用端口8080,MySQL占用端口3306,Oracle占用端口1521等。
(3)动态和/或私有端口(Dynamicand/orPrivatePorts):从49152到65535。理论上,不应为服务分配这些端口。实际上,机器通常从1024起分配动态端口。但也有例外:SUN的RPC端口从32768开始。
- 端口号与IP地址的组合得出一个网络套接字:Socket
public class InetAddressTest {
public static void main(String[] args) {
try {
//File file=new File("hello.txt")
InetAddress inet1 = InetAddress.getByName("192.168.189.132");
System.out.println(inet1);
InetAddress inet2 = InetAddress.getByName("www.CSDN.net");
System.out.println(inet2);
//获取本机的Ip地址
InetAddress inet3 = InetAddress.getByName("127.0.0.1");
System.out.println(inet3);
//获取本机的IP地址
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet4);
//getHostName()
System.out.println(inet2.getHostName());
//getHostAddress()
System.out.println(inet2.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
三、TCP网络编程
传输控制协议TCP(Transmission Control Protocol)
实现TCP的网络编程
例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
public class TCPTest1 {
//客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
//1.创建Socket对象,指明服务器端的ip和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8899);
//2.获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3.写出数据的操作
os.write("你好,我是客户端洪女士!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流资源
try {
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务端
@Test
public void server() {
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.创建服务器端的ServerSocket,指明自己的端口号
ss = new ServerSocket(8899);
//2.调用accept()表示就收来自于客户端的socket
socket = ss.accept();
//3.获取输入流
is = socket.getInputStream();
//不建议这样写,可能会出现乱码
// byte[] buffer = new byte[1024];
// int len;
// while ((len=is.read(buffer))!=-1){
// String str = new String(buffer, 0, len);
// }
//4.读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于:"+socket.getInetAddress().getHostAddress()+"的数据");
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭资源
try {
if(is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(baos!=null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(ss!=null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
例题2:客户端发送文件给服务端,服务端将文件保存在本地。
public class TCPTest2 {
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
//1.
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
//2.
os = socket.getOutputStream();
//3.
fis = new FileInputStream(new File("src\\javaSenior\\javaNetwork\\hzz.png"));
//4.
byte[] buffer = new byte[1024];
int len;
while ((len= fis.read(buffer))!=-1) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.
try {
if(fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
try {
ss = new ServerSocket(9090);
socket = ss.accept();
is = socket.getInputStream();
fos = new FileOutputStream(new File("src\\javaSenior\\javaNetwork\\hgw.png"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(ss!=null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
例题3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。 并关闭相应的连接。
public class TCPTest3 {
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
//2.
os = socket.getOutputStream();
//3.
fis = new FileInputStream(new File("src\\javaSenior\\javaNetwork\\hzz.png"));
//4.
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
//关闭数据的输出
socket.shutdownOutput();
//5.接收来来自于服务器端的数据,并显示到控制台上
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[20];
int len1;
while ((len1 = is.read(buffer1)) != -1) {
baos.write(buffer1, 0, len1);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//6.关闭流资源
try {
if(baos!=null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server() {
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os=null;
try {
//1.
ss = new ServerSocket(9090);
//2.
socket = ss.accept();
//3.
is = socket.getInputStream();
//4.
fos = new FileOutputStream(new File("src\\javaSenior\\javaNetwork\\hzw.png"));
//5.
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
System.out.println("图片传输完成!");
//6.服务器端给予客户端反馈
os = socket.getOutputStream();
os.write("你好,洪女士照片很漂亮!".getBytes());
//7.
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ss != null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、UDP网络编程
用户数据报协议UDP(User Datagram Protocol)
public class UDPTest {
//发送端
@Test
public void sender(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
String str="我是UDP方式发送的导弹";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null)
socket.close();
}
}
//接收端
@Test
public void receiver(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null)
socket.close();
}
}
}
五、URL编程
URL网络编程
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式:
http://localhost:8080/examples/beauty.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
public class URLTest1 {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/javaSenior/javaNetwork/examples/hzz.png");
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream(new File("src\\javaSenior\\javaNetwork\\rqz.png"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if(is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
if(urlConnection!=null)
urlConnection.disconnect();
}
}
}