バイオブロッキング

Javaネットワークプログラミングは、BIO、NIO、AIO 3に大別できます。

BIOの最初のタイプは従来のioストリームに基づいており、ブロッキングタイプです。

これはクライアントです

public class Client { 
    public static void main(String [] args){ 
        Scanner se = new Scanner(System.in); 
        System.out.println( "Please enter the client number:"); 
        int code = se.nextInt() ; 
        System.out.println( "Client number is:" + code); 
        Socket socket = null; 

        try 
        { 
            System.out.println( "Client" + code + "Start connection to the server ..."); 
            socket = new Socket( "127.0.0.1"、8888); 
            if(socket!= Null){ 
                System.out.println( "Client" + code + "Connect to the server successful!"); 
            } 
            OutputStream out = socket.getOutputStream(); 
            while (true){
 
                System.out。println( "クライアント" +コード+ "送信するコンテンツを入力してください:");
               文字列str = se.next(); 
               if(str.trim()。equalsIgnoreCase( "quit"))
                   break; 
               out.write((code + "--->" + str).getBytes( "utf-8")); 
            } 
            out.close(); 
            socket.close(); 


        } catch(Exception ex){ 
            ex.printStackTrace(); 
        } 

    }

  これはサーバーです

public class Server { 
    private static int port = 8888; 
    public static void main(String [] args)throws IOException { 

        ServerSocket serverSocket = null; 
                try { 
                    serverSocket = new ServerSocket(port); 
                    System.out.println( "The server start listen to : "+ Port); 
                    while(true){ 
                     var clientSocket = serverSocket.accept(); //最初のブロック
                        System.out.println("クライアント要求を受け入れる "); 
                        InputStream in = clientSocket.getInputStream(); 
                        int hasread = 0; 
                        バイト[] bufer =新しいバイト[1024];
                        while((hasread = in.read(bufer))!=-1){//第二
                            处阻塞システム.out.println( "客户端输入字符:" + new String(bufer、0、hasread)); 
                        } 
                        clientSocket.close()。
                        in.close(); 
                    } 

                } 
                catch(例外ex){ 
                    ex.printStackTrace(); 
                }最終的に{ 
                    try { 
                        serverSocket.close(); 
                    } catch(IOException e){ 
                        e.printStackTrace(); 
                    } 

                } 
    }

  サーバーは、クライアントをリッスンし、クライアントを受信するときにブロックしています。

サーバーを個別に実行する

 

 リスニングメソッドでプログラムがブロックされています

クライアントを起動する

 

 成功したサーバー接続を表示

 

 この時点で、サーバーはクライアントが入力したテキストの読み取りをブロックされます

 

 この時点で通信は成功していますが、プログラムは連続ループ待機中です。

現在、クライアントは稼働しており、コンテンツを送信しています

 

 

 サーバーは、クライアント1がコンテンツを送信するのを待つのをブロックし、この時点でクライアント1をシャットダウンするため、コンテンツを受信できません。

 

 

 

 サーバーはクライアント2から情報を受信します。

したがって、複数のクライアントが同時に情報を送信できるようにしたい場合、このBIOはブロックされます。複数のスレッドが必要です。

 

おすすめ

転載: www.cnblogs.com/tomato190/p/12675202.html