1 Java Enter / Outflow Concept:
Filtering DataInputStream and DataOutputStream also implements interface DataInput and DataOutput, in addition to subcaters of FilterInputStream and FilterOutputStream, respectively. The method defined in interface DataInput mainly includes reading basic types of data from the stream, reading a row of data, or reading the number of bytes of the specified length, such as readboolean () readint (), readline (), readfully (), etc.. The method defined in interface DataOutput is mainly written to the basic type of data or writes an array of bytes written to a certain length, such as WriteChar (), writeouble () DataInputStream can read from the connected input stream. Basic type data is configured to implement an input method independent of a specific platform; DataInputStream can write to the basic type of data to the connected output stream.
2 Socket mechanism
Socket is designed for a client / server model, two programs on the network implement data exchange through a two-way communication connection, and one end of this bidirectional link is called a socket. Socket is usually used to connect to the client and the server. The client can write requests to the Socket, and the server will process this request, and then return the result to the user via Socket.
The Socket communication mechanism provides two ways: there are joints and unpigneous ways, respectively, for different application requirements. When using a coupling method, the communication link provides a reliable, full-duplex byte stream service. In this mode, communication between communication must create a coupling process and establish a communication link. The subsequent network communication operation is performed completely between this pair, and the communication is turned off. When using non-coupling mode, its system overhead is smaller than in connection, but the communication link provides an unreliable datagram service that cannot guarantee that the data transmitted by the source will be able to reach the content. In this manner, communication between communication does not have to create a coupling process and establish a communication link, and network communication operations are forwarded between different hosts and processes.
3 Java language
The advantages of Java language are mainly in: simple, object-oriented, multi-threaded, distributed, architectural neutral, security, etc.
(1) Simply
Java is very similar to the C language, but Java is simpler than C , it abandon some of C is not absolutely necessary, such as header files, preprocessing files, pointers, structures, operators overload, multiple inheritance, and automatic force. Java achieves automatic garbage collection and simplifies the work of memory management. This makes the programming easier while reducing the possibility of error.
(2) Object-oriented
Java provides simple class mechanisms and dynamic architectural models. It encapsulates its status variables and methods in the object, which enables modular and information hidden. Category provides prototypes of objects, through inheritance and overload mechanisms, subclasses can be used or redefined parent class or The method provided by the superclass, thereby achieving both code multiplexing, providing a dynamic solution.
Java is a programming language that completely faces objects. It is an object in addition to other classes outside the three basic data types of arrays, Boolean and characters, which no longer supports global variables. In Java, if you don't create a new class, you cannot create a program, the Java program must create a class instance before running, and then you can submit.
Java also supports inheritance features, Java classes can inherit behavior from other classes, but Java only supports single-weight inheritance of classes, that is, each class can only inherit from one class.
The Java support interface, the interface allows the programmer definition method but does not implement immediately, a class can implement multiple interfaces, using the interface to get many of the advantages of multiple inheritance and there is no multiple inheritance.
(3) Multithreading
Multi-threading makes applications simultaneously different operations simultaneously, handles different events. In multi-threading mechanisms, different threads handle different tasks, they do not interfere between them without interference, and will not be able to implement real-time interaction on the network because they wait for other parts. The Java program can have multiple execution threads, such as making a thread to make a complicated calculation, and another thread interact with the user so that the user can interact with the system without interrupting the calculation thread. Multithreading guarantees high execution efficiency.
(4) Distribution
Java is a network-oriented language. With the class library it provides, the TCP / IP protocol can be handled, and users can easily access other objects on the network through the URL address.
(5) Arrangement of architecture
Java is a network language that enables Java programs to run anywhere in the network, the Java interpreter generates file formats that are unrelated to the architecture. In order to make the structure neutral, Java has developed a fully unified language text, such as the basic data type of Java does not change with the change of the target machine, and a integer is always 32 bits. A long integer is always 64-bit.
In order to make Java applications can not rely on specific systems, the Java locale also provides a package for accessing the base operating system functionality. When the program uses these packages, make sure it can run in a variety of support Java Platform.
Java.lang: General language pack. These include classes for string processing, multi-thread, exception handling, and digital functions, which is the basic package for implementing the Java program running platform.
Java.util: Utility Kit. These include haveh tables, stack, time and date, etc.
Java.io: Input / output package based on stream model. This package uses a unified stream model to implement input / output of various formats, including input / output of file systems, networks, and devices, etc.
Java.net: Network package. This package supports TCP / IP protocol, which provides programming interfaces for Socket, URL, and WWW
Java.awt: Abstract window tool set. Among them, graphical user interface components that can be cross-platform, including windows, menus, scroll bars, and dialogs, etc.
Java.applet: Basic packages supporting applet programming
(6) Safety
For the network, Java in the distribution environment must prevent the virus from intrusion, Java does not support pointers, everything to be implemented through the object's instance variables, which prevents programmers from using spoofing means to access objects Also avoid errors that are prone to pointer operations.
4 Java Tools
(1) JDK
1) Java compiler
The Java compiler compiles the Java source code file into an executable Java bytecode. The extension of the Java source code file is .java, Java compiler compiles this extension file into the file that extends .class. Each class in the source file will generate a Class file after compiling, which means that a Java source code file may compile multiple Class files.
2) Java interpreter
The Java Interpreter supports the running of the executable of the built-generated bytecode format, which is a command line tool that runs a non-graphics Java program.
3) AppletViewer
It is a simple test tool for Java Applet, which uses it to test the Java Applet program without the support of the WWW browser.
(2) Visual J
Visual J integrates visual interface design, interactive debugging, code editing, online help information, and how to quickly master the utility wizards of the development environment, while having the advantage of making it makes full use of new technologies for Active X and COM. It is a rare Java development system using Visual J to create an interactive Internet application.
5 Realization of Client / Server Communication:
(1) APPLICATION communication with applet
The two ends are connected via the Socket mechanism:
1) Client programming process: Open Socket, create a new socket;
Set an input and output stream for a socket;
Writes from the socket according to the server protocol or writes from the socket;
Clear the socket and input / output flow;
2) Server-end programming process:
Open Server Socket, create a server-type socket and a normal socket, the server type socket service requests the port for the client to request the client;
Use the Accept () method of the ServerSocket class to make the server type socket in the listening state and return the listening result to the ordinary socket;
Create input and output flow for the normal socket;
Read or write from the input and output stream to perform the corresponding processing, and return the result to the client;
Close all objects after the client and server work, such as server type socket, ordinary socket, input, and output stream.
It is because the Java system has Socket-based flexible communication mechanism, so its application can freely open and access objects on the network, just like in the local file system.
(2) Communication between Applet:
Communication between Applet uses the getApplet () method of the Applet Context class.
Just join in the program
Applet OneApplet = getAppletContext (). GetApplet ("first"); uses the method in the applet of the first.
In this problem, the communication method is used, because the Applets of the server-side communication include receiving information methods and send information methods, all client applets use the method in which the APPET responsible for the communication, so the applet of the client The applet responsible for communication must communicate.
6 program
// Server-side program S.java is responsible for communicating with client
Import java.io. *;
Import java.net. *;
Import java.lang. *;
Import T2;
Class Threadechler Extends Thread // Create thread
{
T2 Thet2 = New T2 ();
Socket incoming;
Int counter;
ThreadechoHandler (socket I, int C)
{incoming = i;
Counter = C;}
Public void Run ()
{
Try
{
DataInputStream in = New DataInputStream (incoming.getinputStream ());
DataOutputStream out = new dataputstream (incoming.getoutputstream ());
System.out.Println ("Hello");
Boolean done = false;
While (! done)
{String aa = ""
String str = in.readutf (); // get a string from the client
/ / Add a respective service program here
System.out.println (STR);
Thet2.pass (str); // decoding
Thet2.tongji (); // Modify the information in the monitoring library
AA = Thet2.guan (); // Manipulate the database
System.out.println ("String Z is:" AA);
IF (Aa.comPareto ("NULL")! = 0)
/ / If you query the database, return the result after the query
{// If you are not querying the database, do not output information to the client
Out.writeutf (aa);
Out.flush ();
} // while
incoming.close (); // Thread Close
} // Try
Catch (IOException E)
{System.out.println (e);
} // end run
}
// ----------------------------------------
Class S
{
Public static void main (string [] args)
{
INT i = 1;
Try
{
Serversocket S = New ServerSocket (1111);
For (;;)
{
Socket incoming = S.Accept ();
System.out.println ("Connect:" i);
New ThreadechoHandler (incoming, i) .start ();
i ;
}
}
Catch (Exception E)
{System.out.println (e);
}
}
// Client Communication Applications Echo.java
Import java.io. *;
Import java.net. *;
Import java.awt. *;
Import java.applet. *;
Public Class Echo Extends Applet
{
Textarea TA;
Socket eChosocket;
Dataoutputstream OS;
DataInputStream IS;
String line;
Public void init ()
{
SetBackground (Color.White);
Ta = new textarea (5, 80);
Ta.setedItable (false);
Add (ta);
Try
{Echosocket = New Socket ("10.102.4.41", 1111);} // Establish a connection with the server
Catch (IOException E)
{System.out.println ("Error");
}
Public Void ST (String Stri) // Method for Sending Strings
{
Try
{DataOutputStream Os = New DataOutputStream (echosocket.getOutputStream ());
DataInputStream IS = New DataInputStream (Echosocket.getInputStream ());
Os.writeutf (" stri); // Convection String to the server
Os.flush ();
}
Catch (IOException E)
{System.out.println ("Error:" E);
}
Public String ST1 () / / Receive String Method
{
String line = "";
Try
{DataOutputStream Os = New DataOutputStream (echosocket.getOutputStream ());
DataInputStream IS = New DataInputStream (Echosocket.getInputStream ());
Line = is.readutf (); // Information read from the server
TA.AppendText (" line); // Output information in text domain
}
Catch (IOException E)
{System.out.println ("Error:" E);} Return Line;
}
}
7 Program debugging experience:
1) When establishing a socket connection, the port numbers at both ends must be consistent, otherwise the connection cannot be established. The server side must have a host IP address or hostname parameter.
2) The input and output stream should be determined after the connection is established. In the initial procedure, DataInputStream and PrintStream, the result can only be transmitted in English, generate garbled when transmitting Chinese, change the printstream to DataOutputStream, and use the readutf () and Writeutf () methods, the Chinese transmission problem is resolved.
3) If a program with a port is not closed, another program cannot use this port.
4) Programs that start communicating are Application, because the client's Application should be changed to Applets because it does not comply with the client / server mechanism. The main steps of its transformation are as follows:
Create an HTML file containing the Applet tag;
Remove the main () method in the application;
The class name should inherit the Applet class, not the Frame class, and join in the beginning of the program
Import java.applet. *; statement;
方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法; 方法 方法 方法;;;
If the APPLICATION uses the BorderLayout Layout Manager, you should reset it in the applet's init () method;
If there is a setTitle () method in Application, you must remove it, such as using the menu in Application, replace it in the applet.
5) Know how to refer to methods and variables in a program in a program, add the import class name at the beginning of the program; add the class name instance = new class name (); then use
Examples. Method (), instance. Variables.