java.net.SocketException: Connection reset 20

publicclassClient{publicstaticvoidmain(Stringargs[])throwsException,IOException{Strin... public class Client {

public static void main(String args[]) throws Exception,IOException {
String host = "127.0.0.1";
int port = 8899;
Socket client = new Socket(host, port);
PrintWriter writer = new PrintWriter(new OutputStreamWriter (client.getOutputStream()),true);

File file = new File("test.xml"); //用于发送的文件路径
BufferedReader reader = new BufferedReader(new FileReader(file));
String str=null;
while((str = reader.readLine()) != null)
writer.println(str);
writer.flush();

BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("result.xml",false)));

String st=null;

while((st=br.readLine())!=null){// br.reandLine()这 报错
out.println(st);
}

out.flush();

out.close();
writer.close();
reader.close();
client.close();
}

}

public class Server {
public static void main(String args[]) throws Exception {

String URL="query.xml";
int port = 8899;

ServerSocket server = new ServerSocket(port);

Socket socket = server.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(URL,false)));
String str=null;
String xml=new String();
while((str = br.readLine()) != null){
xml+=str+"/n";
out.println(str);
}
out.flush();
System.out.println(xml);

PrintWriter writer = new PrintWriter(new OutputStreamWriter (socket.getOutputStream()),true);

File file = new File("ser.xml"); //用于发送的文件路径
BufferedReader reader = new BufferedReader(new FileReader(file));
String stre=null;
while((stre = reader.readLine()) != null){
System.out.println(stre);
writer.println(stre);}
writer.flush();
writer.close();
reader.close();
out.close();
socket.close();
server.close();
}
}
错误好像在Client 中的br.readLine()处,怎么解决???
展开
 我来答
蔡平year
2015-02-13
知道答主
回答量:22
采纳率:0%
帮助的人:15.2万
展开全部
。。。。逻辑有问题.当你读取一行的时候是没有读到东西的.所以不会进入while 直接就关闭资源了. 而服务器端 正在监测.正好客户端已经关闭了。所以报错了.错误是连接被重置
追问
怎么解决呢??
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式