黑马程序员技术交流社区

标题: 关于UDP上传服务器文件的一个问题,抛出SocketException [打印本页]

作者: 贾浩田    时间: 2014-9-27 21:25
标题: 关于UDP上传服务器文件的一个问题,抛出SocketException
  1. public class Test10 {

  2.         /**配合第九题的服务端
  3.          * @author :贾浩田
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 try {
  8.                         ServerSocket ss = new ServerSocket(6007);
  9.                         Socket s = ss.accept();
  10.                         //启动服务端线程
  11.                         new Thread(new Server(s)).start();

  12.                                           
  13.                 } catch (IOException e) {
  14.                         // TODO Auto-generated catch block
  15.                         e.printStackTrace();
  16.                 }
  17.         }
  18. }
  19. //把服务端要处理的内容放在线程中
  20. class Server implements Runnable{
  21.         private Socket s;       
  22.         public Server(Socket s){
  23.                 this.s = s;
  24.         }
  25.         @Override
  26.         public void run() {
  27.                 // TODO Auto-generated method stub
  28.                 //定义一个字节输出流
  29.                 FileOutputStream file = null;
  30.                 File f = new File("coby.jpg");
  31.                 try {
  32.                         f.createNewFile();
  33.                         file = new FileOutputStream(f);
  34.                 } catch (IOException e) {
  35.                         // TODO Auto-generated catch block
  36.                         e.printStackTrace();
  37.                 }
  38.                 String ip = s.getInetAddress().getHostAddress();
  39.                 String host_name = s.getInetAddress().getHostName();
  40.                 //打印客户端信息
  41.                 System.out.println("ip:"+ip+ " \t hostName:"+ host_name);
  42.                 //读取客户端的字节流
  43.                 try {
  44.                         InputStream in = s.getInputStream();
  45.                         byte[] buf = new byte[1024];
  46.                         int num = 0;
  47.                         while((num = in.read(buf)) != -1){
  48.                                 file.write(buf, 0, num);
  49.                                 file.flush();
  50.                         }       
  51.                 } catch (IOException e) {
  52.                         // TODO Auto-generated catch block
  53.                         e.printStackTrace();
  54.                 }
  55.                 //给客户端发送反馈
  56.                 try {
  57.                         OutputStream out = s.getOutputStream();
  58.                         out.write("图片上传成功!!".getBytes());
  59.                         //out.flush();
  60.                 } catch (IOException e) {
  61.                         // TODO Auto-generated catch block
  62.                         e.printStackTrace();
  63.                 }
  64.                        
  65.                 try {
  66.                         file.close();  //关闭文件流
  67.                         s.close();     //关闭客户端
  68.                 } catch (Exception e) {
  69.                         // TODO Auto-generated catch block
  70.                         e.printStackTrace();
  71.                 }
  72.         }       
  73. }
复制代码

总是报错:以下是打印信息,希望大家帮我分析一下
ip:192.168.1.101          hostName:lenovo-PC
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at com.itheima.Server.run(Test10.java:59)
        at java.lang.Thread.run(Unknown Source)
java.net.SocketException: Connection reset by peer: socket write error
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(Unknown Source)
        at java.net.SocketOutputStream.write(Unknown Source)
        at com.itheima.Server.run(Test10.java:70)
        at java.lang.Thread.run(Unknown Source)



作者: 华谦    时间: 2014-9-27 21:42
是不是你本地Iip地址弄错了?
作者: 贾浩田    时间: 2014-9-28 00:04
搞定了,把服务端和客户端两个具备主函数的类放在一个.java源文件中,然后用IDE分别运行服务端和客户端模拟通信,并使用Console窗口右侧的Display Selected来切换窗口查看输出信息




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