- package com.itheima;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.io.*;
- import java.net.*;
- //客户端
- class UpLoadText{
- public void main(String[] args) throws Exception {
- System.out.println("fffffffff");
- Socket s = null;
- s = new Socket("localhost",8000);
- BufferedReader bufr = new BufferedReader(new FileReader("Test9.java"));
- PrintWriter out = new PrintWriter(s.getOutputStream(),true);
-
- String line = null;
- while((line=bufr.readLine())!=null){
- out.println(line);
- }
- s.shutdownOutput();//禁用客户端的输出流,相当于给流中加入结束标记-1。
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = bufIn.readLine();
- System.out.println(str);
- bufr.close();
- s.close();
- }
- }
- //服务端
- class Test9{
- public static void main(String[] args) throws Exception {
- new UpLoadText().main(args);
- ServerSocket ss = new ServerSocket(8000);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println("来自IP:"+ip);
-
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter out = new PrintWriter(new FileWriter("Test9_i.java"),true);
-
- String line = null;
- while((line=bufIn.readLine())!=null){
- if("over".equals(line))
- break;
- out.println(line);
- }
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("上传成功");//给客户端发送提示信息。
- out.close();
- s.close();
- ss.close();
-
- }
- }
复制代码 |