In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze asynchronous network programming in Java, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
At present, the java platform has been widely used in all kinds of client / server systems, and the asynchronous processing of the network is often needed in practical programming. For example, if the client program runs before the service program, the client program needs to automatically connect the service program after the service program starts; if the service program stops in the middle of the client program operation, you also need to wait for the service program to run and reconnect without stopping. A class of asynchronous programming methods are provided below.
Network asynchronous applications involve the following key points:
After the customer application is started, check whether the service application exists. If it does not exist, wait for the service application to start without blocking the execution of other tasks of the customer application. Once the service application is started, the customer application should establish a connection with it in a timely manner.
Customer application and service application in data communication, after the service application exits abnormally, the customer application should be able to detect the exit of the service application. At the same time, the customer application automatically clears the communication link, returns to the initial state, and waits for the service application to restart.
The network asynchronous programming first involves a timer and timer events. The timer is used to continuously detect whether the customer application and the service application are connected in the network, and at the same time stop the data communication when the service application is abnormal and return to the initial state. The fault of the network can be known through the exception handling of the network method.
The timer is included in the network communication class, so that the application using this class can not perceive the existence of the timer and process the network information conveniently.
The client class is structured as follows:
Public class.NETComm
Implements ActionListener
{
Javax.swing.Timer timer = new javax.swing.Timer (3000 Jing 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
/ / processing 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 actionPerformed (ActionEvent e)
{
If (net_state = = 0)
NetComm_Init ()
Else
NetComm_Data ()
}
}
In the above program, you can also provide a callback function for external applications to notify the application when the network is abnormal or returns to normal. The network communication classes of service applications are similar and can be placed in the same class.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.