1.TCP通信:
TCP类似于打电话,需要服务器端开启才能进行信息传输
客户端/发送端:
复制代码
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
49package TCP; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; public class Client { public static void main(String[] args) { //1.获取服务器地址和端口号 InetAddress serverIp = null; int port =0; Socket socket =null; OutputStream out = null; try { serverIp = InetAddress.getByName("127.0.0.1"); port=9999; //2.创建输出对象 socket = new Socket(serverIp,port); //3.获取输出流 out = socket.getOutputStream(); //4.输出内容 out.write("你好呀".getBytes(StandardCharsets.UTF_8));//通过字节传输 } catch (Exception e) { e.printStackTrace(); } finally { if (socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null){ try { 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
70package TCP; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream in = null; ByteArrayOutputStream baos = null;//管道输出流(有缓冲的作用) try { //1.创建一个端口 serverSocket = new ServerSocket(9999); //2.等待用户连接 socket = serverSocket.accept(); //3.读取客户端消息 in = socket.getInputStream(); //使用管道流接收 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len ; while((len = in.read(buffer))!=-1) { baos.write(buffer,0,len); } System.out.println(baos.toString("utf-8")); } catch (IOException e) { e.printStackTrace(); } finally { if (baos!=null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
注意:服务器端和客户端是相对而言的,服务器端也可以是发送端
服务器端接收到信息并处理后就停止了,可以通过while+信号灯的方式一直开启,不过serverSocket.accept();方法会将开启的端口进行堵塞连接
2.TPC实现文件传输:
发送端:
复制代码
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
83package FileTransfer; import java.io.*; import java.net.InetAddress; import java.net.Socket; public class Send { public static void main(String[] args) { Socket socket = null; OutputStream out = null; FileInputStream fin = null; ByteArrayOutputStream baos = null; InputStream in = null; try { //获取连接 socket = new Socket(InetAddress.getByName("127.0.0.1"),9999); out = socket.getOutputStream(); //获取文件 fin = new FileInputStream("src/en.jpg");//文件路径 byte[] buffer = new byte[1024]; int len =0; //写文件 while((len = fin.read(buffer))!=-1){ out.write(buffer,0,len); } //通知服务器我已经传输完了 socket.shutdownOutput(); //获取服务器的回复,确保服务器接收完成 in = socket.getInputStream(); //使用管道流 baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[1024]; int len2 =0; while((len2 = in.read(buffer2))!= -1){ baos.write(buffer2); } System.out.println(baos.toString("utf-8")); } catch (Exception e) { e.printStackTrace(); } finally{ //关闭连接 if (baos!=null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (fin!=null){ try { fin.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket!=null){ try { 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
77package FileTransfer; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; public class Receive { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream in = null; FileOutputStream fos = null; OutputStream out = null; try { //创建服务 serverSocket = new ServerSocket(9999); //创建监听客户端的连接,接收消息 socket = serverSocket.accept(); //获取输入流 in = socket.getInputStream(); //文件传输 fos = new FileOutputStream("shoudaode.jpg"); byte[] buffer = new byte[1024]; int len = 0; while((len = in.read(buffer)) != -1){ fos.write(buffer,0,len); } //告诉客户端我接收完毕 out = socket.getOutputStream(); out.write("我已经接收完成了,你可以关闭了".getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { e.printStackTrace(); }finally{//关闭连接 if (out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (serverSocket != null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
最后
以上就是稳重导师最近收集整理的关于java网络 TCP使用案例的全部内容,更多相关java网络内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复