In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Android realizes the unified interface of socket communication. I hope you will get something after reading this article. Let's discuss it together.
Android implements the unified interface of socket communication. After unifying the interface, we can use different functions similar to the current function but different from the bottom without much modification of the application layer code. Take the interface between UDP and TCP as an example.
Realization of UDP Communication and TCP Communication UDP Communication
When we use UDP communication, we will implement this
/ / set socketval socket = DatagramSocket () val serverPort = 9000val address = InetAddress.getByName ("ip address") / / send val bytes = message.toByteArray (Charsets.UTF_8) val len = DatagramPacket (bytes, len, address, serverPort) socket.send (sendPacket) / / receive socket.receive (receivePacket) val data = String (receivePacket.data, Charsets.UTF_8) / / process the received data / / close the connection socket.close () TCP client communication
When we use TCP client communication, we will implement this
/ / set socketval serverPort = 9000val address = InetAddress.getByName ("ip address") val socket = Socket (address, serverPort) val input = socket.getInputStream () val output = socket.getOutputStream () / / send output.write (message.toByteArray (Charsets.UTF_8)) / / receive val len = input.read (receive) val data = String (receive, 0, len, Charsets.UTF_8) / / process received data / / close connection socket.close ()
In this way, if we need to convert UDP connections in the application layer to TCP connections, we will have to modify the code a lot.
Use a unified interface
After unifying the interface, you can use different functions that are similar to the current functions but are implemented at the underlying level without the need for a large number of changes to the application layer code.
Take the two communication modes of UDP and TCP that we implemented before as an example, when we want to convert either of them into the other, or there is a new way of communication that needs to be adopted, it is obviously not a good idea to modify the application layer code every time.
We can simply analyze these two communication methods. They both go through initialization (setting socket) > sending or receiving-> processing data-> closing the connection, then we can abstract these common parts to the application layer for use.
Define Interfac
Create a new Communicate.kt file to implement the Communicate interface
Interface Communicate {/ * Communication Port * / var serverPort: Int / * address * / var address: String / * input Code * / var inCharset: Charset / * output Code * / var outCharset: Charset / * send data * @ param message data content * / fun send (message: String) / * * start receiving data * @ param onReceive function for processing received data The function returns a value of whether to continue to receive messages. * Please do not use the stopReceive () function in the function to stop receiving data, this will not work. * @ return whether successfully enabled * / fun startReceive (onReceive: OnReceiveFunc): Boolean / * stop receiving data * / fun stopReceive () / * enable communication for TCP to establish a connection * @ return whether successfully enabled * / fun open (): Boolean / * close communication * / fun close ()}
OnReceiveFunc is also used in the above code block, which uses type mapping in kotlin, similar to typedef in c, and here is the implementation of OnReceiveFunc, which takes a string as an argument and returns a Boolean variable.
Typealias OnReceiveFunc = (String)-> Boolean
In the specific use of the characteristics of kotlin, you can directly write the OnReceiveFunc method body.
Communicate.startReceive {binding.textView.text = it return@startReceive false}
The usage in java is as follows
Communicate.startReceive (result-> {binding.textView.setText (result); return false;})
Note: the communicate here is a communication object that implements the Communicate interface, and we don't care what kind of communication is used.
In this section, we can use static methods to make it easier for the application layer to create objects (that is, to choose the way you want to connect).
Interface Communicate {companion object {@ JvmStatic val TCPClient: Communicate get () = TCP () @ JvmStatic val UDP: Communicate get () = UDP ()} / / other code}
The @ JvmStatic annotation is used, which allows java to call Communicate without a layer of companion.
Implementation interface
We then implement the UDP and TCPClient classes, both of which implement the Communicate interface.
I have not implemented TCPServer. The two specific implementations that have been implemented can refer to my gitee repository.
Implement the application layer
This allows calls to be made in the application layer in the same style, such as declaring a UDP communication object
Privateval communicate = Communicate.UDP.apply {address = "ip address" serverPort = 9000 inCharset = Charset.forName ("gb2312") outCharset = Charset.forName ("gb2312") open ()}
Declaring an TCPClient communication object requires only this
Privateval communicate = Communicate.TCPClient.apply {/ / exactly the same as UDP}
The invocation part, not to mention, does not need to be modified at all. In this way, when we need to change the current communication mode, we only need to change Communicate.UDP to Communicate.TCPClient, which greatly reduces the workload of subsequent modifications.
After reading this article, I believe you have a certain understanding of "how Android realizes the unified interface of socket communication". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.