天天看点

java实现tcp服务端多线程初级

很基础的简单写了一下,后续有改进

//ClientDemo.java 客户端

package cxjj.TCPDemo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //1 建立客户端socket
        Socket s = new Socket("192.168.43.186",10006);
        // 2 读取客户端要上传的图片
        FileInputStream fis = new FileInputStream("D:\\0.jpg");
        // 3 获取socket输出流
        OutputStream out = s.getOutputStream();
        byte [] buff = new byte[1024];
        int len = 0;
        while (( len = fis.read(buff))!= -1){
            out.write(buff,0,len);
        }
        // 4 告诉服务端说:这边的数据发送完毕。让服务端停止读取。
        s.shutdownOutput();

        // 5 读取服务端发回的数据,一般来说服务端发送的数据很小,所以1024够用了
        InputStream in = s.getInputStream();
        byte[] buffIn = new byte[1024];
        int lenIn = in.read(buffIn);
        String text = new String(buffIn,0,lenIn);
        System.out.println(text);

        fis.close();
        s.close();
    }
}

           

// ServerDemo.java服务端

package cxjj.TCPDemo;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    /**
     * 创建服务端的思路:
     *  1 创建服务端socket服务,通过serversocket 对象
     *  2 服务端必须对外提供一个端口,否则客户端无法连接
     *  3 获取客户端socket流
     *
     * */
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10006);
        while (true){
            Socket s = ss.accept();
            new Thread(new uploadTask(s)).start();
        }
    }
}

           

//uploadTask .java

package cxjj.TCPDemo;

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

public class uploadTask implements Runnable {
    private static final int SIZE = 1024*1024*2;
    private  Socket s ;
    public uploadTask(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        int count = 0;
        String ip = s.getInetAddress().getHostAddress();
        System.out.println(ip);
        // 1 读取客户端发来的数据
        try {
            InputStream in = s.getInputStream();
            // 把读取到的文件存储到一个文件中
            File dir = new File("picture");
            while (!dir.exists()){
                dir.mkdir();
            }
            File file = new File(dir,ip + ".jpg");
            // 如果文件已经存在于服务端
            while (file.exists()){
                file = new File(dir,ip+ "("+ (++count) + ").jpg");
            }

            FileOutputStream out = new FileOutputStream(file);

            byte[] buff = new byte[1024];
            int len  = 0;
            while ((len = in.read(buff))!= -1){
                out.write(buff,0,len);
                //下面也可以判断上传文件的大小
                if(file.length()>SIZE){
                    System.out.println(ip+"文件体积过大");
                    out.close();
                    s.close();
                    System.out.println(ip+"...."+file.delete());
                    return ;
                }
            }
            OutputStream outputStream = s.getOutputStream();
            outputStream.write("上传成功!".getBytes());

            out.close();
            s.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}