This paper is helpful to java programmers who write client server applications, and can solve the problem that the programs will continue to run stably when the other party fails. At present, the java platform has been widely used in all kinds of clients/ In the actual programming of the server system, asynchronous processing of the network is often needed, such as the client program. If the client program runs before the service program, the client program needs to automatically connect the service program after the service program is started. If the service program stops in the middle of the client program, it also needs to wait for the service program to run and reconnect without stopping. The following provides a kind of asynchronous programming method
Network asynchronous application involves the following. Several key points
Check whether the service application exists after the client application starts. If it does not exist, wait for the service application to start without blocking the execution of other tasks of the client application. Once the service application starts, the client application and the service application should establish a connection with it in time. After the service application abnormally exits in data communication, the client application should be able to detect the exit of the service application and the client application will automatically clear the communication link. Return to the initial state and wait for the service application to restart
The asynchronous programming of the network first involves a timer and timer events. The timer is used to continuously detect whether the client application and the service application are connected in the network and suspend data communication when the service application is abnormal. The fault of the network returning to the initial state can be known through the exception handling of the network method.
The timer is included in the network communication class, so that the applications using this class can use it. Handle network information conveniently without perceiving the existence of a timer
The client program class has the following structure: public class NetComm? implements ActionListener{? javax swing Timer timer = new javax swing Timer( this); ? Socket sock; private EventNotifier en; public static int net_state = ; InetAddress ServerAddr; int ServerPort; ? public NetComm(InetAddress addr int port){? ServerAddr = addr; ? ServerPort? = port; }
public void NetComm_Init() {? net_state = ; ? try {? sock = new Socket(ServerAddr ServerPort); ? } catch (IOException e) {? net_state = ; ? }? timer start(); }? public void NetComm_Data(){? try {? OutputStream outputstream = sock getOutputStream(); BufferedWriter out = new BufferedWriter? (new OutputStreamWriter(outputstream)); out write( java by ghf@china ); ? out flush();
? BufferedReader in = new BufferedReader? (new InputStreamReader(sock getInputStream())); boolean more = true; ? while(more) {? String str = in readLine(); ? if(str == null) more = false; ? else? //Processing data? System out println(str); ? } in close();
? } catch (IOException e) {? NetComm_Close(); ? net_state = ; ? }? timer start(); ? } public void NetComm_Close()? {? if(sock ! = null)? try{? sock close(); ? } catch ( IOException e) {? }? } public void actionPerformed(ActionEvent e)? {? if(net_state == )? NetComm_Init(); ? else? NetComm_Data(); ? }}
In the above program, a callback function can also be provided for the external application to inform the application service application that the network communication class is similar when the network is abnormal or returns to normal, and it can be placed in the same class: lishixinzhi/article/program/Java/HX/21311/27252.