Netty学习之路(1)

BIO演示
服务器端:

package com.netease.bio_1;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
        public static void main(String[] args) {
            ServerSocket ss = null;
            try {
                 ss = new ServerSocket(8888);
                 System.out.println("服務器已啟動:");
                while(true){
              Socket socket =   ss.accept();
                 new Thread(new ServerHandle(socket)).start();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

}

处理类:

package com.netease.bio_1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerHandle  implements Runnable{
  private Socket socket = null;
  public ServerHandle(Socket socket){
      this.socket = socket;
  }
public void run()   {
    BufferedReader br = null;
    PrintWriter pw = null;

      try {
         br = new BufferedReader( new InputStreamReader(socket.getInputStream()));
         pw =  new PrintWriter(socket.getOutputStream(),true);
            String str = null;
        while(true){
            str=br.readLine();
            if(str==null)
                break;
            System.out.println(str);

            pw.println("server has gotten the content");
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{

            try {
                if(br!=null)
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(pw!=null)
                pw.close();

            try {
                if(socket!=null)
                    socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}


}

客户端:

package com.netease.bio_1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
  public static void main(String[] args) {
      PrintWriter pw = null;
      Socket s  = null;
      BufferedReader br  = null;
      try {
         s = new Socket("127.0.0.1",8888);
         pw = new PrintWriter(s.getOutputStream(),true);
         br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        pw.println("client send a message 22");
         String respon = null;
         respon=br.readLine();
             System.out.println(" Server return value to Client:"+respon);
      } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        if(pw!=null)
            pw.close();

            try {   if(br!=null)
                br.close();
                if(s != null)
                    s.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
}
}

description:
1、传统bio形式,每个客户端的接入,服务器端创建一个独立的线程去处理请求。
disadvantages:
并发量大的时候,服务器压力较大

猜你喜欢

转载自blog.csdn.net/iphone4grf/article/details/50829039