Asynchronous network programming in Java

zhaozj2021-02-17  40

Asynchronous network programming in Java

[Author: Guo Hongfeng Add Time: 2001-9-10 20:53:32]

Programmer helps

Guo Hongfeng (GHF_EMAI@china.com)

This article helps the Java programmers written by the client server, can resolve the program to continue to run stable when the other party has failed.

At present, the Java platform has been widely used in various customer / server systems, and in actual programming, the network is often required to process asynchronous processing. For example, if the customer program runs first in the service, the client will need to automatically connect the service program after the service program is started; if the service program is stopped in the client program, it is also necessary to stop without stopping. Waiting for the service program to run and reconnect. A type of asynchronous programming is provided below.

Network asynchronous applications involve several key points:

After the customer is started, the detection service application exists. If there is no existence, wait for the service application to start, and do not block the execution of the client to apply other tasks. Once the service application is started, the customer application should establish a connection with it.

Customer applications and service applications In data communication, the service application abnormally exits, the customer application should be able to detect the exit of the service application. At the same time, the client application automatically clears the communication link, returns to the initial state, and wait for the service application to restart.

This network is asynchronous programming first involves a timer and timer event. This timer is used in a constant detection network whether the client application and service application is connected, while aborting the data communication while the service application is abnormal, returning to the initial state. The fault of the network can be known through the abnormality processing of the network method.

The timer is included in the network communication class such that the application of the application is not perceived without the presence of the timer, and convenient processing network information.

The client program is the following structure:

Public Class Netcomm

IMPLEMENTS ACTIONLISTENER

{

Javax.swing.timer Timer = new javax.swing.timer (3000, this);

Socket sock;

PRIVATE EVENTNOTIFIER EN;

Public static int NET_STATE = 0;

Inetaddress serveraddr;

Int Serverport;

Public Netcomm (INETADDRESS ADDR, INT Port) {

ServerAddr = addr;

Serverport = port;

}

Public void netcomm_init () {

NET_STATE = 1;

Try {

SOCK = New Socket (ServerAddr, Serverport);

} catch (ioexception e) {

Net_State = 0;

}

Timer.Start ();

}

Public void netcomm_data ()

{

Try {

OutputStream outputstream = Sock.getOutputStream ();

BufferedWriter out = new bufferedwriter

NEW OUTPUTSTREAMWRITER (OUTPUTSTREAM);

Out.write ("java by ghf@china.com");

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

// Data processing

System.out.println (STR);

}

In.Close ();

} catch (ioexception e) {

NETCOMM_CLOSE ();

Net_State = 0;

}

Timer.Start ();

}

Public void netcomm_close ()

{

IF (Sock! = NULL)

Try {

Sock.close ();

} catch (ioexception e) {

}

}

Public Void ActionPerformed (ActionEvent E)

{

IF (Net_State == 0)

Netcomm_init ();

Else

Netcomm_data ();

}

}

In the above programs, a callback function can also be provided for external applications to notify the application at network abnormalities or restore normal. The network communication class of the service application can be placed in the same class.

About author

Guo Hongfeng, engaged in distributed application system development on UNIX systems, has been developed in four years of distributed application system development experience. E-mail: GHF_EMAIL@china.com.

转载请注明原文地址:https://www.9cbs.com/read-30752.html

New Post(0)