六、socket.shutdownOutput()问题

客户端:

package cn.kgc.test2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClass {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 8800);
            OutputStream os = socket.getOutputStream();
            String info = "请求服务";
            os.write(info.getBytes());
            socket.shutdownOutput();
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String reply = null;
            while ((reply = br.readLine()) != null) {
                System.out.println(reply);
            }
            br.close();
            is.close();
            os.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

}

服务器:

package cn.kgc.test2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketClass {
    public static void main(String[] args) {
        ServerSocket serverSocket;
        try {
            serverSocket = new ServerSocket(8800);
            Socket socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info = null;
            while ((info = br.readLine()) != null) {
                System.out.println(info);
            }
            String reply = "朕知道了";
            OutputStream os = socket.getOutputStream();
            os.write(reply.getBytes());
            os.close();
            br.close();
            is.close();
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        
    }

}

问题:如果在客户端没有:

socket.shutdownOutput()

这句话的话,什么都不会输出。

你可能感兴趣的:(六、socket.shutdownOutput()问题)