The advance of Jave -- Socket, Class Loading, Reflect, AOP(Day06)
来源:互联网 发布:中国对外贸易数据分析 编辑:程序博客网 时间:2024/06/06 07:30
1. Socket: jave uses object of socket as the channel between the Server and Client. when they connected successfully, sockets would be created in the two sides so that finish a session.
①UDP: none-connection oriented, insecurity, fast.
②TCP:connection-oriented, security, slow.
③ServerSocket: using in Server to listen the connection requirement from Client, if there is no connection, it will keep waiting state.
public class Demo02 {public static void main(String[] args) throws UnknownHostException, IOException {Socket s = new Socket("192.168.5.205",3306);InetAddress i = s.getInetAddress();System.out.println(i);System.out.println(i.getCanonicalHostName());System.out.println(i.getHostName());}}
④ TCP communication:
Server:
public class Server1 {public static void main(String[] args) throws IOException {ServerSocket s = new ServerSocket(8888);//server portSystem.out.println("monitoring...");Socket s1 =s.accept();//a blocking method, using to set connection with Client.System.out.println("monitored success!");BufferedReader b = new BufferedReader(new InputStreamReader(s1.getInputStream()));//Gain input stream, and then the byte stream convert System.out.println(b.readLine()); //into character stream, finally we use BufferedReader.PrintWriter w = new PrintWriter(s1.getOutputStream());//Gain the output stream.w.println("Hello Client");//send message to Client.w.flush();}}Client:
public static void main(String[] args) throws UnknownHostException, IOException {Socket s = new Socket("localhost",8888);PrintWriter w = new PrintWriter(s.getOutputStream());w.println("Hello Server");w.flush();//sometimes, the message is stored in buffer rather than in memory.BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream()));System.out.println(b.readLine());}⑤close():
Closing resources.
⑥One for all: (Thread & Concurrency)
Server:
public class Server2 {public static void main(String[] args) throws IOException {ServerSocket s = new ServerSocket(8888);System.out.println("monitoring...");//Thread t = new Thread();ExecutorService e = Executors.newFixedThreadPool(3);while (true) {Socket s1 = s.accept();System.out.println("monitored success!");Runnable r = new MyRunnable(s1);e.execute(r);}}}Client:
public static void main(String[] args) throws UnknownHostException, IOException {Socket s = new Socket("localhost",8888);BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream()));PrintWriter w = new PrintWriter(s.getOutputStream());Scanner scan = new Scanner(System.in);while(true){w.println(Thread.currentThread().getName() +" say: "+scan.nextLine());w.flush();System.out.println(b.readLine());}}MyRunnable:
public class MyRunnable implements Runnable {private Socket s1;public MyRunnable(Socket s1){this.s1 = s1;}@Overridepublic void run() {{try {BufferedReader b = new BufferedReader(new InputStreamReader(s1.getInputStream()));PrintWriter w = new PrintWriter(s1.getOutputStream());Scanner scan = new Scanner(System.in);while (true) {System.out.println(b.readLine());w.println(Thread.currentThread().getName() +" say: "+ scan.nextLine());w.flush();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}⑦Conversely, UDP do not have virtual channel between Server and Client. In this way, Server's socket only receive message, and Client's socket send message.
Server:
public class UDPServer {public static void main(String[] args) {DatagramSocket s = null;try {s = new DatagramSocket(8888);byte[] data = new byte[1024];DatagramPacket p = new DatagramPacket(data, data.length);s.receive(p);String str = new String(p.getData(),0,p.getLength());System.out.println(str);} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(s != null){s.close();}}}}Client:
public class UDPClient {public static void main(String[] args) {DatagramSocket s = null;try {s = new DatagramSocket();byte[] data = "Hello, Server!".getBytes();DatagramPacket p = new DatagramPacket(data,data.length,InetAddress.getByName("localhost"),8888);s.send(p);} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{if(s != null){s.close();}}}}
①Class Loading: Class Loading means that when we read a class into memory, it will forwardly create a java.lang.class object.
Class Connection: Class Connection will merge the binary of class with JRE.
Class Initialization: it will initialize some static block and static attributes.
②Three loader:
(1)root class loader: loading core of java class, such as String, System, etc.
(2)extensions class loader: loading classes in the extension directory of jar in jar package.
(3)system class loader: loading the jar package and class path designated by CLASSPATH
3.Reflect:
public class Demo03 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {Dog d = new Dog("H",12);//Three ways to get object.Class a = d.getClass();//FirstSystem.out.println(a);Class c = Class.forName("com.bsr.day1217.Dog");//SecondSystem.out.println(c);Class e = Dog.class;//ThirdSystem.out.println(e);Constructor[] b = a.getConstructors();//gain a array of constructorsfor (Constructor constructor : b) {System.out.println(constructor);}Constructor f = a.getConstructor(String.class, int.class);//gain a constructorSystem.out.println(f);f = a.getDeclaredConstructor(int.class);//this method can gain a private constructorSystem.out.println(f);Method[] m = a.getDeclaredMethods();//gain a array of methodsfor (Method method : m) {System.out.println(method.getName());}Method m1 = a.getDeclaredMethod("toString", null);//gain a method.System.out.println(m1);}}
public class Demo04 {public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {Class<Dog> c = Dog.class;Dog d1 = new Dog("H",12);Dog d2 = c.newInstance();System.out.println(d2);Constructor<Dog> con = c.getConstructor(String.class, int.class);Dog d3 = con.newInstance("H",3);System.out.println(d3);con = c.getDeclaredConstructor(int.class);con.setAccessible(true);Dog d4 = con.newInstance(3);System.out.println(d4);Method m = c.getMethod("eat", String.class);m.invoke(d3,"H");}}
4. AOP:
AOP can implement a technique to add functionality to a program dynamically without modifying the source code by precompiling and running time dynamic proxies.
- The advance of Jave -- Socket, Class Loading, Reflect, AOP(Day06)
- The Knowledge of Linux-.-Day06
- The advance of JAVA -- Regex, Set(Day02)
- The advance of Java -- IO, Thread(Day05)
- The advance of Java -- Html, Servlet(Day07)
- The advance of Java -- Servlet, Jsp(Day08)
- Internals of Java Class Loading
- Internals of Java Class Loading
- Internals of Java Class Loading
- The advance of Java -- JFC(Java foundation classes library)(Day01)
- The advance of Java -- Set, LinkedList, Map(Day03)
- The advance of Java -- Genericity, Exception, IO(Day04)
- Optimizing the loading of AutoCAD .NET applications
- Jave Bean 和 Java Class
- ioc,,aop,,java-reflect
- The class hierarchy of Gears
- The Meaning of Class Definitions
- The Methods of class Object
- CCF CSP真题——201709-2 公共钥匙盒
- ARM的37个寄存器详解
- CyberLink PhotoDirector Ultra(相片大师9)官方中文版V9.0.2406.0下载 | photodirector 9破解版
- 网站的高性能架构-WEB前端性能优化
- IT风投的优势
- The advance of Jave -- Socket, Class Loading, Reflect, AOP(Day06)
- 道德焦虑与利他动机
- get方法/post方法应用拦截器
- 网站的高性能架构-应用服务器性能优化
- HDOJ1994 利息计算
- 如何用Java实现智能对话机器人
- 软件需求工程--需求分析
- 网站的高性能架构-存储性能优化
- BP算法(python)