(i) TCP programming
(1) Development Steps of TCP (Connection channel) programming client
1)创建客户端的Socket对象Socket:就是客户端的Socket 构造方法 public Socket(InetAddress address, int port) public Socket(String host, int port):创建客户端套接字对象,并且指定端口号和ip文本形式
2) get the output stream object within the channel
3) write data to server side
4) Releasing resources
Java.net.ConnectException:Connection Refused:connect Connection denied
Do not run the client first, the client connection needs to be heard by the server to connect
5) method
- Public socket (String host, int port) to create the client socket object
- Public OutputStream Getoutputstream (): Gets the output stream of the socket
(2) The development steps of the service side:
- 1) Create a server-side socket object
- 2) Monitor the client connection (blocking method)
- 3) get the input stream within the channel
- 4) Read the data and display the console
- 5) Releasing Resources
- Java.net.BindException:Address already in Use:jvm_bind address is bound because there are already servers listening for client connections
6) method
- public serversocket (int port) throws IOException create a server socket bound to a specific port
Listening for client connections (blocking method)
- Public Socket Accept (): throws IOException listens for and accepts connections to this socket. This method blocks until the connection is passed in
- InputStream getInputStream () gets the input stream within the channel
- Get IP text form ': Public inetaddress getinetaddress ()
- Service side cannot be closed
Example: The client reads a text file under the current project, and the server copies the contents of the file to output a new text file (Copy.java)
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.net.Socket;
Import java.net.UnknownHostException;
The client reads a text file under the current project, and the server copies the contents of the file to output a new text file (Copy.java)
/**
- According to the normal thinking, joined the server-side feedback operation, found the server-side feedback data, the client did not receive, the client did not end, and then wait for the server to send
- On the client side, if Senddemo.java is finished, the server side does not know whether or not to read (the stream is not allowed to use NULL as the information end condition, but the client text file can be null as the information end condition)
- The server waits for the client notification text to be read, and the client needs server-side feedback
- Solution:
- 1) client side, write an end condition,----> server if the end condition is read, the server can feedback
- The first way though can solve this problem, not good, not flexible!
-
2) public void Shutdownoutput (): Notifies the server side (I have no data, you hurriedly feedback)
-
*/
public class Uploadclient {
public static void Main (string[] args) throws IOException {
//创建客户端的Socket对象Socket s = new Socket("192.168.10.1",11111); //当前对象创建之后,就会和服务器端建立连接通道//封装文本文件BufferedReader br = new BufferedReader( new FileReader("SendDemo.java")) ;//封装通道内的流BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())) ;//读写操作String line =null;while((line=br.readLine())!=null) { bw.write(line); bw.newLine(); bw.flush();}//自定义的结束条件/*bw.write("over");bw.newLine();bw.flush();*///通知服务器端,客户端的文件已经完了,赶紧给反馈//public void shutdownOutput():s.shutdownOutput(); //获取服务器反馈的数据//获取通道内的字符输入流BufferedReader brClient = new BufferedReader( new InputStreamReader(s.getInputStream())) ;String client = brClient.readLine() ;System.out.println(client);//释放资源br.close();s.close();
}
}
Service side:
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
The client copies the data from the text file to the server-side output of the new Java file, and then the server-side feeds back the data to the client, and the client presents the feedback data
public class Uploadserver {
public static void main(String[] args) throws IOException { //服务器端的Socket对象 ServerSocket ss = new ServerSocket(11111) ; //监听 Socket s = ss.accept() ;//阻塞 //封装通道内的流 BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream())) ; //封装最终要输出的文本文件 BufferedWriter bw= new BufferedWriter(new FileWriter("Copy.java")) ; String line = null ; while((line=br.readLine())!=null) { //阻塞 //通道内的流结束不是用null作为结束条件的 bw.write(line); bw.newLine(); bw.flush(); } //服务端获取通道内的输出流对象 BufferedWriter bwServer = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())) ; //向客户端反馈 bwServer.write("文件上传成功了"); bwServer.newLine(); bwServer.flush(); //释放资源 bw.close(); s.close();}
}
(ii) Reflection
(1) What is reflection:
- Reflection is the class object that is/class by acquiring a class byte-code file object.
- Gets the member variables, constructor methods, and member methods in the bytecode file object
- Field: The Short member variable (field provides information about a single field of a class or interface, as well as its dynamic access rights.)
- The reflected field may be a class (static) field or an instance field. )
Constructor: Constructor Method: Provides information about a single construction method of a class and access to it.
Method: A member approach: information about a single method on a class or interface (and how to access the method)
(2) Get bytecode files
Parameters: Classpath: Package name. class Name
Class C = Class.forName ("Classpath: Package name. class name");
(3) method
- Public constructor<?>[] GetConstructors (): All common constructor methods for classes represented by class objects
- Public constructor<?>[] Getdeclaredconstructors (): Gets all the constructor methods in the current bytecode file object
- How to get a single construction method
- Public constructor<t> getconstructor (class<?> ... parametertypes)
If there is no argument, do not write; if it is, the parameter in the Getcontstructor method is the static property class of the current parameter type.
- Gets the object that is currently constructed; Public T newinstance (Object ... initargs): Initrgs: Actual parameters
- The construction method obtained now is private: public constructor<t> getdeclaredconstructor (class<?> ... parametertypes)
- public void Setaccessible (Boolean flag): A value of TRUE indicates that the reflected object should be removed from the Java language access check when it is used
Exception: Java.lang.IllegalAccessException:Class Org.westos_02.ReflectDemo3 can not access a member of Class Org.westos_01.Per Son with modifiers ' private at sun.reflect.Reflection.ensureMemberAccess (reflection.java:102)
- Public field[] Getdeclaredfields (): Gets all the fields in the current bytecode file object
- Public field[] GetFields (): Gets a common field
- public void Set (Object obj,object value)
Parameter 1: Indicates to which object sets the value of the current field
Parameter 2: Sets a new value for the field that is currently getting the fields object
Java.lang.NoSuchFieldException:name
Name is private and the method must be specified to get
- public field Getdeclaredfield (String name): The specified field
- Gets all the public member methods in the current bytecode file object (including the parent class): Publicly method[] GetMethods ()
- Public method[] Getdeclaredmethods (): All Member Methods
- Gets a single member method,
Public Method GetMethod (String name, Class<?> .... parametertypes)
Parameter 1: Indicates the method name
Parameter 2: Static Class Property representing the parameter data type of the current method
- public object invoke (Object obj, object ... args)
Parameter 1: the underlying invoke () is called on the specified object,
Parameter 2: Represents the actual arguments passed by the call to that method
Java Basics (11)