(5)优化TCP编写 客服端上传图片,服务端给客服端提示接收状态

(5)优化TCP编写 客服端上传图片,服务端给客服端提示接收状态

模拟QQ上传文件时,提示文件上传成功,首先要先启动服务端,否则报错,因为TCP是可靠协议,所以没有对应的服务端时会报错,发送数据为图片

一.编写TCP协议服务端

 1 package demo.soket.tcp;
 2  3   import java.io.BufferedInputStream;
 4   import java.io.BufferedOutputStream;
 5   import java.io.FileOutputStream;
 6   import java.io.IOException;
 7   import java.io.OutputStream;
 8   import java.net.ServerSocket;
 9   import java.net.Socket;
10 11   //TCP协议服务端
12   public class Server {
13       public static void main(String[] args) {
14           /**
15            * 1.创建服务端ServerSocket对象
16            * 2.监听socket连接
17            * 3.通过返回的socket对象来获取io流里面的数据
18            * 4.打印数据
19            */
20           //1.
21           ServerSocket server=null;
22           Socket client=null;
23           BufferedOutputStream os=null;
24           try {
25               server=new ServerSocket(12345);
26               //2.
27               client=server.accept();
28               //3.
29               //字节缓冲输入流,效率高
30               BufferedInputStream bis=new BufferedInputStream(client.getInputStream());
31               
32               //接收客服端胡消息
33               os=new BufferedOutputStream(new FileOutputStream("客户端发过来的图片.jpg"));
34               byte[] b=new byte[1024];
35               int len=0;
36               while((len=bis.read(b))!=-1) {
37                   os.write(b, 0, len);
38                   os.flush();//刷出缓冲流中的数据,关闭连接前一定要刷,否则会数据缺失
39               }
40               System.out.println("-----------------");
41               System.out.println("通知客服端我已经接收了你的图片");
42               //给客服端发消息
43               OutputStream out=client.getOutputStream();
44               out.write("图片上传成功".getBytes());
45           } catch (IOException e) {
46               e.printStackTrace();
47           }finally {
48               if(client!=null) {
49                   try {
50                       client.close();
51                   } catch (IOException e) {
52                       e.printStackTrace();
53                   }
54               }
55               if(os!=null) {
56                   try {
57                       os.close();
58                   } catch (IOException e) {
59                       e.printStackTrace();
60                   }
61               }
62           }
63       }
64 65   }
66

二.编写TCP协议客服端

 1  package demo.soket.tcp;
 2  3   import java.io.BufferedInputStream;
 4   import java.io.BufferedOutputStream;
 5   import java.io.FileInputStream;
 6   import java.io.IOException;
 7   import java.io.InputStream;
 8   import java.net.Socket;
 9   import java.net.UnknownHostException;
10 11   /**
12    * TCP特点:
13    * 1.建立连接,形成传输数据的通道
14    * 2.在链接中进行大量数据传输
15    * 3.通过三次握完成连接,是可靠协议,所以没有对应的服务端时会报错
16    * 4.必建立连接,效率会稍低些
17    * @author Administrator
18    *
19    */
20   public class Client {
21       public static void main(String[] args) {
22           /**
23            * 1.创建一个客服端的socket对象
24            * 2.建立连接
25            * 3.通过io流在管道里面传输数据
26            * 写数据:输出流 getOutputStream() 
27            * 接收数据:输入流 getInputStream() 
28            * 4.关闭socket
29            */
30           //1.|2.
31           Socket client=null;
32           try {
33               client=new Socket("192.168.95.2",12345);
34               //3.
35               BufferedOutputStream os=new BufferedOutputStream(client.getOutputStream());
36               String path="C:\Users\Administrator\Pictures\微信图片_20200629170031.jpg";
37               BufferedInputStream buf=new BufferedInputStream(new FileInputStream(path));
38               byte[] b=new byte[1024];
39               int len=0;
40               while((len=buf.read(b))!=-1) {
41                   os.write(b, 0, len);
42                   os.flush();//关闭连接前一定要刷,否则会数据缺失
43               }
44               System.out.println("-----------------");
45               /**
46                * 关闭连接以进行写入,而不关闭通道。
47                * 这句代码一定要加:否则服务器端不知道你图片什么时候传完,while中会一直连接通道,就不会走下一步
48                */
49               client.shutdownOutput();
50               //获取服务端发过来的消息
51               InputStream in=client.getInputStream();
52               byte[] bi=new byte[1024];
53               int leng=0;
54               while((leng=in.read(bi))!=-1) {
55                   System.out.println("收到服务器通知:"+new String(bi,0,leng));
56               }
57               //关闭读文件操作
58               buf.close();
59               os.close();
60           } catch (UnknownHostException e) {
61               e.printStackTrace();
62           } catch (IOException e) {
63               e.printStackTrace();
64           }finally {
65               //4.
66               if(client!=null) {
67                   try {
68                       client.close();
69                   } catch (IOException e) {
70                       e.printStackTrace();
71                   }
72               }
73           }
74       }
75   }
76

三 .demo运行

刷新项目后会多一张上传的图片,以及可以看到服务器发送的通知:

 

原文地址:https://www.cnblogs.com/KdeS/p/13278339.html