import java.io.*;
import java.net.*;public class FileServer implements Runnable 
{
    private ServerSocket server;
    
    public FileServer(int port)
    {
        try
        {
            server = new ServerSocket(port);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    public void run()
    {
        try
        {
            Socket socket = server.accept();
            
            File file = new File("d:\\2.bmp");
            FileOutputStream fileOut = new FileOutputStream(file);
            InputStream socketIn = socket.getInputStream();
            OutputStream socketOut = socket.getOutputStream();
            DataInputStream dataIn = new DataInputStream(socketIn);
            PrintWriter writerOut = new PrintWriter(new OutputStreamWriter(socketOut));
            
            try
            {
             boolean flag=true;
             byte[] bytes = new byte[1024];
             while ((dataIn.read(bytes)) != -1)
             {
             if(flag)
             {
             socket.setSoTimeout(100);
             flag=false;
             }
             fileOut.write(bytes);
             }
            }
            catch (Exception e)
            {
             /*
              * 请注意这里,如果上面的Exception e改为SocketException e就会报错
              * 这是为什么?
              */
             if(e instanceof SocketException)
             {
                    
                }
            }
            
            fileOut.close();
            
            writerOut.println("OK");
            writerOut.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args)
    {
        new Thread(new FileServer(9999)).start();
    }
}import java.io.*;
import java.net.*;public class FileClient implements Runnable
{
    private Socket socket;
    
    public FileClient(String ip, int port)
    {
        try 
        {
            socket = new Socket(ip, port);
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    
    public void run()
    {
        try
        {
            InputStream socketIn = socket.getInputStream();
            File file = new File("d:\\1.bmp");
            FileInputStream fileIn = new FileInputStream(file);
            OutputStream socketOut = socket.getOutputStream();
            BufferedReader readerIn = new BufferedReader(new InputStreamReader(socketIn));
            DataOutputStream dataOut = new DataOutputStream(socketOut);            byte[] bytes = new byte[1024];
            while ((fileIn.read(bytes)) != -1)
            {    
                dataOut.write(bytes);
            }
            //dataOut.close();
            
            String str = readerIn.readLine();
            System.out.println(str);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args)
    {
        new Thread(new FileClient("localhost", 9999)).start();
    }
}

解决方案 »

  1.   

    因为除了SocketException还可能抛其他checked exception,单单用SocketException是不够的
      

  2.   

    楼上说的有道理。有的方法,在定义的时候,设计对于储多情况抛出各种异常。但是,一般情况下,有些异常是几乎不可能发生的,我们并不关心这种情况。不过,你的语句必须捕获这些各种各样的异常,否则编译不通过。一种是你连接写好几个catch。但是之前已经说了,我并不关心这些异常的处理,那我写的不累吗?所以,我直接catch(Exception e),这样就OK了,然后,我对我关心的异常类型设计处理方法。
      

  3.   

    SocketException是Exception的子类,异常有多种,我们尽量去捕捉一些比较明显或可能的异常,不可知的情况下就直接catch(Exception e)了。