TCP通信程序练习(上传文件服务器给出反馈)
package NetworkProgramming.TCP.SocketDemo05;
import java.io.*;
import java.net.Socket;
/**
* FileName: ClientDemo
* Author: lps
* Date: 2022/4/2 14:54
* Sign:刘品水 Q:1944900433
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.10.102",1944);
BufferedReader br = new BufferedReader(new FileReader("F:\\IdeaProjects\\bos.java"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line= br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//自定义结束标记
// bw.write("886");
// bw.newLine();
// bw.flush();
////void shutdownInput()
// //将此套接字的输入流放在“流结束”中。
// //void shutdownOutput()
// //禁用此套接字的输出流。
s.shutdownOutput();
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data= brClient.readLine();
System.out.println("服务器端的反馈为:"+data);
br.close();
s.close();
bw.close();
}
}
package NetworkProgramming.TCP.SocketDemo05;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* FileName: ServerDemo
* Author: lps
* Date: 2022/4/2 14:54
* Sign:刘品水 Q:1944900433
* @author lps
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1944);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\IdeaProjects\\copy.java"));
String line;
while ((line= br.readLine())!=null){
// if ("886".equals(line)){
// break;
// }
bw.write(line);
bw.newLine();
bw.flush();
System.out.println("接收到的数据为:"+line);
}
//void shutdownInput()
//将此套接字的输入流放在“流结束”中。
//void shutdownOutput()
//禁用此套接字的输出流。
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传完成");
bwServer.newLine();
bwServer.flush();
s.close();
bw.close();
br.close();
}
}