JavaSE.11.网络编程

该博客围绕JavaSE网络编程展开,介绍了IP和端口号、网络通信协议等基础知识,详细讲解了URL网络编程。重点阐述了TCP和UDP编程,给出多个TCP编程示例,如客户端与服务器信息、文件传输,还介绍了UDP编程示例,并总结了TCP和UDP的区别。

JavaSE.11.网络编程
1.网络编程概述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vl68wiLG-1605171954077)(/image-20201109104023366.png)]

1.IP和端口号
在这里插入图片描述

在这里插入图片描述

2.网络通信协议
在这里插入图片描述
在这里插入图片描述

3.TCP
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

4.UDP
在这里插入图片描述

5.URL
在这里插入图片描述

2.URL
URL网络编程
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式:
http://localhost:8080/examples/beauty.jpg?username = Tom
协议 主机名 端口号 资源地址 参数列表
1
2
3
4
5
public class URLTest {
public static void main(String[] args) {
InputStream is = null;
FileOutputStream out = null;
HttpURLConnection hurl = null;
try {
URL url1 = new URL(“http://localhost:8080/examples/beauty.jpg?username = Tom”);

		//public String getProtocal()  获取协议名
		System.out.println(url1.getProtocol());//https
		//public String getHost()	获取主机名
		System.out.println(url1.getHost());//localhost
		//public String getPort()	获取端口号
		System.out.println(url1.getPort());//8080
		//public String getPath()	获取文件路径
		System.out.println(url1.getPath());///examples/beauty.jpg
		//public String getFile()	获取文件名
		System.out.println(url1.getFile());///examples/beauty.jpg?username = Tom
		//public String getQuery()	获取查询名
		System.out.println(url1.getQuery());//username = Tom
		
		hurl =  (HttpURLConnection)url1.openConnection();
		hurl.connect();
		is = hurl.getInputStream();
		out = new FileOutputStream(new File("fuzhi.jpg"));
		
		byte[] buffer = new byte[1024];
		int len;
		while((len = is.read(buffer)) != -1) {
			out.write(buffer,0,len);
		}
		
		System.out.println("下载完成!");
	} catch (MalformedURLException e) {
		e.printStackTrace();
	}catch (IOException e) {
		e.printStackTrace();
	}finally {
		try {
			if(is != null) {
				is.close();
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			if(out != null) {
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(hurl != null) {
			hurl.disconnect();
		}
		
	}
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
3.IP和端口号
通信要素一:IP和端口号
①.IP:唯一的标识 Internet上的计算机
②.在Java中使用InetAdress类代表IP
③.IP的分类:IPv4和IPv6;万维网和局域网
④域名: www.baidu.com
⑤.本地回路地址:127.0.0.1 对应:localhost
⑥如何实例化InetAddress:两个方法:getByName(String host),getLocalHost()
两个常用方法:getHostName()/getHostAddress()
⑦端口号:正在计算上运行的进程
要求:不同的进程有不同的端口号
范围:被规定为一个16位的整数 0~65535
⑧端口号与IP地址的组合得出一个网络套接字:Socket
1
2
3
4
5
6
7
8
9
10
11
12
public class InetAddressTest {
public static void main(String[] args) {
try {
InetAddress inet1 = InetAddress.getByName(“192.168.0.1”);
System.out.println(inet1);
InetAddress inet2 = InetAddress.getByName(“www.baidu.com”);
System.out.println(inet2);
InetAddress inet3 = InetAddress.getByName(“localhost”);
System.out.println(inet3);
//获取本地ip
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet4);
System.out.println(inet2.getHostName());//getHostName()
System.out.println(inet2.getHostAddress());//getHostAddress()
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4.TCP
/*

  • 实现TCP的网络编程

  • 例子1:客户端发送信息给服务器端,服务器端显示在控制台
    */
    public class TCPTest {
    //客户端
    @Test
    public void test1() {
    Socket socket = null;
    OutputStream os = null;
    try {
    InetAddress inet = InetAddress.getByName(“127.0.0.1”);
    socket = new Socket(inet,16404);
    os = socket.getOutputStream();
    os.write(“你好我是你的客户端”.getBytes());
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally {
    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 test2() {
    ServerSocket serverSocket = null;
    InputStream in= null;
    Socket socket = null;
    try {
    //1.创建服务器端的ServerSocket,指明自己的端口号
    serverSocket = new ServerSocket(16404);
    System.out.println(“服务器已经启动”);
    //2.调用accept()接收来自客户端连接的Socket
    socket = serverSocket.accept();
    System.out.println(“已经有客户连接”);
    //3.获取一个输入流
    in = socket.getInputStream();
    //这样接收有可能会乱码
    // byte[] buffer = new byte[20];
    // int len;
    // while((len = in.read(buffer)) != -1) {
    // String str = new String(buffer,0,len);
    // System.out.println(str);
    // }
    //4.读取输入流的一个数据
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[5];
    int len;
    while((len = in.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 {
    try {
    if(serverSocket != null) {
    serverSocket.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    if(in != null) {
    in.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    if(socket != null) {
    socket.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    /*

  • 例题2:客户端发送文件给服务端,服务端将文件保存在本地
    */
    public class TCPTest2 {
    @Test
    public void client() {
    Socket socket = null;
    OutputStream os = null;
    FileInputStream fis = null;
    try {
    socket = new Socket(InetAddress.getByName(“127.0.0.1”),16404);
    os = socket.getOutputStream();
    fis = new FileInputStream(new File(“人物.jpg”));
    byte[] buffer = new byte[1024];
    int len;
    while((len = fis.read(buffer)) != -1) {
    os.write(buffer,0,len);
    }
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    @Test
    public void server() {
    ServerSocket serverSocket = null;
    InputStream in= null;
    Socket socket = null;
    FileOutputStream out = null;
    try {
    //1.创建服务器端的ServerSocket,指明自己的端口号
    serverSocket = new ServerSocket(16404);
    System.out.println(“服务器已经启动”);
    //2.调用accept()接收来自客户端连接的Socket
    socket = serverSocket.accept();
    System.out.println(“已经有客户连接”);
    //3.获取一个输入流
    in = socket.getInputStream();
    //这样接收有可能会乱码
    out = new FileOutputStream(new File(“人物复制.jpg”));

     	byte[] buffer = new byte[1024];
     	int len;
     	while((len = in.read(buffer)) != -1) {//java.net.SocketException: Connection reset
     		out.write(buffer,0,len);
     	}
     } catch (IOException e) {
     	e.printStackTrace();
     }finally {
     	try {
     		if(serverSocket != null) {
     			serverSocket.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     	try {
     		if(in != null) {
     			in.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     	try {
     		if(socket != null) {
     			socket.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     	try {
     		if(out != null) {
     			out.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     }
    

    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*

  • 例题3:
    */
    public class TCPTest3 {
    @Test
    public void client() {
    Socket socket = null;
    OutputStream os = null;
    FileInputStream fis = null;
    try {
    socket = new Socket(InetAddress.getByName(“127.0.0.1”),16404);
    os = socket.getOutputStream();
    fis = new FileInputStream(new File(“人物.jpg”));
    byte[] buffer = new byte[1024];
    int len;
    while((len = fis.read(buffer)) != -1) {
    os.write(buffer,0,len);
    }

     	socket.shutdownOutput();
     	
     	InputStream in = socket.getInputStream();
     	ByteArrayOutputStream baos = new ByteArrayOutputStream();
     	byte[] buffer1 = new byte[20];
     	int len1;
     	while((len1 = in.read(buffer1)) != -1) {//阻塞方法
     		baos.write(buffer1,0,len1);
     	}
     	System.out.println(baos.toString());
     	
     	baos.close();
     	in.close();
     	socket.close();
     	os.close();
     	fis.close();
     } catch (UnknownHostException e) {
     	e.printStackTrace();
     } catch (IOException e) {
     	e.printStackTrace();
     }
    

    }

    @Test
    public void server() {
    ServerSocket serverSocket = null;
    InputStream in= null;
    Socket socket = null;
    FileOutputStream out = null;
    try {
    //1.创建服务器端的ServerSocket,指明自己的端口号
    serverSocket = new ServerSocket(16404);
    System.out.println(“服务器已经启动”);
    //2.调用accept()接收来自客户端连接的Socket
    socket = serverSocket.accept();
    System.out.println(“已经有客户连接”);
    //3.获取一个输入流
    in = socket.getInputStream();
    //这样接收有可能会乱码
    out = new FileOutputStream(new File(“人物复制1.jpg”));

     	byte[] buffer = new byte[1024];
     	int len;
     	while((len = in.read(buffer)) != -1) {//java.net.SocketException: Connection reset
     		out.write(buffer,0,len);
     	}
     	
     	System.out.println("图片已经收到");
     	
     	OutputStream os = socket.getOutputStream();
     	os.write("你的图片我已经收到了!".getBytes());
     	os.close();
     } catch (IOException e) {
     	e.printStackTrace();
     }finally {
     	try {
     		if(serverSocket != null) {
     			serverSocket.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     	try {
     		if(in != null) {
     			in.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     	try {
     		if(socket != null) {
     			socket.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     	try {
     		if(out != null) {
     			out.close();
     		}
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     }
    

    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    5.UDP
    /*

  • UDP网络编程
    */
    public class UDPTest {
    @Test
    public void sender() {
    DatagramSocket ds = null;
    try {
    ds = new DatagramSocket();
    String str = “UDP方式发送的信息”;
    byte[] data = str.getBytes();
    InetAddress inet;
    inet = InetAddress.getLocalHost();
    DatagramPacket dp = new DatagramPacket(data,0,data.length,inet,16404);
    ds.send(dp);

     } catch (SocketException e) {
     	e.printStackTrace();
     }catch (UnknownHostException e) {
     	e.printStackTrace();
     } catch (IOException e) {
     	e.printStackTrace();
     }finally {
     	try {
     		if(ds != null) {
     			ds.close();
     		}
     	} catch (Exception e) {
     		e.printStackTrace();
     	}
     }
    

    }

    @Test
    public void receiver() {
    DatagramSocket ds = null;
    try {
    ds = new DatagramSocket(16404);
    byte[] data = new byte[100];
    DatagramPacket dp = new DatagramPacket(data,0,data.length);
    ds.receive(dp);
    System.out.println(new String(dp.getData(),0,dp.getLength()));
    } catch (SocketException e) {
    e.printStackTrace();
    }catch (IOException e) {
    e.printStackTrace();
    }finally {
    try {
    if(ds != null) {
    ds.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    6.总结
    TCP和UDP的区别?
    TCP:可靠的数据传输(三次握手);大数据量的传输;效率低
    UDP:不可靠的数据传输;以数据报形式发送,数据报限定为64kb;效率高

感谢大家的支持,我终于升级了,上传限制得到提升,所以把资源整合下!希望大家一如既往 Java SE实践教程 pdf格式电子书 下载(一) 更新 http://download.csdn.net/source/2824033 Java SE实践教程 pdf格式电子书 下载(二) 更新 http://download.csdn.net/source/2824040 Java SE实践教程 pdf格式电子书 下载(三) 更新 http://download.csdn.net/source/2824042 Java SE实践教程 pdf格式电子书 下载(四) 更新 http://download.csdn.net/source/2824046 内容简介:此书结合具体实例讲解,通俗易懂,又不乏深度。我觉得这本书写的确实不错,堪称经典,市面上这样的书实在太少了,所以在这里发布下,供大家共享。本书从编程技术、项目实践以及软件工程的角度出发,如果大家想学习基础语法部分,建立去下载别的书籍。当然这本书也讲解了语法,是从实战的角度讲解的。 目录回到顶部↑第1章 进驻爪哇岛——JAVA的基本语法. 1 1.1 讲解 2 1.1.1 爪哇岛的历史与演变 2 1.1.2 爪哇岛基本生存规则 4 1.1.3 爪哇岛上新人新风尚 11 1.2 练习 15 1.2.1 搭建Java开发环境 15 1.2.2 体验Java程序开发 21 1.2.3 J2SE 5.0新特性实践 26 1.3 小结 35 第2章 对象无处不在——面向对象的基本概念 37 2.1 讲解 38 2.1.1 什么是面向对象 38 2.1.2 面向对象的基本概念 38 2.1.3 Java对面向对象的支持 41 2.2 练习 42 2.2.1 JavaBeans技术开发可重用组件 42 2.2.2 面向对象的基础实践 44 2.3 小结 51 第3章 当一个变成多个——集合框架的基本概念 53 .3.1 讲解 54 3.1.1 集合概述 54 3.1.2 Collection接口 54 3.1.3 泛型(Generics) 56 3.1.4 Map接口 57 3.2 练习 59 3.2.1 创建课程管理系统 59 3.3 小结 68 第4章 数据传送的管道——JAVA I/O 71 4.1 讲解 72 4.1.1 流——Java I/O的基础 72 4.1.2 Java I/O库 72 4.2 练习 74 4.2.1 数据传送的通道 74 4.2.2 管道的一端 76 4.2.3 文件处理 78 4.2.4 基于对象的读写 80 4.2.5 NIO 85 4.3 小结 89 第5章 如何走得更稳——测试驱动的基本概念 91 5.1 讲解 92 5.1.1 什么是JUnit 92 5.1.2 使用JUnit的一般过程 92 5.1.3 安装JUnit 93 5.2 编写单元测试 93 5.2.1 第1个单元测试 93 5.3 编写单元测试的步骤 95 5.3.1 常用断言 95 5.3.2 TestSuite 96 5.3.3 JUnit框架组成 96 5.4 练习 97 5.4.1 创建JUnit单元测试 97 5.4.2 setUp和tearDown 102 5.4.3 使用TestSuite 103 5.5 补充:JUNIT 4的新增特性 104 5.5.1 测试方法 104 5.5.2 初始化方法 105 5.5.3 TestSuite初始化 106 5.5.4 兼容性 106 5.6 小结 107 第6章 三头六臂——线程和同步的基本概念 109 6.1 讲解 110 6.1.1 什么是线程 110 6.1.2 创建线程 110 6.1.3 线程的生命周期 112 6.1.4 线程的优先级 114 6.1.5 中断线程 115 6.1.6 线程组 116 6.1.7 处理未被捕获的异常 117 6.1.8 守护线程 117 6.2 同步与锁 118 6.2.1 synchronized和同步 118 6.2.2 锁对象 120 6.2.3 Condition对象 121 6.2.4 再谈synchronized 122 6.3 协调任务 124 6.3.1 线程池和Executor 124 6.3.2 Callable和Future 126 6.3.3 ScheduledExecutorService 127 6.4 线程安全的集合和同步器 128 6.4.1 阻塞队列 128 6.4.2 指定阻塞时间 130 6.4.3 同步器 131 6.4.4 Atomic类型 134 6.5 练习 134 6.5.1 线程间同步 134 6.5.2 生产者、消费者问题.. 137 6.6 小结 140 第7章 我要彩色照片——SWING的基本概念 141 7.1 讲解 142 7.1.1 Swing的基本概念 142 7.1.2 Swing组件继承关系 142 7.1.3 Swing组件一览 143 7.1.4 Swing和MVC设计模式 144 7.1.5 Swing的单线程模型 145 7.2 练习 148 7.2.1 第1个Swing程序 148 7.2.2 外观感觉 150 7.2.3 事件侦听器 151 7.2.4 Swing基本控件和窗口 155 7.2.5 Swing容器 176 7.2.6 Swing高级控件 181 7.3 小结 187 第8章 朋友们,你们在哪里——JAVA数据库运用 189 8.1 讲解 190 8.1.1 数据库的基本概念 190 8.1.2 了解Java DB 190 8.1.3 JDBC 与 JDBC 4.0 191 8.1.4 用Java让数据库动起来 192 8.1.5 事务处理简介 194 8.2 练习 195 8.2.1 数据库操作 195 8.2.2 我的联系手册 199 8.2.3 事务处理 221 8.3 小结 222 第9章 还想再见到你——数据持久化 223 9.1 讲解 224 9.1.1 数据持久化的基本概念 224 9.1.2 数据持久化技术简介 224 9.1.3 Java中的对象关系映射 225 9.1.4 对象XML序列化 227 9.2 练习 228 9.2.1 我的联系手册(JPA实现) 228 9.2.2 我的联系手册(JAXB实现) 238 9.3 小结 242 第10章 准备环球旅行——应用程序国际化 243 10.1 讲解 244 10.1.1 概念介绍 244 10.1.2 设置Locale 249 10.1.3 隔离语言环境相关数据 252 10.1.4 格式化 258 10.2 练习 266 10.2.1 对单独的文件进行国际化 266 10.2.2 在设计时国际化GUI表单 273 10.2.3 国际化整个项目 277 10.3 小结 281 第11章 请保持联系——JAVA网络连接 283 11.1 讲解 284 11.1.1 Java网络连接的基本概念 284 11.1.2 使用TCP协议的Socket网络编程 285 11.1.3 使用UDP协议的Socket 网络编程 289 11.1.4 多点传送和MulticastSocket类 290 11.1.5 NIO及相关技术 290 11.2 练习 291 11.2.1 ServerSocket与Socket示例: 开发一个Server-Client模型的程序 291 11.2.2 多点传送示例 293 11.2.3 打造你自己的QQ 295 11.3 小结 315 第12章 找个好管家——JMX 317 12.1 讲解 318 12.1.1 什么是 JMX 318 12.1.2 JMX基本概念 318 12.1.3 JMX管理应用开发 323 12.2 练习 325 12.2.1 建立JMX开发环境 325 12.2.2 创建拼词游戏程序 326 12.2.3 分布式管理扇形绘制Applet 程序 330 12.3 小结 350 第13章 让我们更专业——软件工程的基本概念 351 13.1 讲解 352 13.1.1 软件工程的基本概念 352 13.1.2 版本控制系统 356 13.1.3 统一建模语言 359 13.2 练习 360 13.2.1 建立CVS的使用环境和基本操作 360 13.2.2 使用标记和分支 373 13.3 使用UML建模 376 13.3.1 对基本结构建模 376 13.3.2 对高级结构建模 381 13.4 小结 387
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值