The advance of Jave -- Socket, Class Loading, Reflect, AOP(Day06)
2017-12-17 19:56
344 查看
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.
④ TCP communication:
Server:
Closing resources.
⑥One for all: (Thread & Concurrency)
Server:
Server:
2.
①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:
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.
①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 port System.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; } @Override public 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 block e.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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.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 block e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(s != null){ s.close(); } } } }
2.
①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();//First System.out.println(a); Class c = Class.forName("com.bsr.day1217.Dog");//Second System.out.println(c); Class e = Dog.class;//Third System.out.println(e); Constructor[] b = a.getConstructors();//gain a array of constructors for (Constructor constructor : b) { System.out.println(constructor); } Constructor f = a.getConstructor(String.class, int.class);//gain a constructor System.out.println(f); f = a.getDeclaredConstructor(int.class);//this method can gain a private constructor System.out.println(f); Method[] m = a.getDeclaredMethods();//gain a array of methods for (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.clas 4000 s); 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 serializable class Myplayer does not declare a static final serialVersionUID field of type long
- Following extensions directory contents added to the end of the classpath
- hibernate 中 Provided id of the wrong type for class 异常
- CXF ws client, dynamic endpoint and loading WSDL from the classpath
- 在Spring中注册组件时报错:The fully qualified name of the bean's class, except if it serves only as a parent d
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- Question 48: In C++, which of the following statements accurately describe a base class destructor calling a virtual function ov
- The color chooser class, you could slide the scroll bars to get the specific numbers of the color
- Andrew NG 《machine learning》week 3,class5 —Solving the Problem of Overfitting
- Class Hierarchies(Chapter 15 of The C++ Programming Language)
- 继承一个类的时候报错:The hierarchy of the type 'Class name' is inconsistent
- Provided id of the wrong type for class pojo.Books. Expected: class java.lang.Integer, got class java.lang.Long
- Why is the size of an empty class not zero?
- How to inspect who is caller of func and who is the class of instance
- jaxb异常 Class has two properties of the same name username
- 关于问题The fully qualified name of the bean's class, except if it serves only as a parent definition fo
- Understanding the Java Classloading Mechanism
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock
- The 6th Class of Oral English
- Thread-Safety with the AutoResetEvent, ManualResetEvent Class(Synchronization of .net)