Asynchronous Network Programming in Java

Source: Internet
Author: User

 

This article describes how to compile a client server applicationJavaHelp programmers,The program continues to run stably when the other party fails..
CurrentlyJavaThe platform has been widely applied to various customers./In server systems, asynchronous network processing is often required in actual programming. For example, if the client program runs before the service program, the client program needs to automatically connect to the service program after the service program is started. If the service program stops midway through, you also need to wait for the service to run and reconnect without stopping. The following describes a kind of asynchronous programming methods.

Asynchronous Network applications involve the following key points:

After the customer starts the application, check whether the service application exists. If the task does not exist, wait for the service application to start and do not block the execution of other tasks of the customer application. Once the service application is started, the customer application should be connected to it in a timely manner.
In data communication, the customer application should be able to detect the exit of the service application after the service application unexpectedly exits. At the same time, the customer application automatically clears the communication link and returns to the initial state, waiting for the service application to restart.

Asynchronous Network Programming involves a timer and a timer event. This timer is used to continuously check whether the customer application and service application are connected in the network. When the service application encounters an exception, the data communication is terminated and returned to the initial state. Network faults can be identified through network exception handling.

The timer is included in the network communication class, so that the application using this class cannot perceive the existence of the timer, and the network information is conveniently processed.

The client program class has 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
//Process Data
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 actionreceivmed (ActionEvent e)
{
If (net_state = 0)
NetComm_Init ();
Else
NetComm_Data ();
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.