ASocket is a TCP/UDP protocol encapsulation library that facilitates and quickly implements TCP long connections and UDP unicast, multicast, broadcast and other related communications.
Unified management of TCP/UDP related Sockets through ASocket, making it suitable for Android, calling and callback in the UI main thread, and asynchronously processing the sending and receiving of messages in the sub-thread
You can also directly download the demo App to experience the effect
Add a remote repository in Project's build.gradle or setting.gradle
repositories {
// ...
mavenCentral()
}
Add imported dependencies in Module's build.gradle
implementation ' com.github.jenly1314:asocket:1.0.0 '
Multicast addresses are specific, and class D addresses are used for multicast. Class D IP addresses are multicast IP addresses, that is, IP addresses between 224.0.0.0 and 239.255.255.255, and are divided into three categories: local connection multicast addresses, reserved multicast addresses, and management rights multicast addresses: Local multicast addresses Broadcast address: between 224.0.0.0 and 224.0.0.255. This is an address reserved for routing protocols and other purposes. The router does not forward IP packets belonging to this range. Reserved multicast address: between 224.0.1.0 and 238.255.255.255, which can be used globally (such as the Internet) or network protocols. Management permission multicast address: between 239.0.0.0 and 239.255.255.255, which can be used within the organization. It is similar to a private IP address and cannot be used on the Internet. The multicast range can be limited.
//初始化一个ISocket的实现类(如:TCPClient、TCPServer、UDPClient、UDPServer、UDPMulticast)
val tcpClient = TCPClient (host,port)
//初始化ASocket
val aSocket = ASocket (tcpClient)
//设置状态监听
aSocket.setOnSocketStateListener( object : ISocket . OnSocketStateListener {
override fun onStarted () {
}
override fun onClosed () {
}
override fun onException ( e : Exception ) {
}
})
//设置接收消息监听
aSocket.setOnMessageReceivedListener { data ->
// TODO 接收消息
}
//启动
aSocket.start()
// ....
//发送消息
aSocket.write(data)
TCPClientActivity
TCPServerActivity
UDPClientActivity
UDPServerActivity
UDPMulticastActivity
For more usage details, please view the source code usage examples in the Demo or directly view the API help documentation
If you like ASocket, or feel that ASocket has helped you, you can click "Star" in the upper right corner to support it. Your support is my motivation, thank you?
You can also scan the QR code below and buy the author a cup of coffee☕
my blog | GitHub | Gitee | CSDN | Blog Garden |
---|---|---|---|---|
Jenly's Blog | jenly1314 | jenly1314 | jenly121 | jelly |
WeChat public account | Gmail | QQ mailbox | QQ group | QQ group |
---|---|---|---|---|
Jenly666 | jenly1314 | jenly1314 | 20867961 | 64020761 |