A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© dubei1993 中级黑马   /  2016-6-9 22:49  /  822 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.itcast_03;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
//tcp上传文件,这是服务器端
public class ServerDemo {
public static void main(String[] args) throws IOException {
  // 创建服务器端Socket对象
  ServerSocket ss = new ServerSocket(9100);
  // 创建客户端监听对象,返回Socket对象
  Socket s = ss.accept();
  // 封装通道内流
  BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  // 封装目录
  BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
  // 复制文件
  String line = null;
  while ((br.readLine()) != null) {
   bw.write(line);
   bw.newLine();
   bw.flush();
  }
  bw.close();
  s.close();
}
}

package cn.itcast_03;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
//tcp协议客户端
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket接口
Socket s=new Socket("localhost",9100);
//封装文件目录
BufferedReader br=new BufferedReader(new FileReader("aaa.txt"));
//封装通道内流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((br.readLine())!=null){
  bw.write(line);
  bw.newLine();
  bw.flush();
}
br.close();
s.close();
}
}


3 个回复

倒序浏览
Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at cn.itcast_03.ServerDemo.main(ServerDemo.java:24)
回复 使用道具 举报
把while((br.readLine())!=null){改成while((line=br.readLine())!=null)试下
回复 使用道具 举报
Fangjie 发表于 2016-6-10 10:49
把while((br.readLine())!=null){改成while((line=br.readLine())!=null)试下

嗯。好了。多谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马