2021-09-29 网络编程(TCP-客户端并发上传图片)

2021-10-08  本文已影响0人  Denholm
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class PicClient1 {

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.out.println("请选择一个jpg格式的图片!");
            return;
        }
        File file = new File(args[0]);
        if (!(file.exists() && file.isFile())) {
            System.out.println("该文件不存在或者不是文件!");
            return;
        }
        if (!(file.getName().endsWith(".jpg"))) {
            System.out.println("图片格式有误,请重新选择!");
            return;
        }
        if (file.length() > 1024 * 1024 * 5) {
            System.out.println("图片过大!");
            return;
        }

        Socket socket = new Socket("192.168.1.6", 10008);
        FileInputStream fis = new FileInputStream(file);
        OutputStream os = socket.getOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = fis.read(buf)) != -1) {
            os.write(buf, 0, len);
        }

        InputStream is = socket.getInputStream();
        socket.shutdownOutput(); // 告诉服务器数据已写完
        byte[] bufIn = new byte[1024];
        int num = is.read(bufIn);
        System.out.println(new String(bufIn, 0, num));

        socket.close();
        fis.close();
    }

}

该服务端有局限性,当A客户端连接上以后,被服务端获取到,服务端执行具体流程,这是B客户端连接,只有等待,因为该服务端还没有处理完A客户的请求,还没有循环回来执行下次accept请求,所以暂时获取不到B客户端对象

为了可以让多个客户端同时并发访问服务器,那么服务器最好将每一个客户端封装到一个单独的线程中,这样,就可以同时处理多个请求。

如何定义线程?
只要明确了每一个客户端要在服务器端执行的代码即可,将该代码存入到run方法中

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

class PicThread implements Runnable {

    private Socket socket;

    public PicThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        int count = 1;
        String ip = socket.getInetAddress().getHostAddress();
        try {
            System.out.println(ip + "...connected");
            InputStream in = socket.getInputStream();
            File file = new File(ip + "(" + count + ")" + ".jpg");
            while (file.exists()) {
                file = new File(ip + "(" + count + ")" + ".jpg");
            }
            FileOutputStream fos = new FileOutputStream(file);
            int len;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            OutputStream out = socket.getOutputStream();
            out.write("上传成功".getBytes());
            fos.close();
            socket.close();
        } catch (Exception e) {
            throw new RuntimeException(ip + "连接失败");
        }
    }

}

public class PicServer1 {

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(10008);
        while (true) {
            Socket socket = server.accept();
            new Thread(new PicThread(socket)).start();
        }
    }

}
上一篇 下一篇

猜你喜欢

热点阅读