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

1106

被折叠的 条评论
为什么被折叠?



