阻塞、超时和关闭

357 查看

Socket的I/O调用可能会因为多种原因阻塞,数据输入方法read和receive方法在没有数据可读时会阻塞。

TCP套接字的write方法在没有足够的空间缓存传输的数据时可能阻塞,ServerSocket的accept方法和Socket的构造函数都会阻塞等待,直到连接建立。同时,长的信息往返时间,高错误率的连接和慢速的(或已发生故障的)服务器,都可能导致需要很长的时间来建立连接。所有这些情况,只有在连接请求得到满足后这些方法才会返回。

accept、read、receive方法,可以使用Socket类、ServerSocket类和DatagramSocket类的setSoTimeout方法,设置其阻塞的最长时间(以毫秒为单位)。如果在指定时间内这些方法没有返回,则将抛出一个InterruptedIOException异常。

对于Socket实例,在调用read方法前,还可以使用InputStream的available方法来检测是否可读的数据。

连接和写数据

Socket类的构造函数会尝试根据参数中指定的主机和端口来建立连接,并阻塞等待,直到连接成功建立或发生了系统定义的超时。不过,系统定义的超时时间很长,而Java又没有提供任何缩短它的方法,要改变这个情况,可以使用Socket类的无参数构造函数,它返回一个没有建立连接的Socket实例,需要建立连接时,调用该实例的connect方法,并制定一个远程终端和超时时间(毫秒)。

write方法调用也会阻塞等待,直到最后一个字节成功写入到TCP实现的本地缓存中。如果可用的缓存空间比要写入的数据小,在write方法调用返回前,必须把一些数据成功传输到连接的另一端。因此,write方法阻塞总时间最终还是取决于接收端的应用程序。Java现在还没有提供任何方法使write超时或由其他方法打断的方法。一个可以在Socket实例上发送大量数据的协议可能会无限期地阻塞下去。

public class TimelimitEchoProtocol implements Runnable {
    private static final int BUFSIZE = 32;  // Size (bytes) buffer
    private static final String TIMELIMIT = "10000";  // Default limit (ms)
    private static final String TIMELIMITPROP = "Timelimit";  // Thread property
    private static int timelimit;
    private Socket clntSock;
    private Logger logger;
    public TimelimitEchoProtocol(Socket clntSock, Logger logger) {
        this.clntSock = clntSock;
        this.logger = logger;
        // Get the time limit from the System properties or take the default
        timelimit = Integer.parseInt(System.getProperty(TIMELIMITPROP,TIMELIMIT));
    }
    public static void handleEchoClient(Socket clntSock, Logger logger) {
        try {
            // Get the input and output I/O streams from socket
            InputStream in = clntSock.getInputStream();
            OutputStream out = clntSock.getOutputStream();
            int recvMsgSize;                        // Size of received message
            int totalBytesEchoed = 0;               // Bytes received from client
            byte[] echoBuffer = new byte[BUFSIZE];  // Receive buffer
            long endTime = System.currentTimeMillis() + timelimit;
            int timeBoundMillis = timelimit;
            clntSock.setSoTimeout(timeBoundMillis);
            // Receive until client closes connection, indicated by -1
            while ((timeBoundMillis > 0) &&     // catch zero values
                    ((recvMsgSize = in.read(echoBuffer)) != -1)) {
                out.write(echoBuffer, 0, recvMsgSize);
                totalBytesEchoed += recvMsgSize;
                timeBoundMillis = (int) (endTime - System.currentTimeMillis()) ;
                clntSock.setSoTimeout(timeBoundMillis);
            }
            logger.info("Client " + clntSock.getRemoteSocketAddress() +
                    ", echoed " + totalBytesEchoed + " bytes.");
        } catch (IOException ex) {
            logger.log(Level.WARNING, "Exception in echo protocol", ex);
        }
    }
    public void run() {
        handleEchoClient(this.clntSock, this.logger);
    }
}

试图将总时间限制在10秒内,每次read调用结束后将重新计算剩余的timeout。

关闭套接字

网络协议通常会明确指定了由谁来发起关闭连接。

