2021-09-27 网络编程(TCP复制文件)

2021-10-09  本文已影响0人  Denholm
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TextClient {

    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("192.168.1.6", 10006);
        BufferedReader bufr = new BufferedReader(new FileReader("E:\\ChatDemo.java"));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        String line;
        while ((line = bufr.readLine()) != null) {
            out.println(line);
        }
        // 关闭客户端的输出流,相当于给流中加入一个结束标记-1
        socket.shutdownOutput();

        BufferedReader bufIn = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        System.out.println(bufIn.readLine());

        bufr.close();
        socket.close();
    }

}
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TextServer {

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(10006);
        Socket accept = server.accept();
        System.out.println(accept.getInetAddress().getHostAddress() + "...connected");
        BufferedReader bufr = new BufferedReader(new InputStreamReader(accept.getInputStream()));
        PrintWriter out = new PrintWriter(new FileWriter("E:\\Server.txt"), true);
        String line;
        while ((line = bufr.readLine()) != null) {
            out.println(line);
        }
        PrintWriter pw = new PrintWriter(accept.getOutputStream(), true);
        pw.println("上传成功");

        server.close();
        accept.close();
        out.close();
    }

}
上一篇下一篇

猜你喜欢

热点阅读