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

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

作者: Denholm | 来源:发表于2021-10-09 15:48 被阅读0次
    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();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:2021-09-27 网络编程(TCP复制文件)

          本文链接:https://www.haomeiwen.com/subject/wvwknltx.html