欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于多线程的TCP局域网通信,客户端向服务端上传 文件简单实现源码

程序员文章站 2024-03-22 19:04:52
...

客户端


import java.io.*;
import java.net.Socket;

/**
 * ### 作业3:
 * 完成文件上传案例学习
 * - 案例需求
 * 客户端:数据来自于本地文件,接收服务器反馈
 * 服务器:接收到的数据写入本地文件,给出反馈
 * - 案例分析
 * - 创建客户端对象,创建输入流对象指向文件,每读一次数据就给服务器输出一次数据,输出结束后使用shutdownOutput()方法告知服务端传输结束
 * - 创建服务器对象,创建输出流对象指向文件,每接受一次数据就使用输出流输出到文件中,传输结束后。使用输出流给客户端反馈信息
 * - 客户端接受服务端的回馈信息
 */
public class TcpSocket {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 10086);
        BufferedInputStream buffInput = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\Demo小案例\\共享文件夹\\IEDA背景图片\\img\\1.png"));
        BufferedOutputStream buffOut = new BufferedOutputStream(socket.getOutputStream());
        byte[] bytes = new byte[1024];
        int length;
        while ((length = buffInput.read(bytes)) != -1) {
            buffOut.write(bytes, 0, length);
            buffOut.flush();
        }
        buffInput.close();
        socket.shutdownOutput();

        //读取反馈
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
        socket.close();

    }
}

服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**

  • @author silence
    */
    public class TcpServerSocket {
    public static void main(String[] args) throws IOException {
    //监听端口
    ServerSocket socket = new ServerSocket(10086);
    //创建线程池
    ThreadPoolExecutor thread = new ThreadPoolExecutor(
    5,
    10,
    60,
    TimeUnit.SECONDS,
    new ArrayBlockingQueue<>(5),
    Executors.defaultThreadFactory(),
    new ThreadPoolExecutor.AbortPolicy()
    );

     while (true) {
         //获得与客户端的连接对象
         Socket accept = socket.accept();
         //将连接对象交给线程处理
         thread.submit(() -> {
             ioFile(accept);
         });
     }
    

    }

    /**

    • @param accept 与客户端的连接对象
      */
      private static void success(Socket accept) {
      //字符输出
      BufferedWriter writer = null;
      try {
      //通过于客户端的连接,获得网络连接对象,获得字节输出流
      OutputStream out = accept.getOutputStream();
      //通过转换流,转换到字符输出流
      writer = new BufferedWriter(new OutputStreamWriter(out));
      writer.write(“服务器接收文件成功”);
      writer.flush();
      } catch (IOException e) {
      e.printStackTrace();
      } finally {
      if (writer != null) {
      try {
      writer.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      try {
      accept.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }

    /**

    • 读取客户端的文件信息,写到本地
    • @param accept 与客户端的连接对象
      */
      private static void ioFile(Socket accept) {
      //字节输入流
      BufferedInputStream input = null;
      //字节输出流
      BufferedOutputStream out = null;
      try {
      //字节输入流
      input = new BufferedInputStream(accept.getInputStream());
      //字节输出流
      out = new BufferedOutputStream(new FileOutputStream(“operate/src/operate424/operate/operate3/” + UUID.randomUUID() + “.png”));
      //每次读取的字节数组
      byte[] bytes = new byte[1024];
      //每次读取的字节个数
      int length;
      while ((length = input.read(bytes)) != -1) {
      out.write(bytes, 0, length);
      }
      System.out.println(“服务器成功接收到一个文件”);
      //给客户端写成功信息
      success(accept);
      } catch (IOException e) {
      e.printStackTrace();
      } finally {
      if (out != null) {
      try {
      out.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      if (input != null) {
      try {
      input.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
      }
      }