开始写的那个没加结束符
重新写一个
服务端
- package com.cn.test2;
- import java.io.*;
- import java.net.*;
- public class TCPFileServer {
- public static void main(String[] args) throws Exception {
- System.out.println("服务端开启……");
- ServerSocket ss=new ServerSocket(5246);
- Socket s=ss.accept();
- BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));//接收的
- PrintWriter pw=new PrintWriter(new FileWriter("TCPRcv.txt"),true);
- String line=null;
- while((line=br.readLine())!=null){
- pw.println(line);
- }
- //上传成功后给客户端一个提示
- PrintWriter pwAfter=new PrintWriter(s.getOutputStream(),true);
- pwAfter.println("上传完成");
- pw.close();
- s.close();
- ss.close();
- }
- }
复制代码
客户端
- package com.cn.test2;
- import java.io.*;
- import java.net.*;
- public class TCPUpFileClient {
- public static void main(String[] args) throws Exception {
- System.out.println("客户端开启……");
- Socket s=new Socket(InetAddress.getByName("192.168.1.4"),5246);
- BufferedReader br=new BufferedReader(new FileReader("ForTCP.txt"));
- PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
- String line=null;
- while((line=br.readLine())!=null){
- pw.println(line);
- }
- s.shutdownOutput();//表示关闭输出流
- BufferedReader brAfter=new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str=brAfter.readLine();
- System.out.println(str);
- br.close();
- s.close();
- }
- }
复制代码
|
|