【TCP】s.shutdownOutput();这行代码的牛逼之处

运行下面代码,客户端和服务器端都处于僵持等待状态

UTP客户端

package com.xiaozhi.jingtian.tcp;  

import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.io.OutputStreamWriter;  
import java.net.Socket;  

public class UploadClient {  

    public static void main(String[] args) throws Exception, IOException {  
        //创建客户端的Socket服务,指定目的主机和端口号  
        Socket socket=new Socket("121.251.158.251",10002);  
          
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream("c://1.txt")));   
        BufferedReader socket_bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter socket_bufferedWriter= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  
          
        String line=null;  
        while((line=bufferedReader.readLine())!=null){  
            socket_bufferedWriter.write(line);  
            socket_bufferedWriter.newLine();  
            socket_bufferedWriter.flush();  
        }  
      
        System.out.println(socket_bufferedReader.readLine());
        
        bufferedReader.close(); 
        socket_bufferedReader.close();
        socket_bufferedWriter.close();  
        socket.close();  
    }  
}  
UTP服务器代码:
package com.xiaozhi.jingtian.tcp;  

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class UploadServer {  

    public static void main(String[] args) throws Exception {  
        //创建服务端的Socket服务,指定端口号  
        ServerSocket serverSocket=new ServerSocket(10002);  
          
        while(true){  
            //通过accept方法获取连接过来的Socket对象  
            Socket socket=serverSocket.accept();  
            new MyThread(socket).start();  
        }  
          
    }  
}  

class MyThread extends Thread{  
    private Socket socket;  
      
    public MyThread(Socket socket) {  
        this.socket = socket;  
    }  

    @Override  
    public void run() {  
        try {  
            System.out.println(socket.getInetAddress().getHostAddress()+"上传文件");  
            BufferedReader socket_bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));  
            BufferedWriter socket_bufferedWriter= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  
            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c://"+System.currentTimeMillis()+".txt")));  
              
            String line=null;  
            while((line=socket_bufferedReader.readLine())!= null){  
                bufferedWriter.write(line);  
                bufferedWriter.newLine();  
                bufferedWriter.flush();  
            }  
             
            socket_bufferedWriter.write("上传成功!");
            socket_bufferedWriter.flush();
            
            socket_bufferedReader.close();  
            socket_bufferedWriter.close();
            bufferedWriter.close();  
            socket.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

因为客户端readLine方法读到-1,就结束了。客户端并没有将-1发送给服务器。服务器因为没有结束标志-1,所以服务器端readLine一直处于等待状态。

而客户端需要服务器端返回数据 上传成功的字样,所以客户端第二个readLIne方法也处于等待状态。

通过加结束标志的方法,来解决这个问题

第一种方法close流,相当于给流中加入一个结束标记-1

第二种方法:s.shutdownOutput();//关闭客户端的输出流。相当于给流中加入一个结束标记-1.这个样子服务器的输入流的reaLine方法就会读到一个-1,然后结束readLIne方法。也可以这样理解s.shutdownOutput();关闭的是客户端的输出流,同时服务器端的输入流也随之关闭。s.shutdownIntput();也是同样的道理,关闭客户端的输入流,同时服务器端的输出流也随之关闭。


你可能感兴趣的:(闲着没事—玩玩Java)