对于HTTP协议,由于客户端不知道文件大小,因此是由服务器端发起关闭套接字来指示文件的结束。

调用Socket的close方法将同时终止两个方向的数据流,一旦一个终端(客户端或服务端)关闭了套接字,它将无法再发送或接收数据。这就是意味着close方法只能够在调用者完成通信之后用来给另一端发送信号。

Socket类的shutdownInput和shutdownOutput方法能够将输入输出流相互独立地关闭,关闭之后,任何没有发送的数据都将毫无提示地被丢弃,任何想从套接字的输入流读取数据的操作都将返回-1。同理,关闭之后,任何尝试向输出流写数据的操作都将抛出一个IOException异常。

在客户端调用了shutdownOutput之后,它还要从服务器读取剩余的已经压缩的字节

public class CompressClient {
    public static final int BUFSIZE = 256;  // Size of read buffer
    public static void main(String[] args) throws IOException {
        if (args.length != 3) { // Test for correct # of args
            throw new IllegalArgumentException("Parameter(s): <Server> <Port> <File>");
        }
        String server = args[0];               // Server name or IP address
        int port = Integer.parseInt(args[1]);  // Server port
        String filename = args[2];             // File to read data from
        // Open input and output file (named input.gz)
        FileInputStream fileIn = new FileInputStream(filename);
        FileOutputStream fileOut = new FileOutputStream(filename + ".gz");
        // Create socket connected to server on specified port
        Socket sock = new Socket(server, port);
        // Send uncompressed byte stream to server
        sendBytes(sock, fileIn);
        // Receive compressed byte stream from server
        InputStream sockIn = sock.getInputStream();
        int bytesRead;                      // Number of bytes read
        byte[] buffer = new byte[BUFSIZE];  // Byte buffer
        while ((bytesRead = sockIn.read(buffer)) != -1) {
            fileOut.write(buffer, 0, bytesRead);
            System.out.print("R");   // Reading progress indicator
        }
        System.out.println();      // End progress indicator line
        sock.close();     // Close the socket and its streams
        fileIn.close();   // Close file streams
        fileOut.close();
    }
    private static void sendBytes(Socket sock, InputStream fileIn)
            throws IOException {
        OutputStream sockOut = sock.getOutputStream();
        int bytesRead;                      // Number of bytes read
        byte[] buffer = new byte[BUFSIZE];  // Byte buffer
        while ((bytesRead = fileIn.read(buffer)) != -1) {
            sockOut.write(buffer, 0, bytesRead);
            System.out.print("W");   // Writing progress indicator
        }
        sock.shutdownOutput();     // Finished sending
    }
}

基于GZIP压缩算法的压缩协议

public class CompressProtocol implements Runnable {
    public static final int BUFSIZE = 1024;   // Size of receive buffer
    private Socket clntSock;
    private Logger logger;
    public CompressProtocol(Socket clntSock, Logger logger) {
        this.clntSock = clntSock;
        this.logger = logger;
    }
    public static void handleCompressClient(Socket clntSock, Logger logger) {
        try {
            // Get the input and output streams from socket
            InputStream in = clntSock.getInputStream();
            GZIPOutputStream out = new GZIPOutputStream(clntSock.getOutputStream());
            byte[] buffer = new byte[BUFSIZE];   // Allocate read/write buffer
            int bytesRead;                       // Number of bytes read
            // Receive until client closes connection, indicated by -1 return
            while ((bytesRead = in.read(buffer)) != -1)
                out.write(buffer, 0, bytesRead);
            out.finish();      // Flush bytes from GZIPOutputStream
            logger.info("Client " + clntSock.getRemoteSocketAddress() + " finished");
        } catch (IOException ex) {
            logger.log(Level.WARNING, "Exception in echo protocol", ex);
        }
        try {  // Close socket
            clntSock.close();
        } catch (IOException e) {
            logger.info("Exception = " +  e.getMessage());
        }
    }
    public void run() {
        handleCompressClient(this.clntSock, this.logger);
    }
}