黑马程序员技术交流社区

标题: 为什么在eclipse上面运行tcp练习时出现异常 [打印本页]

作者: 徐梦侠    时间: 2012-10-12 22:14
标题: 为什么在eclipse上面运行tcp练习时出现异常
一直有个问题没想到解决办法,代码在eclipse上面运行tcp练习时出现SocketException异常 ,但是在其他在命令行中能够正常运行不知道为什么在eclipse中就不能运行呢?这和tcp协议有关系吗?练习udp的时候能在eclipse中运行。还是说这段代码有什么问题?
import java.io.*;
import java.net.*;
class TextClient{
public static void main(String[] args)throws Exception{
  Socket s=new Socket("127.0.0.1",10009);
  BufferedReader bufr=
    new BufferedReader(new FileReader("UdpDemo.java"));
  PrintWriter out=new PrintWriter(s.getOutputStream(),true);
  String line=null;
  while((line=bufr.readLine())!=null){
   out.println(line);
  }
  s.shutdownOutput();//关闭客户端输出流,相当于给流中加入一个结束标记
  BufferedReader bufIn=
    new BufferedReader(new InputStreamReader(s.getInputStream()));
  String str=bufIn.readLine();
  System.out.println(str);
  bufr.close();
  s.close();
}
}
class TextServer{
public static void main(String[] args) throws Exception {
  ServerSocket ss=new ServerSocket(10009);
  Socket s=ss.accept();
  String ip=s.getInetAddress().getHostAddress();
  System.out.println(ip+"......is connected");
  BufferedReader bufIn=
    new BufferedReader(new InputStreamReader(s.getInputStream()));
  PrintWriter out=new PrintWriter(new FileWriter("server.txt"),true);
  String line=null;
  while((line=bufIn.readLine())!=null){
   out.println(line);
  }
  PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
  pw.println("上传成功");
  out.close();
  s.close();
  ss.close();
}
}
public class UploadText {
public static void main(String[] args) {
}
}
我先运行TextServer,再运行TextClient,发生下面结果
127.0.0.1......is connected
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(
SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at com.soho.TextServer.main(UploadText.java:35)







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2